Control Statements


>       Example Programs 


Control Statements
is nothing but, for controlling the execution of the statements, we are using some statements, they are called Control statements, there are classified in the following way according to controlling the statements...

            1.         Conditional Control Statements

            2.         Un-Conditional Control Statements.

            3.         Iterative Statements

                      Conditional Control Statements:-
This type of statements will allow to control, to execute, the statements after satisfied any condition

                        i.        Simple if

                        ii.         if....else

                        iii.       if...if...else..if.. Nested

                        iv.        if...else if... Ladder

                        v.       Switch Statement.

                     Un-Conditional Statement:-
This type of statements will execute the statements without checking any condition

                        i.          goto

                        ii.         break

                        iii.       continue

                      >   Iterative Statements:-
This type of statement will execute the statements in recursive manner in Specified No.of times after satisfied any condition. There are... It supports decision making system

i.                    while loop

ii.                 do...while loop

i.                    for loop

Conditional Control Statements:
                                       4 types
                        1. simple if
                        2. if..else
                        3. if..else if
                        4. Nested if
    1. Simple if
                        if(condition)
                        {
                                    statements;
                        }
Here the condition is TRUE then the statements are executed.Condition is either relational expression or Logical expression that returns TRUE(1) or FALSE (0)

2. if..else
if(condition)
{
statements -1
}
else
{
statements -2
}
This statement is used to control the execution of specified statement according to given condition. If Condition TRUE then “Statement-1” and will terminate to out side, other wise “Statement-2” and will terminate.


3. if.else if Ladar
It is useful to check more than one condition
                        if(condition 1)
                        {
                                    statements;
                        }
                        else if(condition 2)
                        {
                                    statements;
                        }
                        else if(condition 3)
                        {
                                    statements;
                        }
                        else
                        {
                                    statements;
                        }
  4. Nested if
               One if statement is used within another if statement
                        if(condition)
                        {
                                    if(condition)
                                    {
                                                statements;
                                    }
                        }


Un-Conditional Statement

This type of statements will execute the statements without checking any condition.
                        I.          Goto
                        Ii.         Break;
                        Iii.       Continue

Iterative Statement
This type of statement will execute the statements in iteratively in mentioned times after satisfied the condition of before satisfied the condition.

I.                    While loop
II.                 Do...while loop.
III.               For loop.

Iterative Statements (Loops)
Iterative statements are used to perform some set of operations, repeatedly. There are 3 types of loops
            1. While Loop
            2. Do while loop
            3. For loop  & nested for

            While Loop
Initialization;
while (condition)
            {
                        statements;
            increment;
}
This loop is executed, till the condition is true. When the condition becomes false, control will come out of loop. If there is only one loop statement, then enclosing { } is optional.

Do… While loop
initialization;
do
{
            statements;
increment;
}while (condition) ;  (Semi colon is must here)
This loop is executed like while loop, till the condition is true. When the condition is false, control will come out of loop.  But here, condition will be checked after each execution, at the end of loop. So, For the first time, Loop will be executed without bothering the condition. So do while loop will be executed, atleast once if the given condition false also.


For Loop

for(initialization; condition; increment or decrement)
            {                                      
                        statements;
            }
Loop is executed, till the condition is true. Each time, after executing the loop statements, control comes to thrid part of for loop, to increase or decrease the value of variable. Then it checks the condition for continuing the loop. First part is used for initializing variables, at the time of starting loop execution.
If there is only one loop statement, then enclosing parenthesis’s optional.

Switch statement
This Switch case statement is used to check multiple conditions at a same time. In here we can able to check only the result of any Expression. We cannot able to give the Expression as a    condition.
Syn:-
            switch(value (or) Variable)
            {
                        case 1:
                                    statements;
                                    break;
                        case 2:
                                    statements;
                                    break;
                        .
                        .
                        .
                        case value n:
                                    statements;
                                    break;
                        default:
                                    statements;
}


In the Above syntax's the switch of argument will access the value and then it will check that value is available in our case value are not. If that value is equal to any case value then, that corresponding statements will be executed. After Executes successfully it will terminate from the switch statement by the effect of break statement.
If at least one case also not matching, then the default case statements will be executed.

break:
This statement is used to transfer the control from currently executing inner-Most loop or inner-Statement into outer loop statement or statement

Syn:

       break;