Monday, June 20, 2011

JAVA Programming : If Statement

JAVA If Statement


The If statement may be defined as a decision making statement, a control flow statement or a selection statement. In a simple program the execution of code starts form first line and ends at the last line but what if we want to execute a block of code only under a certain circumstances. The if statement is the simplest decision making statement. An If statement evaluates a Boolean expression (true or false) and determine whether to run a particular set of statement or not. The syntax of an if statement is:


if (booleanExpression) {  
      //set of statement 
}



Example of If Statement: Using If statement to find out whether a number is divisible of 5 or not.

public class IfStatement{
  public static void main(String args[]){
    int x=15;
    if(x%5==0){
      System.out.println("x is divisble by 5");   
    }
  }   
}



If-Else Statement:- An If statement runs a block of code if the testing condition is true. By using If-else statement we can give another another block of code that will run when the If condition is not true. In other words in If-Else statement code in If block will run when condition is true and code in Else block will run when condition is false. Below is the syntax of If-Else statement. 


if(booleanExperssion){   
  //set of statement
}
else{
  //set of statement
}



Example of If-Else Statement: In this example we will find weather a number is odd or even using If-Else Statement. As we know if a number is divisible by 2 then it is an even number else it is an odd number. 


public class IfElseStatement{
  public static void main(String args[]){
    int x=5;
    if(x%2==0){
      System.out.println("x is an even number");   
    }else{
      System.out.println("x is an odd number");
    }
  }   
}



If Else If Statement: If-Else-If Statement is used when we have to choose form multiple course of action or we can say If-Else-If Statement is used when we have to check multiple conditions and there is different course of action for every condition. Let’s understand it with an example where we have to set student’s grade on the basis of marks.


public class IfElseIf{
  public static void main(String args[]){    
    int 
marks=85;
    char grade = ' ';
    if(marks >= 90){
      grade = 'A';
    }
    else if (marks >= 80){
      grade = 'B';
    }
    else if (marks >= 70){
      grade = 'C';
    }
    else if (marks >= 60){
      grade = 'D';
    }
    else{
      grade = 'E';
    }
  }   
}


In If-Else-If statements once a condition is found true that particular block of statement are executed and rest of the condition will not be checked. For example when first If condition is found true, it will not check the rest of the Else If or else statements otherwise it will check the second Else If and so on.
In this example, marks are 85 therefore it satisfies more than one given conditions which is greater than equal to 60, greater than equal to 70 and grater than equal to 80. But when the first else if which is greater that equal to 80 found true it will not check the remaining conditions and the grade will be set to B which is the correct grade. But here you can notice a wrongly formatted code can lead to an error. Lets take a look at the code below:


public class IfElseIf{
  public static void main(String args[]){    
    int 
marks=85;
    char grade = ' ';
    if(marks >= 60){
      grade = 'D';
    }
    else if (marks >= 70){
      grade = 'C';
    }
    else if (marks >= 80){
      grade = 'B';
    }
    else if (marks >= 
90){
      grade = 'A';
    }
    else{
      grade = 'E';
    }
  }   
}


In this program as the first if condition is true the grade will be set to D and rest of conditions will be ignored. But the correct grade should be B as the marks scored are more than 80. So now we can see if the program is not correctly formatted it can lead to errors. This problem can be solved using multiple condition in one If block using logical operators like || and &&. 


public class IfElseIf{
  public static void main(String args[]){
    int marks=85;
    char grade = ' ';
    if(marks >= 60 && marks < 70){
      grade = 'D';
    }
    else if (marks >= 70 && marks < 80){     
      grade = 'C';
    }
    else if (marks >= 80 && marks < 90){
      grade = 'B';
    }
    else if (marks >= 90){
      grade = 'A';
    }
    else{
      grade = 'E';
    }
  }   
}



Here by giving multiple conditions using logical operators we can make sure that the code will result in correct output even if the code is not correctly formatted. 


2 comments:

  1. excellent article you are good idea to me .its very interesting to learn and very easy to understand .
    thanks for this blog .
    java training institutes in chennai |
    java j2ee training institutes in velachery

    ReplyDelete