Chapter 1 – Programming language: C++ – Part 2

Statements and flow control

if statement

The first and most important one is the if statement.

while statement

do – while statement

So what’s the difference between this and the one from above?

Well, the first one will check the condition first, and if it fails, it will not execute anything.

This one will execute the statements, and if the condition from the while fails, it will stop, otherwise it will execute them again, until the condition will no longer be true.

for loop

For can be used in the same context as a while – but most of the times we use for when the number of iterations is known from the start.

break and continue

Having such loops, we also need a way to exit them, or to not execute statements for specific cases – and for that, we have break and continue.

The loop will start from 0 and go until 10. If the value i is even, we skip it and go to the next value. If the value of i is 5, we exit the for loop.

switch

Switch is the equivalent of multiple if -else if statements.

Each case should have a break at the end – otherwise, the code will continue to fall to the next case.

You may also like...