Conditional control statement type one in java

Branching statement:

  • All the instructions are executed sequentially by default when no repetitions of some calculations are necessary.
  • In some situation, we may have to change the execution order of statements based on condition or to repeat a set of the statement until certain conditions are met.

Tyes of branching statements:

1. If statement
2. If – else statement
3. Nested if....else statement
4. If ....else ladder
5. Switch case

1. If statement:
  • It is used to check the condition and if true the condition then it executes.
Syntax:

 if (condition)
{
          True statement;
}

Example program for if:

package exampleprogram;

import java.util.Scanner;

public class ifcondition_program {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int a;

        System.out.println("Enter a value  : ");

        a = input.nextInt();

        if (a >= 5) {

            System.out.println("The entered number " + a + " is greater than 5");

        }

    }

}

Output:
if condition:
False case:
2. If – else statement:
  • It is used to check both conditions i.e. true and false.
  • It executes only one condition at a time.
Syntax:

if (condition)
    {
          True statement
     }
else
    {
          false statement;
     }

Example program for if-else:

package exampleprogram;

import java.util.Scanner;

public class ifcondition_program {

 

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int a;

        System.out.println("Enter a value  : ");

        a = input.nextInt();

        if (a >= 5) {

            System.out.println("The entered number " + a + " is greater than 5");

        } 

        else {

            System.out.println("The entered number " + a + " is less than 5");

        }

    }

}

Output:
if condition:
else condition:
3. Nested if....else statement:
  • Multiple if-else conditions are validating i.e. within condition check first after that execute.
Syntax:

if(condition 1)
   {
          if(condition 2)
              {
                   True statement2;
               }
          else
              {
                   False statement2;
               }
   }
else
    {
          False statement1;
     }

Example program for nested if...else:

package exampleprogram;

import java.util.Scanner;

public class ifcondition_program {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int a, b;

        System.out.println("Enter a result:\n 1.pass\n 2.fail\n press 1 or 2\n");

        a = input.nextInt();

        if (a == 1) {

            System.out.println("Enter total mark : ");

            b = input.nextInt();

            if (b > 450) {

                System.out.println("You can get first group i.e. maths group");

            } else {

                System.out.println("You can get all group except the first group");

            }

        } else {

            System.out.println("Passed student only can join the 11th standard");

        }

    }

}

Output:
if-if condition:
if-if-else case:
else condition:
4. The if...else ladder:
  • It is similar to nested if but it checks continually.
Syntax:

if(condition)
   {
          Statement1;
    }
else if(condition 2)
   {
          Statement2;
    }
else if(condition 3)
   {
          Statement3;
    }
else
   {
          Statement4;
   }

Example program for if...else ladder:

package exampleprogram;

import java.util.Scanner;

public class ifcondition_program {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int a, b, c;

        System.out.println("Enter 3 values : ");

        a = input.nextInt();

        b = input.nextInt();

        c = input.nextInt();

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

            System.out.println("\n Biggest value " + a + " is a");

        } else if (b > c) {

            System.out.println("\n Biggest value " + b + " is b");

        } else {

            System.out.println("\n Biggest value " + c + "  is c");

        }

    }

}

Output:
if condition:
else if  condition:
else condition:
5. Switch case:
It is used to execute a particular group of statements.
Syntax:

 switch(expression)
  {
          case condition 1:
          statement1;
          break;
          case condition 2:
          statement2;
          break;
          .........
          .........
          default
          statements
          break;
  }

Example program for switch case:

package exampleprogram;

import java.util.Scanner;

public class ifcondition_program {

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        int choice;

        System.out.println("Enter a choice 1 or 2 ");

        choice = input.nextInt();

        switch (choice) {

            case 1:

                System.out.println("\n You are chooses option 1 ");

                break;

            case 2:

                System.out.println("\n You are chooses option 2 ");

                break;

            default:

                System.out.println("\n You can only entered 1 or 2 choice ");

                break;

        }

    }

}

Output:
Case 1:
Case 2:
Default case:
Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me