c# · level 1 · unit 5 · lesson 1
- meet it
- guess first
- your turn
- fix it
- nailed it
Tap a green word if you get lost.
meet it
A does the same job again and again.
A loop repeats a block of code, so you never copy and paste. A for loop counts for you. for (int i = 1; i <= 3; i++) has three parts, split by semicolons.
Start: make a box i holding 1. Keep going while: run the block as long as i <= 3 is true. Step: after each turn, i++ adds one to i, because means add one.
So the block runs with i as 1, then 2, then 3. When i reaches 4, the condition is false and the loop stops. You can use i inside the block.
using System;
for (int i = 1; i <= 3; i++)
{
Console.WriteLine($"Hooray number {i}");
}
Console.WriteLine("Party over!"); i is a short name people often give the counting box in a for loop.
guess first
Where does it stop? Guess first.
Guess before you run it. Wrong guesses are how you learn, and nothing is marked.
using System;
for (int number = 1; number < 5; number++)
{
Console.WriteLine(number);
}
your turn
Your turn. Run a few laps.
Choose how many laps to run, as a whole number of at least 1, and type it in the blank. Then press run.
output
press run to see what happens
fix it
The bunny should hop three times. Can you fix it?
output
press run to see what happens
- Run it. The bunny only hops twice. Look at the keep-going part of line 3.
- When
hopis 3, ishop < 3stilltrue? - Change
<to<=on line 3, then press run.
nailed it
your code can count now. :D
- A loop repeats a block, so you never copy and paste.
- A
forloop has a start, a keep-going check and a step. i++adds one toiafter every turn.
new words in your collection
- loop
- for
- ++
This lesson isn’t saved. A free account remembers every lesson you finish.