Google+

54. 'if' selection statements






'if' selection statements are used for the below reason -

If you want any statement to be executed only when the condition passes. Then we have to go with if selection statements.  i.e. Lets say there is a software at park entrance, which says -> for children below 10 years they charge entrace ticket at Rs 50 and for others its free. So in this case we can use if selection statement, when age less than 10 years is true, Rs 50 should be assigned to the ticket price. if selection statement is one of the 'Control Flow' Statements.

Program to demonstrate the if condition statement when the condition provided to it results true            

class IfConditionTrue
{
   public static void main(String args[])
   {
      int a=3,b=4;

           if(a<b)    // The statements inside the if block will get executed only if the condition is true i.e. if(a<b) -> if(3<4) -> if(true)
           {
                System.out.println("You are Lucky");
            }

   }
 }

Output of this Program:

You are Lucky

As the result of the condition provide in the if statement i.e. a<b is true, the if statement will execute all the statements provided in the if block, so it executed System.out.println("You are lucky"); statement.

Program to demonstrate the if condition statement when the condition provided to it results false

class IfConditionFalse
{
   public static void main(String args[])
   {
      int a=4,b=3;

            if(a<b)  // The statement inside the if block wont get executed if the condition is false i.e. if(a<b) -> if(4<3) -> if(false)
           {
               System.out.println("You are lucky");
            }

    }
 }

Output of this program:  (No Output)

The program runs without any errors but nothing will get printed as the condition provided in the if statement i.e. a<b results false as a result, all the statements inside the if statement block wont get executed. Hence in this program nothing got printed.

Program to compare two variables using if statement and print the comparison result

class IfConditionCompare
{
   public static void main(String args[])
   {
      int x=4,y=4;

       if(x<y)
       {
         System.out.println("x is less than y");
       }

       if(x>y)
     {
        System.out.println("x is greater than y");
       }

       if(x==y)
      {
        System.out.println("x is equal to y");
       }

   }
 }

Output of this program:

x is equal to y 

If.....else... Statement  (program demonstration)

/* As you have seen in the above programs, that the statements inside the if block wont execute if the conditions fails, and hence the program wont print anything. But we can add addition else block to the if block, so that the program can execute the statements inside the else block if the if block condition fails. */

class IfElse
{
   public static void main(String args[])
   {
      int x=4,y=3;

       if(x<y)
       {
        System.out.println("x is less than y");
       }
       else                                         // else block will get executed when the if condition fails
       {
         System.out.println("x is greater than y");
       }
   }
 }

Output of this program:

x is greater than y

Since the if condition failed, the statement inside the else block got executed.

If statements  can be written without blocks i.e. without { and } flower braces if there is a single statement inside the block -  (Program  Demonstration)

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

       int a=2,b=2;

       if(a==b)
           System.out.println("a is equal to b");
       else
           System.out.println("a is not equal to b");

    }
 }

Output of this program:

a is equal to b

Observe that this program still works if we wont provide any flower braces to the if..else statements. Any how this is only possible if we have a single statement inside the if or else blocks.

Nested Ifs - (Program Demonstration)

 if...else...statements inside another if..else..statements will result in nested Ifs.

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

        int a=3,b=4,c=5,d=3;

        if(a==d)
        {
           System.out.println("We are inside the Outer 'if' block");

              if(b>c)
                 System.out.println(" I know this text in the inner 'if' block is not going to be printed on execution as the condition of this inner 'if' block results fail");
              else
                 System.out.println("We are inside the inner 'else' block");
        }
        else
           System.out.println("I know this text in the outer 'else' block is not going to be printed on execution as the condition of the inner 'if' block results pass");

    }
 }

Output of this program:

We are inside the Outer 'if' block 
We are inside the inner 'else' block

 if-else-if Ladder (Program demonstration)

As you can see in our previous program where we have executed the 'if' block if the condition results true and executing the 'else' block without any condition. But in this if-else-if ladder, we will add condition to the 'else' part until all our conditions gets finished.

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

        int a=3,b=4,c=5;

        if(a==4)
           System.out.println("This will get printed if the a==4 condition resutls ture, else it will check for the next condition in the else-if part");
        else if(b==5)
          System.out.println("This will get printed if the b==5 condition results true, else it will check for the next condition in the else-if part");
       else if(c==6)
           System.out.println("This will get printed if the c==6 condition results true, else it will check for the next condition in the else-if part");
         else
           System.out.println("All the above conditions failed, hence this statement got executed and printed");
    }
 }

Output of this program:

All the above conditions failed, hence this statement got executed and printed

Lets go through the program once:

  1. started with if(a==4) i..e. 3==4 i.e. false, hence the statement inside the 'if' block didn't execute. (The program would have ended if  if(a==4) resulted true)
  2. Next it executed else if(b==5) i.e. b==5 -> 4==5 -> false. Hence the statement inside else if(b==5) didn't execute. (The program would have ended if else if(b==5) resulted true)
  3. Next it executed else if(c==6) i.e. c==6 -> 5==6 -> false. Hence the statement inside else if(c==6) didn't execute (The program would have ended if else if(c==6) resulted true)
  4. Next it executed 'else' block and printed "All the above conditions failed, hence this statement got executed and printed"  




Please comment below to feedback or ask questions.

How to use 'Switch' Selection Statements will be explained in the next post.




No comments: