LOOPS

Loops let us execute a set of instructions multiple times.
There are two frequently used loops: FOR LOOP and WHILE LOOP.

You will use FOR LOOP more often but WHILE LOOP is easier to explain.

WHILE LOOP 

While loop will repeat something WHILE certain condition is true.

Notice how we first check the condition  x < 5  and if that is true, increment  x  by  1.

int x = 0

while x < 5
{
print x
x = x + 1
}

The program will output numbers 0 to 4.

Notice how curly braces outline the scope of the loop. Many languages use braces { } to outline the scope of loops, if statements, functions, etc.

Also, many languages will recognize x++ in place of x=x+1

This is called increment operator, something C++ language is named after.

Leave a comment

Your email address will not be published.