Friday 23 May 2014

If-else control statement

If is a selection statement which allows you to control the flow of  program's execution based upon condition known only during run time.

If
The if statement is java's conditional statement.It is used to perform task depending on whether the given condition is true or false.
Syntax
if(condition)
{
      //if block code
       statements
}
Here , If condition is true then statements inside block executed otherwise these statements skipped.

Flow chart of If





Let's see simple program of if statement
package org.modi;
public class Ifstatement
{
public static void main(String[] args )
{
int i=10;
if(i ==10)
{
System.out.println("This is if statement");
}
}
}

OUTPUT

This is if statement

If...else statement
If the condition is true then if block execute.Otherwise else block is execute.

Syntax
if(condition)
{
           //if block
}
else
{
          //else block
}
Here, if condition is true then if block executed otherwise else block executed.
Flow chart of If..else


Let's see simple program of if..else statement

package org.modi;
public class Test
{
public static void main(String[] args )
       {
          int i=10;
          if(i >10)
          {
              System.out.println("This is if statement");
          }
          else
          {
              System.out.println("This is else statement");
          }
       }
}
OUTPUT
This ia else statement

If the value of i is 10 then if block executed and it will print This is if statement.If values of  i other than 10 then else block executed and it prints This is else statement

Nested if...else
a nested if is an if statement within if or else statement.when we require to check more than one condition then we can use nested if..else.

Syntax
Form-1
if(condition 1)
      statement 1;
      if(condition 2)
        statement 2;
Form-2
if(condition 1)
     statement 1 ;
else
    else-statement;
    if(condition 2)
       statement 2;
Let's see simple program of  nested if..else statement

Form-1
package org.modi;
public class Test
{
public static void main(String[] args) 
{
int i=62;
if(i>18)
{
System.out.println("You are eligible for voting");
if(i>60)
{
System.out.println("You are Senoir citizen");
}
}
else
{
System.out.println("You are not eligible for voting");
}
}
}

OUTPUT
You are eligible for voting
You are Senoir citizen

Form-2
package org.modi;
public class Test
{
public static void main(String[] args) 
{
int temp=-2;
if(temp>10)
{
System.out.println("Hot day");
}
else
{
System.out.println("cool day");
if(temp<0 p="">
{
System.out.println("Water become ice");
}
}
}
}

OUTPUT
cool day
Water become ice

The If-else-if Ladder
Syntax
if(condition1)
    statement1;
else if(condition2)
   statement2;
else if(condition3)
   statement3;
.
.
.
.
else
    else statement;

Here,If condition 1 is true then statement 1 is executed.If condition 1 is false then condition 2 is tested.If condition 2 is true then statement 2 is executed,otherwise condition 3 is tested.If any conditions false then else statement executed. 

package org.modi;
public class IfLadder
{
public static void main(String[] args)
{
int i=85;
if(i>=90)
{
System.out.println("A Grade");
}
else if(i>=80)
{
System.out.println("B Grade");
}
else if(i>=70)
{
System.out.println("C Grade");
}
else if(i>=60)
{
System.out.println("D Grade");
}
else 
{
System.out.println("F Grade");
}
}

OUTPUT
B Grade

Important point:
1.If if block contains only one statement then it can be included without enclosing in curly brackets. 
2. == must be used for comparison in expression of condition.If we use = then it always return true because it's used for assignment not for comparison. 
E.g

int a=10;
if(a==10)
   System.out.println("a is equal to 10");


0 comments :

Post a Comment