Control flow statements (if statement, & switch statement) in java!!!

Control  flow statements

In JAVA language there are several keywords, that are used to decide the flow of the program. when the program runs the statement are executed from top of the source to the bottom. one by one this flow can be altered by specific keywords, statement can be executed multiple time. some statement are called conditional  statement, they are executed only if a specific condition is met. 

Selection Statement :-

It calls the program control to be transfer to a specific flow based upon whether a certain condition is true or not. this are called as conditional statement they are two types of selection statement in JAVA.

They are :-
  1. if statements.                                                                               
  2. switch statement.
1 )- if statement :-

if statement consist of a Boolean statement expression followed by one or more statement in this type of statement, if expression is true, then respective block of code is executed.

Syntax :-

if(expression)
{
     statement;
}

ex :-

class test1{
public static void main(String args[]){

int a = 10;
  if(a>=0)
  {
   System.out.println("given no. is +ve");
  }
}

2 )- if .....else statement :-

In this type of statement, group of statement are executed when the expression are true. if the expression is false the else statements are executed.

The else is an optional statement.

Syntax :-

if(expression)
{
      statement;
}
else
{
       statement;
}

ex :-

WAP to check the given no. is +ve or -ve.

class test2
{
     public static void main(String args[])
     {
           int a = 10;
           if(a>0)
           {
                   System.out.println("given no. is +ve:");
           }
           else
           {
                  System.out.println("given no. is -ve");
            }
      }
}



3 )- if.....else if  statement :-

if statement can be followed by an optional else if.....else statement which is very use full to test various condition using sing if.....else if statement. while using if....else if...else statement.

There are few points to keep in mind.

1 )- if can have 0 or 1 else statement, it must come after any else if condition.

2 )- if can have 0 or many else if statement, if should be before else statement.

3 )- once an else if succeed none of the remaining else statement or else if will be tested

Syntax :-

if (expression)
{
     statement;
}
else if (expression)
{
     statement;
}
else if (expression)
{
     statement;
}
else
{
     statement;
}

ex :-

WAP to display the the grades.

 class test3
 {  
  public static void main(String args[]) {  
    int marks=65;  
      
    if(marks<50)
   {  
        System.out.println("fail");  
    }  
    else if(marks>=50 && marks<60)
     {  
        System.out.println("D grade");  
     }  
    else if(marks>=60 && marks<70)
    {  
        System.out.println("C grade");  
    }  
    else if(marks>=70 && marks<80)
    {  
        System.out.println("B grade");  
    }  
    else if(marks>=80 && marks<90)
   {  
        System.out.println("A grade");  
    } 
else if(marks>=90 && marks<100)
{  
        System.out.println("A+ grade");  
    }
     Else
    {  
        System.out.println("Invalid!");  
    }  
}  

4 )- nested if else statement :-

It is always legal nest if else statement, which means you can use one if or else if statement inside another if or else if statement.

Syntax :-

if (expression)
{
       if (expression)
      {
          statement;
      }

      else
     {
        statement;
     }
else
{
     if(expression)
    {
             statement;
     }
}


ex :-

WAP to find out the largest from the given three numbers.

class test4
{
    public static void main(string args[])
    {
           int a= 10; b=5; c= 30;
           if(a>b)
           {
                if (a>c)
                {
                         System.out.println("a is greater :"):
                 }
              else
              {
                         Sysem.out.println("c is greater :");
              }
         }
       else
       {
              if(b>c)
              {
                    System.out.println("b is greater :");
               }
        System.out.println("c is greater :");
     }
  }
}


1 )- switch statement :-

The switch statement is JAVA multi way branch statement, it provides an easy way to dispatch. execution to different part of your code based on value of an expression. it provide a better alternative of a large series of else if statement. 

Syntax :-

switch (expression)
{
    case constant expression :
    {
     statement;
    break;
   }
  case constant expression :
 {
     statement;
     break;
 }
  default :
 {
    statement;
 }
}


ex :-


public class test 
{  
public static void main(String args[] )
 {  
    int number=20;  
    switch(number)
    {  
    case 1: System.out.println("10");
                  break;  
    case 2: System.out.println("20"); 
                  break;  
    case 3: System.out.println("30");
                 break;  
    default: System.out.println("Not in 10, 20 or 30");  
    }  
}  



   









Post a Comment

2 Comments