Pages

Control Statements ,JAVA



Control statements are the statements which alter the flow of execution and provide better control to the programmer on the flow of execution. In Java control statements are categorized into selection control statements, iteration control statements and jump control statements. 
· Java’s Selection Statements: Java supports two selection statements: if and switch. These statements allow us to control the flow of program execution based on condition.
o if Statement: if statement performs a task depending on whether a condition is true or false.
Syntax:            if (condition)
                                     statement1;              else
                                     statement2;
Here, each statement may be a single statement or a compound statement enclosed in curly braces (that is, a block). The condition is any expression that returns a boolean value. The else clause is optional.


Program 1:  Write a program to find biggest of three numbers.
//Biggest of three numbers class BiggestNo
{        
    public static void main(String args[])
             {       
   int a=5,b=7,c=6;          
  if ( a> b && a>c)
System.out.println ("a is big");   
                         else if ( b > c)
                                     System.out.println ("b is big");
                         else
                                     System.out.println ("c is big");
             }
}
o Switch Statement: When there are several options and we have to choose only one option from the available ones, we can use switch statement. 
Syntax:            switch (expression)
                         {           case value1:     //statement sequence
                                                             break;
                                     case value2:     //statement sequence
                                                             break;               
                                      ………….…..
                                     case valueN: //statement sequence                                             break;
                                     default:            //default statement sequence
                         }
Here, depending on the value of the expression, a particular corresponding case will be executed.

Program 2:  Write a program for using the switch statement to execute a particular task depending on color value.
//To display a color name depending on color value class ColorDemo

{           public static void main(String args[])
             {          char color = ‘r’;              switch (color)
                         {          case ‘r’: System.out.println (“red”);     break;                case ‘g’: System.out.println (“green”);             break;                          case ‘b’: System.out.println (“blue”);     break;                          case ‘y’: System.out.println (“yellow”);       break;                          case ‘w’: System.out.println (“white”); break;
                                     default: System.out.println (“No Color Selected”);
                         }
             }
}
 · Java’s Iteration Statements: Java’s iteration statements are for, while and do-while. These statements are used to repeat same set of instructions specified number of times called loops. A loop repeatedly executes the same set of instructions until a termination condition is met.
o while Loop: while loop repeats a group of statements as long as condition is true. Once the condition is false, the loop is terminated. In while loop, the condition is tested first; if it is true, then only the statements are executed. while loop is called as entry control loop.
Syntax:            while (condition)
                         {
                                     statements;
                         }


Program 3: Write a program to generate numbers from 1 to 20.
//Program to generate numbers from 1 to 20. class Natural
{           public static void main(String args[])
             {          int i=1;               while (i <= 20)
                         {          System.out.print (i + “\t”);
                                     i++;
                         }
             }
} 

 o do…while Loop: do…while loop repeats a group of statements as long as condition is true. In do...while loop, the statements are executed first and then the condition is tested.
do…while loop is also called as exit control loop.
Syntax:            do
                         {
                                     statements;
                         } while (condition);

Program 4: Write a program to generate numbers from 1 to 20.
//Program to generate numbers from 1 to 20. class Natural
{           public static void main(String args[])
             {          int i=1;
                         do
                         {          System.out.print (i + “\t”);
                                     i++;
                         } while (i <= 20);
             }
}  

o for Loop: The for loop is also same as do…while or while loop, but it is more compact syntactically.  The for loop executes a group of statements as long as a condition is true.
Syntax:            for (expression1; expression2; expression3)
                         {                      statements;
                         }
Here, expression1 is used to initialize the variables, expression2 is used for condition checking and expression3 is used for increment or decrement variable value.

· Java’s Jump Statements: Java supports three jump statements: break, continue and return. These statements transfer control to another part of the program.
o break: 
Ø  break can be used inside a loop to come out of it. 
Ø  break can be used inside the switch block to come out of the switch block.
Ø  break can be used in nested blocks to go to the end of a block. Nested blocks represent a block written within another block. 
Syntax:     break;   (or)            
break label;//here label represents the name of the block.  

o continue: This statement is useful to continue the next repetition of a loop/ iteration. When continue is executed, subsequent statements inside the loop are not executed.
Syntax:            continue;

Program 7: Write a program to generate numbers from 1 to 20.
//Program to generate numbers from 1 to 20. class Natural
{           public static void main (String args[])
             {          int i=1;
                         while (true)
                         {          System.out.print (i + “\t”);
                                     i++;
                                     if (i <= 20 )
                                                 continue;                        else                                          break;
                         }
            
}

o return statement: 
return statement is useful to terminate a method and come back to the calling method.  
Ø  return statement in main method terminates the application. 
Ø  return statement can be used to return some value from a method to a calling method.
Syntax:                     
 return;    (or)           
 return value; // value may be of any type
  \

 Program 8: Write a program to demonstrate return statement.

            //Demonstrate return    class ReturnDemo
            {          public static void main(String args[])
                        {          boolean t = true;
                                    System.out.println (“Before the return”);
                                    if (t)
                                                return;
                                    System.out.println (“This won’t execute”);
                       }
            }



Note: goto statement is not available in java, because it leads to confusion and forms infinite loops.