flow control - sluczak/cpp_by_example GitHub Wiki

Return to the previous lesson streams

Flow control


Examples for this part available on git after calling the command:

git checkout -b origin flow-control

Conditions

In order to execute some code conditionally, C++ delivers three condition-statements.

If condition

"If-else" clause is the most common way of executing some code conditionally.

The syntax of such clause is as following:

if(!isBusy && (age >= 21)) {
   //go to the bar
}

The following example checks if isBusy is false and age is greater or equal than 21.

In such case, the code in scope with //go to the bar comment will execute.

If we want to do something else if condition is not met, you can define else condition else condition has to be defined directly after the closing bracket } of if condition:

if(!isBusy && (age >= 21)) {
   //go to the bar
} else {
   //go and play with schoolmates
}

What is more, "if-else" clauses can be chained in order to check more variants of condition evaluation. This occurs when you put if keyword directly after the else.

if (!isBusy && (age > 21)) {
	cout << "Go to the bar" << endl;
} else if (!isBusy && (age > 12)) {
	cout << "Go to the cinema" << endl;
} else {
	cout << "Go to the sanbox" << endl;
}

Hint: else clause is optional

if-else doesn't require to always add the scope brackets, but it is a good practise

The alternative to if-else is the three-argument operator condition ? doForTrue : doForFalse; described in operators

There is also a second conditional evaluator - switch-case expression. This will be introduced in the next lessons.

Loops

Loops are used to evaluate some expressions repedately , untill some condition is met.

'For' loop

The most common loop is the for loop. This expression allows you to define preconditions, condition and action for each iteration in one line.

The for syntax is as follows: for(preconditions; condition; do_for_each_iteration) { expression_to_repeat }

And this is how it looks like in practice:

int bricksToMove = 20;
for(int currentBrick = 0; currentBrick < bricksToMove; currentBrick++) {
     //move the 'i' brick;
}

This code shows, that we can define some variables ( currentBrick) in preconditions area (but we don't have to), and modify this value in do_for_each_iteration area. i value can be used inside of for expression scope. The for loop will end its execution when currentBrick is greater or equal than 20.

For loops can be nested, but this radically raises the algorithm complexity!

'While' loop

While loop is an alternative for the 'for' loop. It alows you to define only some condition. Until this condition is met, the loop will execute.

Remember to avoid the infinite loops!

"While" loop has a much simplier syntax:

while(condition) { expression_to_repeat }

Example in C++ will look as follows:

int year = 2015;
while (year > 2000) {
	year--;
	cout << "I still check years in while!" << endl;
} 

"While" loop can be also seen as a do-while expression:

do {expression_to_repeat} while(condtion);

This assures that the expression_to_repeat will be performed at least once, even if condition is not met.

Proceed to the next leson: Visual Studio 2013 features