Thursday, September 9, 2010

Object Oriented Language: C++, Control Flow



Control flow statements controls the flow of execution in a program. The control flow statement discuss here will not contain any loops; they are, however, taught in the next lesson.

Let's say you have to write a simple program that answers true/false to the given string.
"3+3=6" is the string, for example.
How do you write such a program? Let's not worry about the actual implementation of the program, and instead, look at the design and logic of the program. The program has to read in the string and decide if 3+3=6 is true or false. This decision is based on the result of the given condition. If the condition is true, the program outputs "true", else the program outputs "false". Simple. The way we tell our program to do that is by using the if/else statement.
if ( 3+3 == 6 ){
    cout << "true";
}else{
    cout << "false";
}
Notice in line 1, I wrote 3+3 == 6, instead of what is expected to be: 3+3 = 6. Since = operator is dedicated to assignment, the C programming language creator chose, Dennis Ritchie, to use the == instead to check for equality. The code itself should be self-explanatory and easy to comprehend.

The syntax & semantics for the if/else statement is as follow:
if ( condition ){
    // execute this block if the condition is evaluated to true
}
else{
    // execute this block if the condition is evaluated to false
}


if statement




if/else statement

If you only want to check if the condition is true, then just use the if statement. If there are multiple conditions to be checked, one can use the following syntax:
if(condition1){
... statement
}else if(condition2){
...statement
}
.
.
.
else if(conditionN){
...statement
}else{
...statement
}


if/else if/else statement



If you have many conditions to check the if/else if/else may be a little burden for most people, therefore, switch is specialized to do that kind of operations.
The syntax & semantics for switch is as follow:
switch(condition | int | char){
    case value1:
        //...statements
        break;
    case value2:
        //...statements
        break;
    ...
    ...
    ...
    case valueN:
        //...statements
        break;
    default: // optional, default case if none of the above matches
        //...statements
        break;
}


switch statement


There is a few things to note about a switch statement, I'll go over them with you one by one. switch can only take in a boolean value, integer, or a character, which means the following is prohibited:
string msg = "hi";
switch(msg){
    case "hi": // This is not allowed!
        ...
    ...
}

char aChar = 'x';
switch(aChar){ // example 1
    case 'a':
    ...
    ...
    ...
    case 'x':
        cout << "x";
        break;
    ...
    ...
}


int x = 10;
switch (x){ // example 2
    case 1:
        cout << "x = 1";
        break;
    case 2:
    case 3:
    case 4:
        cout << "x > 1 && x < 5";
        break;
    case 5:
        cout << "x = 5";
        break;
    case 6:
    case 7:
    case 8:
    case 9:
        cout << "x > 5 && x < 10";
        break;
    case 10:
        cout << "x = 1";
        break;
    default:
        cout << "x is not in between 1-10";
        break;
}
Another thing to note is that each case are ended with a break statement, the control of flow will flow through to other cases if it were not to be terminated by a break statement. For the second example above, if x were to be initialized to 6, the control flow will flow to case 6, continuing to 7 ,8, and finally terminated by the break statement in the case 9.

No comments:

Post a Comment