Learn Programming

Learn D Programming Lesson 6 - Loops

What is a loop?

A loop is something that is used in a program to repeat things. A loop lets you repeat a block of code without having to type it out as many times as you want to repeat it.

For loops

A for loop repeats commands a certain number of times between two numbers. You create a for loop using the for keyword followed by the loop instructions in brackets and then followed by the code to repeat in the loop. Here is a very basic example that prints out the word Hello 5 times.

for (int i = 1; i <= 5; i++)
   writefln("Hello");

There are 3 instructions inside the brackets of a for loop that are separated by semi-colons. The first one initailizes the loop variable. The loop variable is a variable that counts the number of times the loop runs. In the example above the first instruction is "int i = 1" which initializes the loop variable of type integer to the value of 1. The second instruction is the condition that must be met for the loop to continue running the loop. The second instruction in the example is "i <= 5" which says that the loop must keep repeating while the loop variable is less than or equal to 5. The third instruction is a command to increment the loop variable. In the example above it is "i++" which increments the value of i by 1. The ++ is a special operator that is used to increment variables by 1 and means the same as i = i + 1. There is also a -- operator which decrements a variable by 1 which you can use if you want to run a loop backwards. The last part of the loop is the command to repeat which in this example is "writefln("Hello");". Notice how it has been indented to help you see that it belongs to the loop.

If you want to repeat more than one line of code inside a loop then you must put the code between curly brackets. Here is an example that has 2 commands. The first one prints the loop variable and the second one prints Hello.

for (int i = 1; i <= 5; i++)
{
   writefln(i);
   writefln("Hello");
}

While loops

A while loop keeps repeating the code inside it until a condition is met. You create a while loop using the while keyword followed by the condition in brackets and then followed by the code. The following is an example of how to use a while loop that prints out the word Hello 5 times just like in a previous example.

int i = 1;
while (i <= 5)
{
   writefln("Hello");
   i = i + 1;
}

In the example above you will see that the loop counter variable had to be initialized before starting the loop. Notice how you also have to increment the variable inside the loop. Be careful because it is easy to forget to increment the loop variable in a while loop and forgetting to do this will create an infinite loop. When learning about the while loop for the first time beginners often make the mistake of thinking that it ends the loop as soon as the loop variable makes the loop condition false no matter where you are in the loop. What really happens is that the loop condition is tested only at the beginning of each iteration of the loop.

Do while loops

Do while loops are the same as while loops except that the condition is tested at the bottom of the loop instead of the top. This means that a do while loop will always run at least once which is different to a while loop which makes it possible to not enter the loop if that is what you need. Here is an example of how to use a do while loop which asks the user for a password until the user enters the correct one.

string password;
do
{
   writefln("Please enter the password");
   password = din.readLine();
} while (password != "pass");

Foreach loops

A foreach loop is similar to a for loop but it is much easier to use. foreach loops are mostly used with arrays. You will learn more about this type of loop later in the lesson about arrays.

The break statement

The break statement is a command you can use when you need to exit a loop before the loop condition is met. Here is an example which would usually print Hello 5 times but exits the loop early after printing it only 3 times.

for (int i = 1; i <= 5; i++)
{
   writefln("Hello");
   if (i == 3)
      break;
}

Practice

Try write a program with a loop that iterates 10 times. Each time it must print the same number of x's on screen as the loop variable. So on the first line it will print 1 x and on the second line it will print 2 x's and so on up until 10. Make sure that you solve the problem first with a for loop and then convert it to use a while loop.