JAVA

Conditional Statements In Java

Java - Conditional Statements In Java

Conditional Statements

Conditional statements are a process to check the condition and execute the particular statement. The conditional statement always returns logical value.

 

If Conditional Statement 

If-statement is a conditional statement that allows to check the condition and return true or false value. We check the condition with if keyword.

 

Flowchart:-

If case
Image Source:- https://www.javatpoint.com

 

Syntax:-
if (condition)
{
     statements;
       }
if (condition)
{
statements;
}

 

If statement
Image Source:- https://rephactor.com

 

Example:-
import java.util.scanner;
Class largest
{
     public static void main (String s[])
       {
           Scanner sc = new Scanner (System.in);
           System.out.print ("Enter first value");
           int a = sc.nextInt();
           System.out.print ("Enter second value");
           int b = sc.nextInt();
           if (a > b)
            {
                 System.out.print ("A is largest");
                    }
            if (b > a)
            {
                System.out.print (" B is largest");
                   }
              }
}

 

If-else Conditional Statement

If-else statement is a method to check the condition and execute the statement and the condition get true or false. Else is a keyword that allows executing the statement.

 

Flowchart:-

If else statements
Image Source:- http://1.bp.blogspot.com

 

Syntax:-
if (condition)
{
     statement;
       }
else {
      statement;
        }

 

no semicolon
Image Source:- http://sofia.cs.vt.edu
Example:-
import java.util.*;
Class A
{
    public static void main (String s[])
     {
         Scanner sc = new Scanner ( System.in); 
          System.out.print ("Enter first value");
           int a = sc.nextInt();
           System.out.print ("Enter second value");
           int b = sc.nextInt();
           if (a > b)
            {
                 System.out.print ("A is largest");
                    }
            if (b > a)
            {
                System.out.print (" B is largest");
                   }
              }
}

Ladder-If Conditional Statement

Ladder-If is a statement that allows executing more than one functions but it terminates the processing when the system get the true statement. We use if keyword to check the condition and execute the statements if the condition gets true otherwise switch to the if-else statement.

 

Flowchart:-

flow chart
Image Source:- http://3.bp.blogspot.com

 

Syntax:-
if (condition)
{
   statement;
     }
else if (condition)
{
   statement;
    }
else {
    statement;
    }

 

Example:- 
import java.util.*;
Class A
{
    public static void main ( String s[])
     {
         Scanner sc = new Scanner (System.in);
         System.out.print ("Enter value");
          int a = sc.nextInt();
         System.out.print ("Enter value");
         int b = sc.nextInt();
         if (a > b)
         {
             System.out.print ("A is largest");
               }
          else if (b > a)
           {
               System.out.print ("B is largest");
                 }
             else {
                  System.out.print (" Both are equal");
                  }
              }
}

 

Nested if Statement

Nested-if is a condition allows defining the conditions as nested form. The inner condition controlled by its outer condition.

 

Flowchart:-

conditional statements
Image Source:- https://www.tutorialgateway.org

 

Syntax:-
if (condition)
{
    if (condition)
     {
         statement;
           }
     else { 
        statement;
           }
    }
else {
      statement ;
    }

 

Example:-
import java.util.*;
Class A
{
    public static void main ( String s[])
     {
         Scanner sc = new Scanner (System.in);
         System.out.print (" Enter value ");
          int a = sc.nextInt();
          System.out.print (" Enter value");
           int b = sc.nextInt();
           if (a == 5)
            {
                if (b == 6)
                 {
                      System.out.print ( " welcome");
                        }
                   else {
                       System.out.print (" wrong value of b");
                           }
                      }
              else {
                 System.out.print (" wrong value of a");
                    }
}

 

 

Switch Statement

The switch statement is a statement that checks the condition and executes the particular instruction.

  1. Case: It is a keyword that checks the condition and executing the statement.
  2. Break: It is a keyword that allows terminating the switch processing.
  3. Default: It is used to execute the false statement of the switch.

 

Flowchart:-

Conditional statement
Image Source:- http://www.phpf1.com

 

Example:-
import java.util.*;
Class A
{
    public static void main (String s[])
     {
         Scanner sc = new Scanner (System.in);
          System.out.print ("enter value");
         int a = sc.nextInt();
          switch (a)
          {
               case 1:
              System.out.print ("one");
               break;
               case 2:
              System.out.print ("two");
               break;
               case 3:
              System.out.print ("three");
              break;
              default:
              System.out.print (" wrong choice ");
              }
      }

 

 

About the author

Udit Mutreja

14 Comments

Click here to post a comment