Chapter 6 - Notes
Conditional Execution
If-else statements
Conditional execution of code in C++ is done by using the
if ... elseconstruct:
if (condition) {
// do something if the condition is true
}
else {
// do something else if the condition is false
}The
elsecomponent of theif ... elseconstruct is optional. It can be committed in situations where no code is to be executed otherwise.Nested
ifstatements allow the user to validate against two or more conditions.
if (day == Sunday) {
std::cout << "Today is Sunday!" << std::endl;
if (weather == Rainy) {
std::cout << "You should stay inside today." << std::endl;
}
else {
std::cout << "You should go outside today!" << std::endl;
}
}The
if ... elseconstructs can also be grouped together:
if (day == Friday) {
std::cout << "Today is Friday!" << std::endl;
}
else if (day == Saturday) {
std::cout << "Today is Saturday!" << std::endl;
}
else {
std::cout << "Today is neither Friday nor Saturday..." << std::endl;
}Switch-case statements
The
switch-caseconstruct checks a particular expression against a range of possible constants, conditionally executing code if the expression matches each of the different values.
switch (expression) {
case value1:
// do something if expression == value1
break;
case value2:
// do something if expression == value2
break;
default:
// do something if all the above cases don't match
break;
}Each case label is required to be a constant.
The
breakkeyword causes the program to exit the current code block (the block belonging to theswitchstatement. This is not strictly necessary, but without thebreakstatements, the program will continue checking eachcaselabel, which we want to avoid.switch-caseconstructs are ideal for use with enums.
Ternary operator
The ternary operator is an operator that allows us to express an
if .. elseconstruct in a single line:
(condition) ? (expression if true) : (expression if false)The program evaluates the condition before the
?– and iftrue, returns the expression on the left of the:, and if false, returns the expression on the right of the:
int larger = (a > b) ? a : b;
// If `a` is larger than `b`, the ternary operator returns `a`
// If `b` is larger or equal to `a`, the ternary operator returns `b`Looping
Loops allow programs to repeat a block of code without repeating the block of code already written
While loops
while (expression) {
// do something
}The block within the while loop will execute repeatedly as long as
expressionevaluates to true.It is important to structure the code block within such that there are situations where
expressioneventually evaluates tofalse, otherwise the while loop will never end.
Do-while loops
do {
// do something
} while (expression);Do-while loops are the same as while loops. The only difference being the condition is evaluated at the end of each iteration.
This means that the block within the do-while loops always execute at least once.
For loops
for (initial expression; exit condition; loop expression) {
// do something
}initial expression: This expression is executed once – before the first iteration of the loopexit condition: This conditional expression is evaluated before the next iteration of the loop, and will determine whether the next iteration should run.loop expression: This expression is executed at the end of every iteration of the loop.
Range-based for loop
Range-based loops are a variant of the for loop that makes operating over a collection (array or vectors, for example) simpler to code and read
for (ElementType variable : collection) {
// `variable` corresponds to each element in the collection
// do something with `variable`
}This is frequently used with automatic type deduction (using the keyword
auto) for the type of each element in the collection.
Modifying loop behaviour
Nested loops
Last updated
Was this helpful?