Home / JAVA / Conditional Statements In 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 Udit Mutreja

Check Also

Inheritance in Java

Inheritance in Java Inheritance is the feature of object-oriented programming concept for extending the data member …

14 comments

  1. Felicito, su pensamiento es brillante
    for the ears

  2. Willingly I accept. The theme is interesting, I will take part in the discussion.

  3. Thanks for giving your ideas with this blog. Likewise, a myth regarding the banking institutions intentions if talking about home foreclosure is that the loan company will not getreceive my installments. There is a specific amount of time which the bank can take payments here and there. If you are very deep inside the hole, they should commonly require that you pay the particular payment completely. However, that doesn’t mean that they will have any sort of payments at all. In case you and the standard bank can find a way to work one thing out, your foreclosure method may halt. However, in case you continue to pass up payments in the new program, the home foreclosure process can pick up exactly where it was left off.

  4. I am genuinely thankful to the owner of this website who has shared this enormous piece of writing at here.

  5. I’ll immediately grab your RSS feed as I can not find your email subscription link or newsletter service. Do you’ve any? Kindly let me know in order that I could subscribe. Thanks.

  6. Messaggi personali vengono inviati a tutti i oggi?

Leave a Reply

Your email address will not be published. Required fields are marked *