Chapter 6 - Notes

Conditional Execution

If-else statements

  • Conditional execution of code in C++ is done by using the if ... else construct:

if (condition) {
    // do something if the condition is true 
}
else {
    // do something else if the condition is false
}

Every expression in C++ is truthy, meaning that it can be evaluated as a boolean.

Anything that evaluates to zero is false. Conversely, anything that evaluates to any non-zero number is true.

  • The else component of the if ... else construct is optional. It can be committed in situations where no code is to be executed otherwise.

  • Nested if statements 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 ... else constructs 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;
}

What is the difference between using grouped if ... else statements as opposed to using multiple chained if statements?

Switch-case statements

  • The switch-case construct 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 break keyword causes the program to exit the current code block (the block belonging to the switch statement. This is not strictly necessary, but without the break statements, the program will continue checking each case label, which we want to avoid.

  • switch-case constructs are ideal for use with enums.

A program using switch-case can be adapted to use grouped if ... else instead. The benefit of using the former over the latter is that the syntax of the latter is a little more structured.

Ternary operator

  • The ternary operator is an operator that allows us to express an if .. else construct in a single line:

(condition) ? (expression if true) : (expression if false)
  • The program evaluates the condition before the ? – and if true, 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 expression evaluates to true.

  • It is important to structure the code block within such that there are situations where expression eventually evaluates to false, 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 loop

  • exit 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?