Skip to content
wannacode

Loops

Problem

How much will you save?

You decide to save money: the first month you put aside €10, the second €20, the third €30, and so on, €10 more each time. How much will you have after 4 months?

int total = 0;
total += 1 * 10;
total += 2 * 10;
total += 3 * 10;
total += 4 * 10;
Console.WriteLine(total);
Output
100

It works. But after 3 years that is 36 lines, almost all the same. And if the number of months is typed on the keyboard, you have no idea how many lines to write. You need a way to say "repeat this so many times".

The idea

for: repeat while the condition is true

A piece of code that repeats is called a loop. The one you will use most is for. Here are the numbers from 1 to 10:

for (int i = 1; i <= 10; i++)
{
Console.WriteLine(i);
}
Output
1
2
3
4
5
6
7
8
9
10

The parentheses hold three parts, separated by ;:

  • Start: int i = 1. It runs once, before anything else. This is where the counter is created.
  • Condition: i <= 10. It is checked before every round. If it is true, the body runs. If not, the loop ends.
  • Step: i++. It runs after every round.

Each run of the body is called an iteration, or simply a round.

More precisely, if you are curious
  • The step can be whatever you need: i += 2 (every other number), i-- (backward).
  • The counter i exists only inside the loop. After the closing } it is gone, and if you use it, the program does not compile.
  • You will often see counting from 0: for (int i = 0; i < 10; i++). It is still 10 rounds. Why you count from 0 will become clear in the lesson on arrays.

See it

Round by round

These are the savings from the first card, with a loop. Watch how month and total change.

int total = 0;
for (int month = 1; month <= 4; month++)
{
total += month * 10;
}
Console.WriteLine(total); // 100
Step by step, in words
  1. total starts at 0.
  2. month = 1. Check: 1 <= 4 is true, so we go into the body.
  3. Round 1: total += 1 * 10, total becomes 10.
  4. month++ makes month 2. 2 <= 4: one more round, total becomes 30.
  5. month 3: total becomes 60.
  6. month 4: total becomes 100.
  7. month++ makes month 5. 5 <= 4 is false: the loop ends.
  8. After the loop total is 100.

The body is one line, and it is written once. For 36 months you only change 4 to 36. You can even put a variable read from the keyboard there.

Try it

Countdown

What will the program print?

for (int sec = 3; sec > 0; sec--)
{
Console.WriteLine(sec);
}
Console.WriteLine("Go!");
Answer

The idea

while: until the end comes

Sometimes you don't know ahead of time how many rounds there will be. The cash register reads prices until the cashier enters 0, and then it gives the total. for does not fit here. while does: just a condition, no start and no step.

int total = 0;
int price = int.Parse(Console.ReadLine());
while (price != 0)
{
total += price;
price = int.Parse(Console.ReadLine());
}
Console.WriteLine("Total: €" + total);
Input
12
4
7
0
Output
Total: €23

The first price is read before the loop, so the condition has something to check. The next one is read at the end of the body. If the first number is 0, the body does not run even once.

If you want the body to run at least once, there is do-while. The condition is at the bottom and is checked after the round. It is handy for a question that repeats until the answer is acceptable:

int count;
do
{
Console.WriteLine("How many tickets (1 to 10)?");
count = int.Parse(Console.ReadLine());
} while (count < 1 || count > 10);
Console.WriteLine("Tickets booked: " + count);
Input
0
15
3
Output
How many tickets (1 to 10)?
How many tickets (1 to 10)?
How many tickets (1 to 10)?
Tickets booked: 3
More precisely, if you are curious
  • If the condition never becomes false, the loop never ends. This is an infinite loop: the program "hangs" and you have to stop it by hand. The most common cause is a forgotten change to the variable in the condition, for example a missing price = … at the end of the body.
  • After the while (…) of a do-while there is a ;. Without it the program does not compile.
  • Any for can be written as a while, and the other way around. You pick the one that reads more clearly: for for a known number of rounds, while for "until something happens".

Try it

Find the bug

The program should add up the numbers from 1 to 5: 1 + 2 + 3 + 4 + 5 = 15. It prints 10. Which line has the bug?

Pick the line with the bug.

The idea

break and continue

Two words change the usual flow of a loop.

continue skips the rest of the current round and moves on to the next one. You are listing appointment times from 9 to 17, but 13 is the lunch break:

for (int hour = 9; hour <= 17; hour++)
{
if (hour == 13)
{
continue;
}
Console.WriteLine(hour + ":00");
}
Output
9:00
10:00
11:00
12:00
14:00
15:00
16:00
17:00

break stops the whole loop right away. You are looking for the first number that 91 divides by. Once you find it, there is no point in going on:

int n = 91;
for (int d = 2; d < n; d++)
{
if (n % d == 0)
{
Console.WriteLine(n + " is divisible by " + d);
break;
}
}
Output
91 is divisible by 7
More precisely, if you are curious
  • break stops only the loop it is in. If it is in a nested loop (next card), the outer one keeps going.
  • You will often see while (true) with a break inside: a loop that ends not because of its condition, but because of a break in the right place.

The idea

A loop inside a loop

The movie theater has 3 rows of 4 seats. You want to print every seat. For each row you go through all the seats in it:

for (int row = 1; row <= 3; row++)
{
for (int seat = 1; seat <= 4; seat++)
{
Console.Write("[" + row + "-" + seat + "]");
}
Console.WriteLine();
}
Output
[1-1][1-2][1-3][1-4]
[2-1][2-2][2-3][2-4]
[3-1][3-2][3-3][3-4]

These are called nested loops. The inner loop goes all the way through on every round of the outer one: 3 rows · 4 seats = 12 prints. Write keeps the seats on one line, and the empty WriteLine() after the inner loop moves to a new line.

Try it

Staircase

The first row should have one star, the second two, the fourth four. What goes in the blank?

for (int row = 1; row <= 4; row++)
{
    for (int star = 1; star <= ; star++)
    {
        Console.Write("*");
    }
    Console.WriteLine();
}

The idea

Random numbers for games

A dice game needs a number the program does not know ahead of time. Random gives you one:

Random random = new Random();
int dice = random.Next(1, 7);
Console.WriteLine("Rolled " + dice);

Random is an object, so you create it with new. What an object is, you will learn in the module "Classes and objects". For now, use it like this: you create it once and then ask it for as many numbers as you need.

random.Next(1, 7) gives a number from 1 to 6: the lower limit is included, the upper one is not.

Each run gives a different number, so there is no output here. And here is a loop that rolls until it gets a six:

Random random = new Random();
int rolls = 0;
int dice;
do
{
dice = random.Next(1, 7);
rolls++;
} while (dice != 6);
Console.WriteLine("Six after " + rolls + " rolls");
More precisely, if you are curious
  • If you give Random a starting number (new Random(42)), every run gives the same sequence. This helps with testing. Java and C# give different sequences from the same starting number.
  • The numbers are not truly random: they are computed with a formula. They are good enough for games, but not for passwords and security.

Exercise

Loop exercises

Solve the exercises in your editor. Each one has sample input and output to compare yours with. You read the number the same way as in the lesson "Input and output".

1. Multiplication table. Read a number and print its products with the numbers from 1 to 10.

Input
7
Output
7 x 1 = 7
7 x 2 = 14
7 x 3 = 21
7 x 4 = 28
7 x 5 = 35
7 x 6 = 42
7 x 7 = 49
7 x 8 = 56
7 x 9 = 63
7 x 10 = 70

2. Prime number. A number is prime if it divides only by 1 and by itself: 2, 3, 5, 7, 11… Read a number and say whether it is prime. Hint: try the divisors from 2 up, and stop with break at the first one.

Input
91
Output
91 is not prime

For 11 the output is "11 is prime".

3. The digits of a number. Read a positive whole number. Print it reversed and the sum of its digits. Hint: n % 10 is the last digit, and n / 10 drops the last digit. Repeat until the number becomes 0.

Input
2024
Output
Reversed: 4202
Sum of digits: 8

4. Guess the number. The computer "thinks of" a number from 1 to 100, and you look for it. After each guess the program says "It's lower." or "It's higher.", and at the end how many tries it took you. First write it with the secret number 37, so you can check it against the example:

Input
50
25
37
Output
It's lower.
It's higher.
You got it in 3 tries!

Once it works, replace 37 with a random number from 1 to 100. Now even you don't know the answer.

More exercises

  • From 1 to N. Read N and print the numbers from 1 to N on one line. For 5: 1 2 3 4 5.
  • Factorial. N! = 1 · 2 · 3 · … · N. For 5: 120. Use long: int only reaches 12!, while long reaches 20! (2432902008176640000).
  • Primes up to N. Print all prime numbers up to N. For 20: 2 3 5 7 11 13 17 19. The check from exercise 2 goes inside a loop over the numbers: nested loops.
  • Fibonacci. Each number is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8… Read N and print the first N numbers. For 10: 0 1 1 2 3 5 8 13 21 34.
  • Square of stars. Read N and print an N × N square. For 3:
Output
* * *
* * *
* * *
  • Triangle of stars. The same, but each row has as many stars as its number. For 3:
Output
*
* *
* * *
  • Coin toss. Read N, toss a coin N times with a random number (0 is heads, 1 is tails) and print how many times each came up. The bigger N is, the closer both numbers get to N / 2.
  • Rock, paper, scissors. A player against the computer. The player types a choice, the computer picks at random, and the program says who wins. With do-while it asks "Another game? (yes/no)" and at the end prints the score.

Test

Check yourself

1. How many times does the body of for (int i = 0; i < 5; i++) run?
2. What is the difference between while and do-while?
3. What does continue do?
4. The outer loop runs 3 times and the inner loop runs 4 times. How many times does the body of the inner loop run?
5. Which line gives a random number from 1 to 6?

Made by Ivo Gergov as part of Evo Tech's campaign for accessible programming education ·