Wednesday 12 October 2011

Control Statement (Decision and Loop)


Control statements are of these two types:
1. Conditional branching (decision making)
2. Looping

Conditional branching , a program is decide whether or not to execute the next statement based on the resultant value of the current expression.

Looping , the program performs a set of steps or operations repeatedly till a given condition is obtained.

Decision making and branching

following are the conditional statement.

1.           if statement
2.           switch statement

These statements are considered as decision – making statements.

These statements are also ‘ control ‘ the flow of execution , so they considered as control statements.

Explanation about above control structure is as follows


Decision making with if statement

The if statement is a powerful decision – making statement. It can implemented with using different forms such as:

1.     Simple if statement
2.     if.....else statement
3.     Nested if...else Statement.
4.     else if ladder.

1.Simple if statement :
          
     The if statement is a control statement that tests a particular condition.

The if keyword is used to perform the basic conditional test that evaluates a given expression for a boolean value true or false.

The syntax of if statement is as under.

           
            if(test Condition)
            {
                Statement block
                   -
                   -
                   -
            }
                   Statement x;

According to the above syntax, The ‘Statement block’ may be a single statement or a multiple statements.  All contained within the braces , each ending with semi colon.

If the test condition is true the statement block will be execute. Otherwise the statement block will be skipped & the execution will jump to the statement x. The test expression must be enclosed within parentheses ().

  2. if-else

     The if statement is evaluates the statement when condition  is true but it can not provide any way when condition is false. So that if…else statement is used.

The syntax of if…else Statement is as under.

                   if (test expression)
                    {
                             True Block statement(s);
                    }
                   else
                    {
                             False block statement(s);
                    }
                             Statement-x

According to the above syntax the else keyword is used with if to specify conditional branching. if the expression is true, then true-block statement(s) immediately following the if statement are executed otherwise the false-block statement(s) are executed and  after control moves to execute the statement-x after the if—else block.

For Ex.
            int a,b;
           
            if(a<b)
                 a=0;
            else
                 b=0;
            According to above example if a is greater than b than a is equal to Zero otherwise b is equal to Zero.

3. Nested if…else Statement :
           
When a series of decisions are involved, there is a need to use more than one if…else statement,  This is called Nesting of if statement.

The syntax of nested if is as under :

            if (test condition1)
             {
                   if (test condition2)
                    {
                             Statement-1;
                    }
                   else
                    {
                             Statement-2;
                   }
            }
            else
             {
                   Statement-3;
            }
            Statement-x;
            
          According to the above syntax if the test condition1 is true, it continue to perform second test. If the test condition 2 is true, The statement 1 will be evaluated, otherwise statement 2 will be evaluated. If the test condition 1 is false, the statement 3 will be evaluated and then control is transferred to statement x.

4.else if ladder :
           
There is a another way of putting if together when multiple decision are involved. 
The syntax of else  if ladder is as under :

            if (condition 1)
                   Statement-1;
            else if (condition-2)
                   Statement-2;
            else if (condition-3)
                   Statement-3;
            else if (condition-n)
                   Statement-n;
            else
                   Default statement;
            Statement-x;
    
This construction is known as else if ladder. The condition are evaluated from the top to down words as soon as a true condition  is found, the statement associated with it is executed & control is transferred to statement x. When all the n condition becomes false the else containing the default statement will be executed.

Switch Statement

The Switch Statement is Java’s multiway branch Statement. It provides a better alternative than a large series of if-else-if. The syntax of switch statement  is as under :

switch(expression)
{
       case value 1;
       //statement sequence
       break;
      
       case value 2;
       //statement sequence
       break;
           .
           .
           .
      
       case value N;
       //statement sequence
       break;
      
       default:
       //default statement sequence
 }
           
According to above syntax. The expression must be of type,byte,short,int or char. The values specified in the case statement must be of a type compatible with the expression. Each case value must be a unique literal. Duplicate case values are not allowed The break statement is used inside the switch to terminate a statement sequence The values of expression is compared with each of the literal values in the case statement. If a match is found, the code sequence following that case statement is executed. If none of the constant matches the values of the expression then the default statement is optional. If no case matches and no default is present, then no further, action is takes.
           


For Ex.
                   char ch=’c’;
          switch(ch)
           {
               case 'a':
               System.Out.println(”Good Morning”);
               break;
                   
               case 'b':
               System.Out.println (”Good Afternoon”);
               break;

               case 'c':
               System.Out.println (”Good Night”);
               break;
                            
               default:
               System.Out.println (”Good Day”);
            }

Looping Statement

(1)     while loop
(2)     do-----while loop
(3)     for loop
           
(1)                 while loop
           
While repeats a statement or block while condition is true.It is the entry controlled loop.

The syntax of while loop is  as under :

            while (condition)
              {
                   Body of loop
              }
           
According to above syntax ,the condition can be any boolean expression. The body of the loop will be executed as long as the conditional expression is true when condition becomes false control passes the next line of code immediately following the loop.

                   For Ex.
                             int n=10;
                             while(n>0)
                             {
                                       System.out.println(“Number”+n);
                                      n--;
                             }
                   As shown in  above example the loop will execute until the value of n is greater than 0

(2)                 do-while loop
     Sometimes it is desirable to execute the body of a while loop at least once even if the conditional expression is false to begin with to test the termination expression at the end of the loop rather than at the beginning.

     The do while loop always executes its body at least once because its conditional expression is at the bottom of the loop.It is called exit controlled loop.
     The syntax of do…while loop  is as under :
                  
            do
             {
                   Body of loop
            }
            while(condition);
           
According to above syntax ,do-while loop first execute the body of the loop and then evaluates the conditional expression. If this expression is true the loop will repeat otherwise the loop terminates Condition must be a Boolean expression.
Ex.
                             int i=1;
              do
              {
                 Syntax.out.println(Number:-“+i);
                 i++;
              }
              while(i<5);
            
As shown in above example first loop will execute and then check condition until the i is less than 5.

(3)                 For loop
           
The for loop contruct is used to execute a set of statements for a given no of times. Thus it is a shorthand method for executing statement in a loop. It is the entry controlled loop.

The syntax of for loop is as under :

for(initialization;test condition;increment or decrement)
                   {
                             body of loop
                   }

According to the above syntax , initialization is the initial value of the loop counter initialization of the control variable is done first, using assignment statement. The test Condition is the boolean expression. It tests the loop control variable against a target value. It the condition is true then the body of the loop is executed. If it is false the loop terminates. Increment or decrement is specifies the increasing or decreasing value of loop counter by a specified number.            
The for loop first evaluating the conditional expression then executing the body of the loop and the executing the iteration expression with each pass.

For Ex.
                   for(i=0;i<10;i++)
           {
             System.out.println(“Number:-“+i);
           }
           

break
The break statement is used inside loop to terminate the loop,bypassing the conditional expression and any remaining code in the body of the loop.

When a break statement is encounterd inside a loop the loop is terminated and program control resumes at the next statement as shown below example.

            for(int i=0,i<100;i++)
        {
         if(i==10)
         break;
         System.out.println(“i:-“+i);
        }
        System.out.println(“Loop Complete”);

continue
           
Sometimes need to continue running the loop but stop processing the remainder of the code in its body continue statement is used.

In while and do-while loops a continue statement caused control to be transferred directly to be conditional expression that controls the loop in a for loop control goes first to the iteration portion of the for statement and then to the conditional expression.

            For Ex.
                   for(int i=0; i<10;i++)
          {
           System.out.println (i+” “);
           if(i%2 ==0)
           continue;
           System.out.println (” “);
          }

Posted by: Ruchita Pandya

2 comments:

  1. Looping , the program performs a set of steps or operations repeatedly till a given condition is obtained.thanks for your easy undersatnding about the concepts.java training in chennai | java training in velachery

    ReplyDelete
  2. Loop control statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false.Very informative ..i suggest this blog to my friends..Thank you for sharingjava training in chennai | chennai's no.1 java training in chennai | best java institute in chennai

    ReplyDelete