Java If-else Statement Explain | What are the 4 types of if statement in java?

Java If-else

Java If-else Statement Explain.


The Java if statement is used to test the condition. It checks boolean condition: true or false. There are various types of if statement in Java.

  • if statement
  • if-else statement
  • if-else-if ladder
  • nested if statement

Java if-else-if ladder Statement

The if-else-if ladder statement executes one condition from multiple statements.

Syntax:

  1. if(condition1){  
  2. //code to be executed if condition1 is true  
  3. }else if(condition2){  
  4. //code to be executed if condition2 is true  
  5. }  
  6. else if(condition3){  
  7. //code to be executed if condition3 is true  
  8. }  
  9. ...  
  10. else{  
  11. //code to be executed if all the conditions are false  
  12. }  
  13. Example:-

    1. //Java Program to demonstrate the use of If else-if ladder.  
    2. //It is a program of grading system for fail, D grade, C grade, B grade, A grade and A+.  
    3. public class IfElseIfExample {  
    4. public static void main(String[] args) {  
    5.     int marks=65;  
    6.       
    7.     if(marks<50){  
    8.         System.out.println("fail");  
    9.     }  
    10.     else if(marks>=50 && marks<60){  
    11.         System.out.println("D grade");  
    12.     }  
    13.     else if(marks>=60 && marks<70){  
    14.         System.out.println("C grade");  
    15.     }  
    16.     else if(marks>=70 && marks<80){  
    17.         System.out.println("B grade");  
    18.     }  
    19.     else if(marks>=80 && marks<90){  
    20.         System.out.println("A grade");  
    21.     }else if(marks>=90 && marks<100){  
    22.         System.out.println("A+ grade");  
    23.     }else{  
    24.         System.out.println("Invalid!");  
    25.     }  
    26. }  
    27. }  

    Output:

    C grade


 Certainly! In Java, the if-else statement is a conditional statement that allows you to execute different blocks of code based on whether a certain condition is true or false.

Here's the basic syntax of the if-else statement:

if (condition) { // code to execute if the condition is true } else { // code to execute if the condition is false }


When the program encounters an if-else statement, it first evaluates the condition inside the parentheses. If the condition is true, the code inside the first block of curly braces will be executed, and the code inside the else block will be skipped. If the condition is false, the code inside the else block will be executed instead.

Here's an example of how to use the if-else statement

int x = 10; if (x > 0) { System.out.println("x is positive"); } else { System.out.println("x is not positive");
 }


In this example, the condition inside the parentheses is x > 0. If x is greater than 0, the code inside the first block of curly braces will be executed, and the message "x is positive" will be printed to the console. If x is less than or equal to 0, the code inside the else block will be executed, and the message "x is not positive" will be printed instead.

Note that the if-else statement can also be nested inside other if-else statements to create more complex conditions.

Previous Post Next Post