JAVA IF ELSE statement:

java if else statement

The JAVA if else statement is the most used control flow statement in JAVA development.

There are many use cases for the JAVA if else statement that we will treat them one by one:

JAVA IF Statement:

This statement is used when we want to do some processing only if a condition if true and otherwise this processing will not be done.

The syntax of the simple if else statement is the following:

if (<boolean_expression> ) {
<statements>
}

Or we can omit the opening and closing curli braces (“{“and “}”). The syntax becomes the following:

if (<boolean_expression> )
<statements>

Examples:

int average = 60;
if (average >= 50){
System.out.println(“The Student passed the Exam”);
}

Or we can use the second syntax to do the same thing:

int average = 60;
if (average >= 50)
System.out.println(“The Student passed the Exam”);

In the two previous examples, we are checking if the average of the marks got by a student is above 50.

If this is the case, we display a message in the console which says: “The student passed the Exam” (without quotation marks surely).

This is a dummy example because we initialized the average with the value 60 which is greater than 50.

So, the student always succeeds.

IF ELSE Statement:

This statement is used if we have only two cases or situations or conditions (condition1 and condition2 (which is the opposite of condition1)).

If condition1 is true we do processing1 and otherwise we do processing2.

The syntax of the IF ELSE statement is the following:

if ( <condition1> ) {
<processing1>
}
else {
<processing2>
}

Or we can omit the curli braces. The syntax becomes the following:

if ( <condition1> ) 
<processing1>
else
<processing2>

Examples:

int average = 60;
if (average >= 50){
System.out.println(“The Student passed the Exam”);
}
else {
System.out.println(“The Student failed in the Exam”);
}

Or we can use the second syntax to do the same thing:

int average = 60;
if (average >= 50)
System.out.println(“The Student passed the Exam”);
else
System.out.println(“The Student failed in the Exam”);

Here, we display the message “The Student passed the exam” if the average of his marks is greater or equal 50%.

Otherwise (average < 50), we display the message: “The Student failed in the Exam”

Remark: we can’t put more than one instruction in the “If” clause when omitting the curli brace.

The example below generates a compilation error.

int average = 60;
if (average >= 50)
System.out.println("The Student passed the Exam");
System.out.println("Congratulations");
else
System.out.println("The Student failed in the Exam");

We got an error because we have two instructions in the “if” clause and we omitted the curli braces:

  • Instruction 1: System.out.println("The Student passed the Exam");
  • Instruction 2: System.out.println("Congratulations");

IF ELSE IF … ELSE Statement:

This statement is used if we have more than two conditions.

This is the general syntax because the ELSE IF and the ELSE clauses are optional.

The syntax of the IF ELSE IF … ELSE statement is the following:

if ( <condition1> ) {
<processing1>
}
else if (condition2) {
<processing2>
}
.
.
.
else if (conditionN) {
<processingN>
}
else{
<processingN+1>
}

We can’t omit the curli braces when using “else if” clauses.

The dots mean that we can put an arbitrary number of “else if” clauses (more than one. Otherwise the “IF ELSE” statement suffices).

Example:

int average = 60;
if (average >= 90){
System.out.println(“The Student’s grade is A”);
}
else if (average >= 50 && average < 90) {
System.out.println(“The Student’s grade is B”);
}
Else{
System.out.println(“The Student’s grade is B”);
}

If a student gets an average greater or equal to 90, then his grade will be A.

If a student gets an average between 50 and 90, then his grade will be B.

Otherwise, his grade will be C.

Remark: We recommend using the syntax with curli braces because it ensures the readability of your code and it minimizes the risk of errors.

This java core tutorial which deals with java condition and specifically the “IF ELSE” statements arrives at its end.

Please, don’t forget to follow us on Facebook to get our next JAVA core tutorial.

Post a Comment

Previous Post Next Post