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

Similar Programs Chapter Last Updated
Java For Loop And ForEach Loop Example Control Statements 03-09-2016
Java Enum And Switch Statement Control Statements 03-09-2016
Control Statements In Java Example Control Statements 19-06-2016
If Statement In Java Example Control Statements 17-06-2016
Java Switch Example Control Statements 14-06-2016
While Loop In Java Example Control Statements 08-06-2017
Return Statement In Java example Control Statements 15-07-2016
Print Stars In Java Example Control Statements 15-07-2016
Nested Loop In Java Example Control Statements 15-07-2016
Infinite While Loop In Java Control Statements 10-06-2016
Infinite Loop In Java Example Control Statements 15-07-2016
For Loop In Java Example Control Statements 07-06-2017
For Each In Java Example Control Statements 07-06-2017
Do While Loop In Java Example Control Statements 10-06-2016
Continue Statement In Java Example Control Statements 30-03-2017
Java Break Statement Example Control Statements 06-07-2016

1