×

c# · level 1 · unit 5 · lesson 1

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. 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.