Java If Statement

18-Sep-2024

Learn how to use the if statement for conditional logic in Java programs.



In the if control statement. Code will be executed based on a condition. If the condition is true then the if block is executed and if the condition is false then nothing will happen.


Syntax :



if(condition) {

// if the condition is true then this block of code will execute

}



Flow Chart:





 Example:



import java.util.Scanner;
public class ifest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int age = sc.nextInt();

      if(age>18){

      System.out.println("adult");

}

    }

}
     


In this example, if age is grater than 18  then if block will exucuted ( output is adult ). If the condition is false then nothing will happen.


Comments