Loops - While, For and Do-while

While Loop

The while loop is used to repeatedly execute a block of code as long as a specified condition is true.


#include <iostream>
using namespace std;

int main() {
    while(3 < 5) { // the condition is written in parentheses, and it can be any boolean expression
        cout << "3 is less than 5" << endl; 
    }
    return 0;
}
                

This will print "3 is less than 5" infinitely because the condition is always true.

Note that the condition can be anything that can be turned into a boolean expression, such as a number (integer and decimals) and a character (the ascii code is an integer, so a character is treated as one in C++).

For Loop

The for loop is used to execute a block of code a specified number of times.


                    #include <iostream>
                    using namespace std;
                    
                    int main() {
                        for(int i = 0 /*1*/; i < 5/*2*/; i++/*3*/) { 
                            cout << i << endl;
                        }
                        return 0;
                    }
                    

For for loops, statement 1 is run before the loop, and then the program will check if expression 2 is true. If it is, the loop will run the code block and then run statement 3. The loop will then check if expression 2 is true again, and if it is, the loop will run the code block again, and so on.

In this example, the loop starts from 0 (1), increments by 1 each time (2), and stops at 4 (3). The loop will print the numbers 0, 1, 2, 3, 4.

Note that the variables declared in the for loop, including statement 1, are only accessible within the loop. The statements 1 and 3, and expression 2 can be empty. for example:


                            for(;;){//infinite loop
                                cout << "Hello World" << endl;
                            }
                        
is valid.

Do-while Loop

The do-while loop is used to execute a block of code once, and then repeatedly executes the block as long as a specified condition is true. It is similar to a while loop, but the condition is checked after executing the code block.


                        #include <iostream>
                        using namespace std;
                        
                        int main() {
                            do { 
                                cout << "Hello World" << endl; 
                            } while(3 > 5); // the condition is written in parentheses, and it can be any boolean expression, note that there IS a semicolon after the while statement
                            return 0;
                        }
                        // Output: Hello World
                    
                    

Continue and Break Statements

The continue statement is used to skip the current iteration of a loop and move on to the next iteration. The break statement is used to stop the execution of a loop and exit the loop entirely.

Here is an example of using continue and break statements:


                        #include <iostream>
                        using namespace std;
                        
                        int main() {
                            for(int i = 0; i < 10; i++) { 
                                if(i == 5) {
                                    continue; // skip the current iteration and move on to the next one
                                } 
                                cout << i << " "; 
                            }
                            for(int i = 0; i < 10; i++) { 
                                if(i == 5) {
                                    break; // stop the execution of the loop and exit the loop entirely
                                } 
                                cout << i << " "; 
                            }
                            return 0;
                        }
                        // Output: 0 1 2 3 4 6 7 8 9 0 1 2 3 4
                    

Notice how the output skips 5 in the first loop and stops at 5 in the second loop.

Okay, now you know what a loop is, but there is no use for it unless you have a way to store a large amount of data at once, so hop on to the next chapter, arrays, to know how to store 1000000 variables at once!