If Else Statement In Java Example
Chapter:
Control Statements
Last Updated:
10-06-2016 12:31:11 UTC
Program:
/* ............... START ............... */
public class JavaIfElseStatement {
public static void main(String[] args) {
int marks, passingMarks;
passingMarks = 40;
Scanner input = new Scanner(System.in);
System.out.println("Input marks scored by you");
marks = input.nextInt();
if (marks >= passingMarks) {
System.out.println("You have passed the exam.");
} else {
System.out.println("Sorry!. You have failed the exam.");
}
}
}
/* ............... END ............... */
Output
Input marks scored by you
80
You have passed the exam.
20
Sorry!. You have failed the exam.
Notes:
-
If the boolean expression evaluates to true, then the if block of code will be executed,otherwise else block of code will be executed.
- If statement can be followed by an optional else statement, which executes when the Boolean expression is false.
Tags
If Else Statement, Java