Flow Control - itzjac/cpplearning GitHub Wiki

For the programs we have seen so far the execution of the program is done in a linear way, line by line starting at main, until it reaches the end

1: #include 
2: 
3: int main()
4: {
5:    int x = 5;
6:    int y = 0;
7:
8:    int sum = x +y;
9:    std::cout<<"Sum of " << x << " + " << y <<" = " << sum << std::endl;
10:    return 0;
11: }

The program starts at line 3, where the main is, continues executing the instruction at the next line 4, and so on.

It reaches the end of the program when the curly brackets at line 11 is found and the program ends.

The next instruction we are going to learn, will change the order in which the program is executed. Those instructions are named Flow Control instructions, because they modify the flow of the program.

We are going to define two kinds of flow control instructions: looping and conditional.

Boolean tables

Relational Operators

int a = 5; int b = 0; int c = 15; int d = -3;


bool b0 = a != b; // 5 != 0, so b0 = true
bool b1 = a == (c/3); // 5 == 15/3, so b1 = true
bool b2 = b < d; // 0 is not < -3, so b2 = false
bool b3 = d < b; // -3 < 0, so b3 = true
bool b4 = a >= b; // 5 >= 0, so b4 = true
bool b5 = c <= d; // 15 is not <= -3, so b5 = false

Logical operators And Not

Logical operators Or XOr

bool A = true; bool B = false; bool C = true;


A && !B
!B || C


(A || C) && !(A && C)
= (T || T) && !(T && T)
= T && !T
= T && F
= F