C# Lesson 5 - Loops

One other thing computers are very good at is repeating instruction aka. Loops or Iteration.

There are 3 main kind of loops

  • For Loops
  • Do Loops
  • While Loops

While Loop

The following program repeats while ‘i’ is greater than zero

Note the use of i--, this takes one off whatever the value of ‘i’ is. It is a shorter version of writing i=i-1



While Loops are used in all the programs you currently have running!



    //Run program


Only when you exit the program does this loop end, use carefully!

Do Loop

Both the while and the do loop appear to do the exact same thing. The difference is a While loop may never run, where as a Do Loop will always run at least once.


Changing the i=10 line to i=0 in both loops and see the difference!


For Loops

For Loops will be the loop you are likely to use the most.  



Write a program that will;

Output the square of each number up to and including 20

Output the Square Root of the numbers between 2 and 25

Make you output for program 1 look like this