Conditional Statements in Java – Branching in Java

About Branching in Java

Branching means decision making structures. Here conditions are tested for true or false. If condition is true some statements will be executed and if it is false some other statements will be executed 

In java the above situation can be handled by using following elements

A.            Conditional operator

B.            If else

C.            Switch

A. Conditional Operator

Conditional operator can be used to handle the branching concept.  If a condition is true some action will be done otherwise some other action will be done. It can be used to replace “if” in java.

Syntax of conditional operator:

(Condition) ? (Result1) : (Result2);

Notice the use and placement of the question mark and colon. In order to determine the value of the whole expression, initially condition is evaluated. If the value of condition is true, then the value of result 1 will be the actual result. If the value of condition is false, then result 2 will be the actual result.

Following things can be used for conditional operators

~             ==      equal to

~             !=      not equal to

~             >       greater than

~             >=      greater than or equal to

~             <       less than

~             <=      less than or equal to

Programming Example of conditional operator

Accept a number and check that is even or odd

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x,y;

               String  z;

               System.out.println(“Enter a Number”); 

               x=sc.nextInt(); 

               z=(x%2==0)? ” Even ” : ” Odd “;

               System.out.println(“Result is = “+z);

          }

}

Output

Enter a Number

56

Result is =  Even

Output

Enter a Number

57

Result is =  Odd

Accept a year and check that is leap year or not (Simple)

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x,y;

               String  z;

               System.out.println(“Enter Year”); 

               x=sc.nextInt(); 

               z=(x%4==0)? ” Leap Year ” : ” not Leap Year “;

               System.out.println(“Result is = “+z);

          }

}

Output

Enter Year

2017

Result is =  not Leap Year

Output

Enter Year

2020

Result is =  Leap Year

Accept a number and check it is divisible by a number or not

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x,y;

               String  z;

               System.out.println(“Enter a number”); 

               x=sc.nextInt(); 

               System.out.println(“Enter a number”); 

               y=sc.nextInt(); 

               z=(x%y==0)? ” Divisible ” : ” not Divisible “;

               System.out.println(“Result is = “+z);

          }

}

Output

Enter a number

45

Enter a number

23

Result is =  not Divisible

Output

Enter a number

45

Enter a number

9

Result is =  Divisible

Accept two numbers and check they are equal or not

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x,y;

               String  z;

               System.out.println(“Enter a number”); 

               x=sc.nextInt(); 

               System.out.println(“Enter a number”); 

               y=sc.nextInt(); 

               z=(x==y)? ” equal ” : ” not equal “;

               System.out.println(“Result is = “+z);

          }

}

Output

Enter a number

12

Enter a number

13

Result is =  not equal

Output

Enter a number

12

Enter a number

12

Result is =  equal

Accept two numbers and check which is greater              

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x,y;

               String  z;

               System.out.println(“Enter a number”); 

               x=sc.nextInt(); 

               System.out.println(“Enter a number”); 

               y=sc.nextInt(); 

               z=(x>y)? x+”is greater” : y+”is greater”;

               System.out.println(“Result is “+z);

          }

}

Output

Enter a number

23

Enter a number

15

Result is 23is greater

Output

Enter a number

56

Enter a number

90

Result is 90is greater

Accept age of a person and check his eligibility for voting

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x;

               String  z;

               System.out.println(“Enter age to test voting in India”); 

               x=sc.nextInt(); 

               z=(x>=18)? “eligible for voting” : ” Not eligible for voting”;

               System.out.println(“Result is “+z);

          }

}

Output

Enter age to test voting in India

12

Result is  Not eligible for voting

Output

Enter age to test voting in India

18

Result is eligible for voting

A.1  Conditional Operator – Multiple Conditions

accept 5 marks of a student and find the result depending on following conditions

  if total mark >= 400 grade is A

  if total mark >= 300 grade is B

  if total mark >= 200 grade is C

  else if total mark < 200 grade is D

import java.util.Scanner; 

class student

 public static void main(String args[])

  { 

   int a,b,c,d,e,tot;

   Scanner scc=new Scanner(System.in); 

   System.out.println(“Enter english mark”); 

   a=scc.nextInt();

   System.out.println(“Enter hindi mark”); 

   b=scc.nextInt();

   System.out.println(“Enter odia mark”); 

   c=scc.nextInt();

   System.out.println(“Enter telugu mark”); 

   d=scc.nextInt();

   System.out.println(“Enter tamil mark”); 

   e=scc.nextInt();

   tot = a+b+c+d+e;

   System.out.println(“The total mark is  “+tot);

   String gd=(tot>=400)?”A” :(tot>=300)? “B” :(tot>=200) ? “C” :”D”;

   System.out.println(“The grade is  “+gd);

   scc.close(); 

 } 

}  

Output

Enter english mark

50

Enter hindi mark

60

Enter odia mark

70

Enter telugu mark

80

Enter tamil mark

90

The total mark is  350

The grade is  B

Accept a year and check leap year or not

import java.util.Scanner;

public class student

{

    public static void main(String args[])

    {

        String msg;

        Scanner sc = new Scanner(System.in);

        System.out.println(“Enter a year”);

        int n = sc.nextInt();

 msg=(((n%4==0)&&(n%100!=0))||(n%400==0)) ? n+” is a Leap Year”:    n +” is Not a Leap Year”;

        System.out.println(msg);

    }

}

Output

Enter a year

2200

2200 is Not a Leap Year

Output

Enter a year

1900

1900 is Not a Leap Year

Output

Enter a year

2020

2020 is a Leap Year

Accept three numbers and find the greatest one

import java.util.Scanner;

public class student

{

    public static void main(String args[])

    {

        String msg;

        int a,b,c;

        Scanner sc = new Scanner(System.in);

        System.out.println(“Enter three numbers”);

        a = sc.nextInt();

        b = sc.nextInt();

        c = sc.nextInt();

        msg=((a>b)&&(a>c))? a+” is greater”:((b>c)&&(b>a)) ? b+” is greater” :c+” is greater”;

        System.out.println(msg);

    }

}

Output

Enter three numbers

11

33

22

33 is greater

Output

Enter three numbers

77

66

55

77 is greater

Output

Enter three numbers

11

22

34

34 is greater

B. If else

“If else” can be used to handle the branching concept If a condition is true some action will be evaluated otherwise some other action will be evaluated. It can be used to replace conditional operator in java.

Syntax:

 if (condition)

{

                Actions 1;

}

else

{

Actions 2;

}

In order to determine the value of the whole expression, initially condition is evaluated. If the value of condition is true, then the value of Action 1 will be the actual result. If the value of condition is false, then Action 2 will be the actual result.

Following things can be used for if

==      equal to

!=      not equal to

>       greater than

>=      greater than or equal to

<       less than

<=      less than or equal to

Programming Examples of if

Accept a number and check that is even or odd

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x,y;

               System.out.println(“Enter a number”); 

               x=sc.nextInt(); 

               if(x%2==0)

               System.out.println(” even “);

                 else

               System.out.println(” odd “);

          }

}

Output

Enter a number

56

 even

Output

Enter a number

57

 odd

Accept a year and check that is leap year or not (Simple)

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x,y;

               System.out.println(“Enter Year”); 

               x=sc.nextInt(); 

              if(x%4==0)

               System.out.println(” Leap Year “);

                 else

               System.out.println(” Not Leap Year”);

            }

}

Output

Enter Year

2018

 Not Leap Year

Output

Enter Year

2020

 Leap Year

Accept a year and check that is leap year or not (Real Logic)

import java.util.Scanner;

public class student

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.println(“Enter a year”);

        int n = sc.nextInt();

        if(((n%4==0)&&(n%100!=0))||(n%400==0))

        {

           System.out.println(n +” is a Leap Year”);

        }

        else

        {

           System.out.println(n +” is Not a Leap Year”);

        }

    }

}

Output

Enter a year

2018

2018 is Not a Leap Year

Output

Enter a year

2020

2020 is a Leap Year

Accept a number and check it is divisible by a number or not

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x,y;

               System.out.println(“Enter a number”); 

               x=sc.nextInt(); 

               System.out.println(“Enter a number”); 

               y=sc.nextInt(); 

               if (x%y==0)

  System.out.println( ” Divisible “);

  else

 System.out.println( ” not Divisible “);

              }

}

Output

Enter a number

34

Enter a number

2

 Divisible

Output

Enter a number

35

Enter a number

6

 not Divisible

Accept two numbers and check they are equal or not

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x,y;

               String  z;

               System.out.println(“Enter a number”); 

               x=sc.nextInt(); 

               System.out.println(“Enter a number”); 

               y=sc.nextInt(); 

               if (x==y)

System.out.println (” equal “);

else

System.out.println( ” not equal “);

          }

}

Output

Enter a number

12

Enter a number

23

 not equal

Output

Enter a number

12

Enter a number

12

 equal

Accept two numbers and check which is greater              

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x,y;

               System.out.println(“Enter a number”); 

               x=sc.nextInt(); 

               System.out.println(“Enter a number”); 

               y=sc.nextInt(); 

               if(x>y)

               System.out.println( x+”is greater”);

               else

               System.out.println (y+”is greater”);

          }

}

Output

Enter a number

12

Enter a number

45

45is greater

Output

Enter a number

90

Enter a number

12

90is greater

Accept age of a person and check his eligibility for voting

import java.util.Scanner; 

public class student

{

        public static void main(String []args)

           {  

               Scanner sc=new Scanner(System.in); 

               int  x;

               System.out.println(“Enter age to test voting in India”); 

               x=sc.nextInt(); 

               if(x>=18)

               System.out.println(“eligible for voting”);

               else

               System.out.println( ” Not eligible for voting”);

          }

}

Output

Enter age to test voting in India

12

 Not eligible for voting

Output

Enter age to test voting in India

25

eligible for voting

B.1.  If else if else

accept 5 marks of a student and find the result depending on following conditions

  if total mark >= 400 grade is A

  if total mark >= 300 grade is B

  if total mark >= 200 grade is C

  else if total mark < 200 grade is D

 import java.util.Scanner; 

class student

 public static void main(String args[])

  { 

   int a,b,c,d,e,tot;

   String gd;

   Scanner sc=new Scanner(System.in); 

   System.out.println(“Enter english mark”); 

   a=sc.nextInt();

   System.out.println(“Enter hindi mark”); 

   b=sc.nextInt();

   System.out.println(“Enter odia mark”); 

   c=sc.nextInt();

   System.out.println(“Enter telugu mark”); 

   d=sc.nextInt();

   System.out.println(“Enter tamil mark”); 

   e=sc.nextInt();

   tot = a+b+c+d+e;

   System.out.println(“The total mark is  “+tot);

   if (tot>=400)

   gd=”A”;

   else

   if(tot>=300)

   gd=”B”;

   else

   if(tot>=200)

   gd=”C”;

   else

   gd=”D”;

   System.out.println(“The grade is  “+gd);

   sc.close(); 

 } 

}  

Output

Enter english mark

50

Enter hindi mark

60

Enter odia mark

70

Enter telugu mark

80

Enter tamil mark

90

The total mark is  350

The grade is  B

Output

Enter english mark

98

Enter hindi mark

87

Enter odia mark

99

Enter telugu mark

79

Enter tamil mark

98

The total mark is  461

The grade is  A

Accept a year and check leap year or not

import java.util.Scanner;

public class student

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.println(“Enter a year”);

        int n = sc.nextInt();

        if (n%400 == 0)

        System.out.println(n +” is a Leap Year”);

        else

        if ((n%100 != 0)&&( n%4 == 0 ))

        System.out.println(n +” is a Leap Year”);

        else

        System.out.println(n +” is Not a Leap Year”);

    }

}

Output

Enter a year

2018

2018 is Not a Leap Year

Output

Enter a year

2020

2020 is a Leap Year

Output

Enter a year

1900

1900 is Not a Leap Year

Accept three numbers and find the greatest one

import java.util.Scanner;

public class student

{

    public static void main(String args[])

    {

        String msg;

        int a,b,c;

        Scanner sc = new Scanner(System.in);

        System.out.println(“Enter three numbers”);

        a = sc.nextInt();

        b = sc.nextInt();

        c = sc.nextInt();

        if((a>b)&&(a>c))

        msg=a+” is greater”;

        else

        if((b>c)&&(b>a))

        msg=b+” is greater” ;

        else

        msg=c+” is greater”;

        System.out.println(msg);

    }

}

Output

Enter three numbers

45

67

89

89 is greater

Output

Enter three numbers

56

90

23

90 is greater

C. Switch Case in Java

We can test a variable for equality against a list of values by using if. If this type of ‘if’ is handled it is called nested if. These things can be handled in switch case. It means switch case statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.

But we have to remember following points

  • You can have any number of case statements within a switch.
  • Break is mandatory in switch statement as control flows automatically to the end.
  • The switch statement terminates when a break statement is reached.
  • The value for a case must be a constant.
  •  “Default” is option in switch statement

Programming Exercises (Switch Case)

Accept the day number and find the day name

import java.util.Scanner;

class student

{

public static void main(String args[])

{

    Scanner sc=new Scanner(System.in);

    System.out.println(“1 for Monday”);

    System.out.println(“2 for Tuesday”);

    System.out.println(“3 for Wednesday”);

    System.out.println(“4 for Thursday”);

    System.out.println(“5 for Friday”);

    System.out.println(“6 for Saturday”);

    System.out.println(“7 for Sunday “);

    System.out.println(“Enter Your Choice”);

    int ch=sc.nextInt();

        switch(ch)

        {

            case 1:

            System.out.println(“monday”);

            break;

            case 2:

            System.out.println(“tuesday”);

            break;

            case 3:

            System.out.println(“wednesday”);

            break;

            case 4:

            System.out.println(“thursday”);

            break;

            case 5:

            System.out.println(“friday”);

            break;

            case 6:

            System.out.println(“saturday”);

            break;

            case 7:

            System.out.println(“sunday”);

            break;

            default:   

            System.out.println(“wrong choice”);

            break;

        }

}

}

Output

1 for Monday

2 for Tuesday

3 for Wednesday

4 for Thursday

5 for Friday

6 for Saturday

7 for Sunday

Enter Your Choice

4

thursday

Accept the month number and find the month name

import java.util.Scanner;

class student

{

    public static void main(String args[])

    {

        Scanner sc=new Scanner(System.in);

        System.out.println(“1=January”);

        System.out.println(“2=February”);

        System.out.println(“3=March”);

        System.out.println(“4=April”);

        System.out.println(“5=May”);

        System.out.println(“6=June”);

        System.out.println(“7=July”);

        System.out.println(“8=August”);

        System.out.println(“9=September”);

        System.out.println(“10=October”);

        System.out.println(“11=November”);

        System.out.println(“12=December”);

        int ch=sc.nextInt();

        switch(ch)

        {

            case 1:

            System.out.println(“January”);

            break;

            case 2:

            System.out.println(“February”);

            break;

            case 3:

            System.out.println(“March”);

            break;

            case 4:

            System.out.println(“April”);

            break;

            case 5:

            System.out.println(“May”);

            break;

            case 6:

            System.out.println(“June”);

            break;

            case 7:

            System.out.println(“July”);

            break;

            case 8:

            System.out.println(“August”);

            break;

            case 9:

            System.out.println(“September”);

            break;

            case 10:

            System.out.println(“October”);

            break;

            case 11:

            System.out.println(“November”);

            break;

            case 12:

            System.out.println(“December”);

            break;

            default:

            System.out.println(“Wrong Choice”);

            break;

        }

}

}

Output

1=January

2=February

3=March

4=April

5=May

6=June

7=July

8=August

9=September

10=October

11=November

12=December

4

April

Accept two numbers and do the arithmetic operation depending on the choice. (Number 1,2,3,4)

import java.util.Scanner;

class student

{

    public static void main(String args[])

    {

        int a,b;

        Scanner sc=new Scanner(System.in);

        System.out.println(“enter two numbers”);

        a=sc.nextInt();

        b=sc.nextInt();

        System.out.println(“1.addition”);

        System.out.println(“2.subtraction”);

        System.out.println(“3.multiplication”);

        System.out.println(“4.division”);

        System.out.println(“5.remainder”);

        System.out.println(“Enter Your Choice”);

        int ch=sc.nextInt();

        switch(ch)

        {

            case 1:     

            System.out.println(“addition is”+(a+b));

            break;

            case 2:     

            System.out.println(“subtraction is”+(a-b));

            break;

            case 3:     

            System.out.println(“multiplication is”+(a*b));

            break;

            case 4:     

            System.out.println(“division is”+(a/b));

            break;

            case 5:     

            System.out.println(“remainder  is”+(a%b));

            break;

            default:   

            System.out.println(“wrong choice”);

            break;

        }

    }

}

Output

enter two numbers

30

70

1.addition

2.subtraction

3.multiplication

4.division

5.remainder

Enter Your Choice

3

multiplication is2100

Accept a month number and print its number of days

import java.util.Scanner; 

class student

{

    public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.println(“Enter The Month Number”);

        int choice = sc.nextInt();

        switch(choice)

        {

        case 1:

        System.out.println(“Days = 31”);

        break;

        case 2:

        System.out.println(“Days = 28 or 29 Depends on Leap Year”);

        break;

        case 3:

        System.out.println(“Days = 31”);

        break;

        case 4:

        System.out.println(“Days = 30”);

        break;

        case 5:

        System.out.println(“Days = 31”);

        break;

        case 6:

        System.out.println(“Days = 30”);

        break;

        case 7:

        System.out.println(“Days = 31”);

        break;

        case 8:

        System.out.println(“Days = 31”);

        break;

        case 9:

        System.out.println(“Days = 30”);

        break;

        case 10:

        System.out.println(“Days = 31”);

        break;

        case 11:

        System.out.println(“Days = 30”);

        break;

        case 12:

        System.out.println(“Days = 31”);

        break;

        default : System.out.println(“Wrong Choice !!! “);

       }

 sc.close(); 

}

}

Output

Enter The Month Number

2

Days = 28 or 29 Depends on Leap Year

Output

Enter The Month Number

12

Days = 31

Output

Enter The Month Number

15

Wrong Choice !!!

Accept a month number and print its number of days (Another Way)

import java.util.Scanner; 

class student

{

public static void main(String args[])

    {

        Scanner sc = new Scanner(System.in);

        System.out.println(“Enter The Month Number”);

        int choice = sc.nextInt();

        switch(choice)

        {

            case 1:

            case 3:

            case 5:

            case 7:

            case 8:

            case 10:

            case 12:

            System.out.println(“Days = 31”);

            break;

            case 4:

            case 6:

            case 9:

            case 11:

            System.out.println(“Days = 30”);

            break;

            case 2:

            System.out.println(“Days = 28 or 29 Depends on Leap Year”);

            break;

            default:

            System.out.println(“Wrong Choice !!! “);

        }

 sc.close(); 

}

}

Output

Enter The Month Number

2

Days = 28 or 29 Depends on Leap Year

Enter The Month Number

12

Days = 31

Enter The Month Number

5

Days = 31

Enter The Month Number

10

Days = 31

Enter The Month Number

11

Days = 30

Enter The Month Number

15

Wrong Choice !!!

 

Leave a Comment

Scroll to Top