C++ Controls

To be or not to be, that is the question.

By default, instruction execution is sequential.  Conditional statement is used to change the program execution sequence.

If, If..else

choose one of the two options...

if (a > b) cout << "a is greater than b";

else cout << "a is less than or equal to b";    // only one of the two cout will be executed

What if we want to have more than one instructions in the option?

...  use curly braces to "block" the instructions.

if (a > b)  {

    cout << "a is greater than b";

    cout << "are you happy?";

}

else  {

cout << "a is less than or equal to b";    // only one of the two cout will be executed

}   

...  since there is only one instruction, braces are not needed, but it does not hurt.

What if the decision is not a simple one?

...  compound conditions are possible.

...  check out the logic operators

if (a > =10 && a < 100)  {

    cout << "a is a 2 digit number\n";

    cout << "are you happy?" << endl;    // note that endl and \n achieve the same result 

}

Relational  & Logical Operators

Conditional statements need a true or false "condition" (the bool type).  It is typically generated by a relational or logical operators.

Relational operators are:  >,    <,    >=,    <=,    ==  (equal),    != (not equal)

Note:

Logical operators are:  && (and),    || (or),    ! (not) 

Note:

Switch

switch (x) {

    case 1:  cout << "x is 1\n";

                    break;

    case 2:

    case 3: cout << "x is 2 or 3" << endl; 

                    break;

    case 4: cout << "x is 4\n";        // 4 will continue to 5

    case 5: cout << "x is 5\n"; break;

    default: 

                cout << "everything else" << endl;

}

Note:

Loop statement instructs the repetition of instructions.

For loop

While loop

Do While loop

Three different ways to solve the same problem : print 2 digit even numbers.

#define  LO     10

#define  HI      100

for (int i= LO; i <  HI; i=i+2)  cout << i << "   " ;

cout << endl;

i = LO;

while ( i <  HI) {

        cout << i << " ";

        i=i+2;

}

i = LO;

do {

    cout << i;

    i = i+2;

} while ( i < HI );

Note:

Loop Interrupt -- break and continue

Discussions:

for (int i= LO; i <  HI; i++)  {

if (i % 2 == 0) cout << i << "   " ;

}

cout << endl;

for (int i= LO; i <  HI; i++)  {

if ( ( i %2 ==0) && ( i%3 !=0) ) cout << i << "   " ;

}

cout << endl;

for (int i= LO; i <  HI; i++)  if ( i %2 ==0 &&  i%3  ) cout << i << "   " ;

cout << endl;

...... Although it is "less code", I won't recommend this style of programming for a simple reason: hard to read.  A few more parenthesis or braces or comparators do not really improve performance, but it sure makes code more readable.

for (int i= LO; i <  HI; i++) {

    if ( i % 2 ) continue;

    if ( i % 3 == 0 ) continue;

    cout << i<< "  ";

}

cout << endl;

...... If it is not an even number, we continue to next loop (i.e. skip the rest).  If it is a multiple of 3, we skip it too.  Coding can be a good brain exercise, right?