Monday, 8 February 2016

if, ifelse and switch statements in JAVA (Flow Control in JAVA)

Flow control describes the order in which the statements should execute at run time. 
In JAVA we have below Flow control statements are available

v  if
v  if-else
v  switch
v  break
v  continue

if statement: If the condition is true then statements will execute which are mentioned in between open and closed curly braces.

    Syntax:

             if (condition) {
                   Statement 1;
                   Statement 2;
                         ….
                   Statement n;
             }



Important Points:
1.       Curly braces are not mandatory if there is only one statement present
2.       Condition should returns Boolean value 

if-else statement: If the condition is true then if block statements will execute otherwise else block statements will execute.

          Syntax:

                   if (condition) {
                   Statement 1;
                   Statement 2;
                         ….
                   Statement n;
                 }else {
                       Statement 1;
                       Statement 2;
                         ….
                      Statement n;
                   }



Important Points:
1.       Curly braces are not mandatory for both if and else if there is only one statement present
2.       Condition should returns Boolean value
  

switch statement: The switch is used for selecting one option from multiple options

          Syntax:
      
                   switch(value){
                        case value/expression :
                                       statements;
                                       break;
                               ……..

                       default:
                              statemets;
                    }   



Important Points:

1.       Curly braces are mandatory
2.       In switch value should be integral values like int , byte, short, long, char. In jdk 1.5 onwards wrapper classes are allowing
3.       For case statement break is not mandatory but stop executing other cases we should use break
4.       Default is also not mandatory. If some think should execute if there is no case was matched then we should keep default case


break statement: The break statement is used for breaking the execution of the block. So that control will come out from the block

continue statement: If we wants to skip the rest of the statements and start the execution from the first statement on wards we will go for continue statement.

Programs:

if:

         class IfStatmentDemo
   {
           public static void main(String[] args){
           int i=10;
           System.out.println("first statement");
           if(i==9)
            System.out.println("second statement");
    
           System.out.println("third statement");    
           }
}

if-else:

                class IfElseStatmentDemo
{
              public static void main(String[] args){
                     int i=10;
                     System.out.println("first statement");
                     if(i==9)     
                            System.out.println("second statement");
                     else
                           System.out.println("else statement");
                     System.out.println("third statement");
      
              }
}

Switch:

class SwitchDemo
{
              public static void main(String[] args){
                     int i=1;
                     System.out.println("first statement");
                     switch(i)
                     {
                            case 1: System.out.println("one");
                                    break;
                            case 2: System.out.println("two");
                                    break;
                           case 3: System.out.println("three");
                                    break;
                           case 4: System.out.println("four");
                                    break;                       
                           default:System.out.println("default");
      
                     }     
              }

}

No comments:

Post a Comment