# C++ Tutorial for noobs - Part 7: For, While and Do-Until

...Back to Part 6: If, Else and Switch

## For

### Motivation

Let's say we want to show all the numbers from 0 to 99 in C++ with what we know so far:

``````std::cout<<"number:"<<0<<"\n";
std::cout<<"number:"<<1<<"\n";
std::cout<<"number:"<<2<<"\n";
std::cout<<"number:"<<3<<"\n";
std::cout<<"number:"<<4<<"\n";
std::cout<<"number:"<<5<<"\n";
std::cout<<"number:"<<6<<"\n";
std::cout<<"number:"<<7<<"\n";
std::cout<<"number:"<<8<<"\n";
std::cout<<"number:"<<9<<"\n";
std::cout<<"number:"<<10<<"\n";
std::cout<<"number:"<<11<<"\n";
std::cout<<"number:"<<12<<"\n";
std::cout<<"number:"<<13<<"\n";
std::cout<<"number:"<<14<<"\n";
std::cout<<"number:"<<15<<"\n";
std::cout<<"number:"<<16<<"\n";``````

Okay let's stop right here, obviously it would be a lot of work to do such a relatively simple task.

### Understanding For

Let's see how we can make our lives easier here...

In one of the earlier parts of this tutorial series we talked about variables. Maybe we could use some kind of int variable and do something like:

``````int i = 0;
std::cout<<"number:"<<i<<"\n";
i = i + 1;
std::cout<<"number:"<<i<<"\n";
i = i + 1;
std::cout<<"number:"<<i<<"\n";
i = i + 1;
...``````

This way we won't have to write 1, 2, 3, 4, 5, ... anymore, which makes it a bit easier already. Now the question is, can we automate this part:

``````std::cout<<"number:"<<i<<"\n";
i = i + 1;``````

so it does the same thing for all numbers between 0 and 99? Good guy C++ says we can. A for loop does exactly that, it does 'SOMETHING' for each number from a certain start to a certain end.

Here is how it works:

``````for (int i = 0; i <= 99; i = i + 1) {
std::cout<<"number:"<<i<<"\n";
}``````

Or in other words:

• for a int variable i with the initial value 0
• while the variable is less than or equal to 99
• show the number message
• and then increase the variable by one

Feel free to try it out, it does exactly what we want it to do.

### Common writing style

Now even though the above code seems just about alright, we usually see it written in a slightly different way like this:

``````for (int i = 0; i < 100; ++i) {
std::cout<<"number:"<<i<<"\n";
}``````

It does the exact same thing, but:

• i < 100 is (mathematically seen) more pretty than i <= 99
• ++i is the short form for i = i + 1

Hint: don't get confused when you see that 99% of all the tutorials out there use i++ instead of ++i. It works too, but it's not the right way to do it.

## While

For-loops are cool, but what if we want to do something forever or until the user presses a stop button or something like that?

C++ comes with the while construct, which does just that:

``````while (start == true)
{
std::cout<<"still working...\n";
}``````

The condition between ( and ) is the same kind of condition that is used in if statements. As long as the condition returns true, the stuff between { and } will be executed over and over and over and over and over and over and over and over and over and over and over and over and over and over again.

## Do

There is another loop construct which is called do-while and it looks like this:

``````do
{
std::cout<<"still working...\n";
} while (start == true)``````

The difference is that in do loops the condition is checked in the end, while in while loops the condition is checked in the beginning each time.

There are some use cases for do, but for our own sanity we won't go more into detail there. For and While are all we need for now.

## Break

Okay, we are almost done. One last thing to learn is break, which can be used to stop any kind of loop like this:

``````for (int i = 0; i < 100; ++i) {
std::cout<<"number:"<<i<<"\n";

// stop it at number 42
if (i == 42) {
break;
}
}``````

The above example results in all numbers from 0 to 42 to be shown.

Note: as stated before, this can be used in while and do loops as well.