Unit 04: Control flow

  1. Boolean expressions
  2. Logic operators
  3. Logic operator example
  4. Truth tables (AND)
  5. Truth tables (OR)
  6. Truth tables (NOT)
  7. If statements
    1. If statements
    2. If/else statements
    3. If/else if statements
  8. While loops
    1. While loops
    2. Example while loop uses
    3. Continue and break
  9. Switch statements

  10. Example code and additional resources

Control flow

Boolean expressions

In order to start writing questions that a computer can understand, we need to understand boolean expressions.
A boolean expression is a statement that result in either true or false:

  • "A is less than B"
  • "2 + 4 is equal to 100"
  • "The sky is green"
  • "You are a worm AND I still love you"
  • "I have a cookie OR you have a cookie"

Whether the statement is true or not, as long as it can be answered with true (1) or false (0) then it is a Boolean Expression... and the type of statement that a computer understands.

Logic operators

We can also combine boolean expressions together to make one big expression. I usually call the "parts" of this a "sub-expression" to differentiate it from the expression as a whole.

NOT operator: !

The NOT operator takes an expression and flips the result...
if the sub-expression was TRUE, then NOT THAT will be FALSE, and vice versa.

AND operator: &&

We can combine two or more sub-expressions together using the AND operator.
The full expression that is only TRUE if ALL SUB-EXPRESSIONS ARE TRUE...
If even one of the sub-expressions is FALSE, then the entire expression is FALSE.

OR operator: ||

We can also combine two or more sub-expressions with OR.
The entire expression is TRUE if at least one sub-expression is TRUE...
The entire expression is only FALSE if EVERY sub-expression is FALSE.

Logic operator example

  • Sub-expressions: has_cats is TRUE and has_dogs is FALSE

  • "Do they have pets?"

    has_cats || has_dogs = TRUE OR FALSE... So the result is TRUE

  • "Do they have both cats AND dogs?"

    has_cats && has_dogs = TRUE AND FALSE... So the result is FALSE

  • "Is it NOT true that they have cats?"

    !( has_cats ) = NOT (TRUE) = FALSE

  • "Is it NOT true that they have dogs?"

    !( has_dogs ) = NOT (FALSE) = TRUE

Truth tables (AND)

A handy tool to figure out the result of an expression, given all the sub-expressions, is a truth table. With a truth table, we list out all sub-expressions, their possible states (true or false for each) and the possible combinations of those states, and what the result of the full expression for those states.

has_cats has_dogs has_cats && has_dogs

Using AND means the expression is ONLY TRUE


Truth tables (OR)

has_cats has_dogs has_cats || has_dogs

Using OR means the expression is ONLY FALSE


Truth tables (NOT)

has_cats !(has_cats)

Using NOT means the expression result is the OPPOSITE of the internal sub-expression.

If statements

In programming, we can change the behavior of our program by asking these true/false, yes/no questions. Choosing between "path A", "path B", or others is known as BRANCHING.

To create a branch, we need to ask a boolean expression - the branch is taken if the expression evaluates to TRUE.

The simplest form is having one if statement, which executes special code only if its expression is TRUE.

Generic form:

// Do things 1

  // Do things 1-a

// Do things 2

We'll step through some example code later.

If/else statements

We can also create a branch where there are two paths to take. If the expression evaluates to TRUE we do "path A", and if the expression evalutes to FALSE we do "path B".

Generic form:

// Do things 1

  // Do things 1-a
  // Do things 1-b

// Do things 2

If/else if statements

We can also combine multiple expressions into a chain of IF, ELSE IF statements, including an optional ELSE at the end to be the "catch all" if none of the above expressions evaluate to true.

With an IF/ELSE IF statement, we can check for several different conditions. The program reads the conditions from TOP to BOTTOM, and whichever condition evaluates to TRUE first is the one that gets executed - the rest below it get ignored.

Generic form:

// Do things 1
  // Do things 1-a
else if ( CONDITION2 )
  // Do things 1-b
else if ( CONDITION3 )
  // Do things 1-c
  // Do things 1-d
// Do things 2

While loops

While loops are similar to if statements in that they require some boolean expression as a CONDITION to execute its contents:

while ( CONDITION )
  // Do stuff repeatedly

The difference, however, is that the WHILE LOOP will continue running its contents WHILE the boolean expression evaluates to TRUE... an IF statement only executes its contents once.

INFINITE LOOP! -- It's easy to accidentally make your program loop forever with a while loop. This happens when the CONDITION never valuates to FALSE. To avoid this, we need to make sure something happens within the while loop that will cause the condition to eventually become FALSE.

Example while loop uses

Example uses for a while loop (click each to open/close)...

continue and break

There are special commands continue and break that we can use within a loop.

  • continue; will stop the current iteration of the loop and start the next iteration.
  • break; will leave the loop altogether.

Switch statements

Switch statements are a special type of branching mechanism that only checks if the value of a variable is equal to one of several values. Switch statements can be useful when implementing a menu in a program, or something else where you only have a few, finite, discrete options.

In C++, switch statements only work with primitive data types, like integers and chars - not strings.

Generic form:

switch ( VARIABLE )
 case VALUE1:
  // Do thing

 case VALUE2:
  // Do thing

  // Default code

case VALUE1: is equivalent to if ( VARIABLE == VALUE1 ).

default: is equivalent to the final else case, the catch-all.

Example code and additional resources

Example code: (repository)

These will be available after class!

  1. IF:
  2. IF/ELSE:
  3. IF/ELSE IF:
  4. WHILE:
  5. SWITCH:

Archived videos and class lectures: