Java Tutorials

100+ tutorials, explanation videos, and examples to help you develop creative and technical skills.

Java if-else Statements

Java starts execution of a program from the first line and goes through every statement (except comments) till the end of the program.

This execution finishes in milliseconds. Now suppose, we want to pause/skip some lines of code from execution. For example, a college needs a java program to count the number of students of a specific class and we have data of all students of the college. Here we need a java program that can skip all the classes except one class of which we want to count the number of students. So here, if-else conditions will help to do that.

If Statement

If statement consists of a Boolean expression followed by one or more statements.

Syntax of if statement

In the above syntax, the condition is a Boolean expression that returns either true or false. If the expression returns true then statements (in if body) will be executed otherwise java skips all these statements.

Examples of if statement

Example 1.

Example 2.

In example 1, 10 is printed because expression ( a==10 ) is true and if’s body (statements in if’s body) executed.

In example 2, nothing is printed because expression ( a > 10 ) is false. Java skipped the statement in if’s body

If - Else Statement

The ‘if’ statement executes block of code if the expression is true otherwise it does nothing.

‘else’ statement is alternate of ‘if’ statement. If expression is false then ‘if’ statement won’t execute In this case we can use an option ‘else’ statement.

Syntax of if - else

Examples of if - else

Example 1.

Example 2.

If you have any Questions or Queries
You can mail us at info.learnoset@gmail.com

Follow us to learn Coding and get in touch with new Technologies.