Friday, October 3, 2014

3.1a DECISION MAKING & BRANCHING

In practice, we have a number of situations where we may have to change the order of execution of statements based on certain conditions, or repeat a group of statements until certain specified conditions are met. This involves a kind of decision making.

C language possesses such decision-making capabilities by supporting the following statements:
Ø  if Statement
Ø  switch Statement
Ø  Conditional Operator Statement
Ø  goto Statement

 
IF STATEMENT
§  General form:
            if (this condition is true)
                    execute this statement;

§  Syntax:
            if (test-expression)
            {
                        statement-block;
            }
            statement-x;

§  The keyword ‘if’ tells the compiler that what follows is a decision control statement.
§  The condition following the keyword ‘if’ is always enclosed within a pair of parenthesis.
§  If the condition is true, then the statement is executed. If the statement is not true, then the statement is not executed.
§  To express a condition we use relational operators.
<          <=        >          >=        = =       !=
§  Flowchart:
 
IF-ELSE STATEMENT
§  General form:
            if (this condition is true)
                    execute this statement;
            else
                    execute this statement;   

§  Syntax:
            if (test-expression)
            {
                        true-block statement;
            }
            else
            {
                        false-block statement;
            }
            statement-x;

§  If the condition is true, then the statement(s) immediately following the ‘if’ statement are executed, otherwise the false-block statements are executed.
§  Flowchart:

NESTED IF-ELSE STATEMENT
§  When a series of decisions are involved, we may have to use more than one if…else statement in nested form.
§  General form:
            if (condition-1)
            {
                        if (condition-2)
                        {
                                    stmt-1;
                        }
                        else
                        {
                                    stmt-2;
                        }
            }
            else
            {
                        stmt-3;
            }
            stmt-x;

§  If condition-1 is false, stmt-3 will be executed, otherwise it continues to perform the second test.
§  If condition-2 is true, stmt-1 will be executed otherwise stmt-2 will be evaluated and then the control is transferred to stmt-x.
§  Flowchart:

ELSE-IF LADDER
§  A multipath decision is a chain of if’s in which the statement associated with each else is an if.
§  General form:
            if (condition-1)
                        stmt-1;
                else if (condition-2)
                        stmt-2;
                    else if (condition-3)
                           stmt-3;
                        else if (condition-4)
                                stmt-4;
                                    else
                                        default-stmt;
            stmt-x;       

§  This construct is known as the else-if ladder. The conditions are evaluated from the top, downwards.
§  As soon as a true condition is found, the statement associated with it is executed and the control is transferred to the stmt-x (skipping the rest of the ladder).
§  When all the n-conditions become false, then the final else containing the default-stmt will be executed.
§  Flowchart:
 
RULES FOR INDENTATION
When using control structures, a statement often controls many other statements that follow it. In such situations it is good practice to use indentation to show that the indented statements are dependent on the preceding controlling statement. Some guidelines that could be followed while using indentation are listed below:
v  Indent statements that are dependent on the previous statements; provide at least three spaces of indentation.
v  Align vertically else clause with their matching if clause.
v  Use braces on separate lines to identify a block of statements.
v  Indent the statements in the block by at least three spaces to the right of the braces.
v  Align the opening and closing braces.
v  Use appropriate comments to signify the beginning and end of blocks.
v  Indent the nested statements as per the above rules.
v  Code only one clause or statements on each line.
 
SWITCH STATEMENT
§  When we have to select one of the many alternatives, we can use an if-statement to control the selection. However, the complexity of such a program increases as the number of alternatives increase.
§  C has a built-in multiway decision statement known as switch. The switch statement tests the value of a given variable (or expression) against a list of case values and when a match is found, a block of statements associated with that case is executed.
§  General form:
switch (expression)
{
            case value-1:
                        block-1;
                        break;
            case value-2:
                        block-2;
                        break;
            …..
            …..
            default:
                        default-block;
                        break;
}
next-stmt;
                      
            where:       
                                i.            expression is an integer expression or character.
                              ii.            value-1, value-2, … are constants or constant expressions and are known as case labels.
      NOTE: Float values are not accepted.
      NOTE: Each of these values should be unique within a switch statement.
      NOTE: Case labels ends with a colon(:)
                            iii.            block-1, block-2,…. Are statement lists and may contain zero or more statements.
                            iv.            The break statement at the end of each block signals the end of a particular case and causes an exit from the switch statement, transferring the control to the next-stmt following the switch.
§  When the switch is executed, the value of the expression is successfully compared against the values value-1, value-2,… If a case is found whose value matches with the value of the expression, then the block of statements that follow are executed.
§  The default is an optional case. (It can be placed anywhere but usually is placed at the end).
§  It is permitted to nest switch statement.
§  Flowchart:


Switch versus if-else ladder
Switch works faster than an equivalent if-else ladder.
            This is because, the comp iler generates a jump table for a switch during compilation. As a result, during the execution it simply refers the jump table to decide which case should be executed, rather than actually checking which case is satisfied.
            If-else’s are slower because the conditions in them are evaluated at execution time.

NOTE: In case of questions having range, if-else is a better option than switch statement.

GOTO STATEMENT
§  The C language supports the goto statement to branch unconditionally from one point to another in the program.
§  The goto statement requires a label in order to identify the place where the branch is to be made.
A label is any valid variable name, and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred.

§  General form:
 
§  The label : can be anywhere in the program either before or after the goto label statement.
§  During the running of the program when a statement like:
goto begin;

is met, the flow of control will jump to the statement immediately following the label begin:. This happens unconditionally.

NOTE that a goto breaks the normal sequential execution of the program.

           If the label is before the statement goto label; a loop will be formed and some statements will            be executed repeatedly. Such a jump is known as a backward jump.
            On the other hand, if the label: is placed after the goto label; some statements will be skipped and the jump is known as a forward jump.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.