If Else Statement Java: In Java programming language, If-else statements are fundamental constructs that allow developers to control the flow of their program based on certain conditions. These statements allow the programmer to execute a specific code set if a condition is met and another code if the condition is not.
The If-else statement is a powerful tool in Java as it enables the creation of flexible and dynamic programs that can adapt to changing inputs and situations. This allows developers to design robust and efficient applications that handle various scenarios. This article will explore the If-else statement in Java, its syntax, and its various applications.
In this article, we will learn how to use two different conditional statements in Java. That is, if and if….else, and also we see the control flow of the program execution when we are trying to execute some set of statements based on some condition, we have to use the control flow statements.
Java Programs Structure
Let’s take an example of a boy’s age being greater than 18; we want to print “Major” but if his age is less than 18, we want to write “Minor”. If we print Major or minor based on the input value at that time, we have to use control flow statements.
Control Statements In Java
This core Java tutorial post will show the different types of control flow statements used in the Java program per the requirement. In Java, There are four main types of control flow statements present:
If Statement Java
To begin with, the If-else statement in Java is a conditional statement that evaluates a Boolean expression and executes a block of code if the expression is true. If the expression is false, the program executes a different code block. The basic syntax of an If-else statement in Java is as follows:
if (Boolean expression) { // Code to execute if the expression is true } else { // Code to execute if the expression is false }
As the above syntax shows, the statement is only executed when the condition becomes True. If the condition is false, the execution completely ignores the statement body.
Program: If Statement Java
package java_Basics; public class If_Example { public static void main(String[] args) { int number = 99; if (number > 0) { System.out.println("Number is positive."); } System.out.println("This statement is always executed."); } }
Output:
Number is positive. This statement is always executed.
Nested if Statement in Java
A nested if statement is a conditional statement placed inside another if statement. The nested if statement allows programmers to create more complex conditions and control the flow of their program based on multiple conditions. A nested if statement consists of an outer statement and one or more inner if statements.
The syntax of a nested if statement in Java is as follows:
if (condition1) { // code to execute if condition1 is true if (condition2) { // code to execute if condition2 is true } } else { // code to execute if condition1 is false }
In the above example, an outer if statement evaluates condition1. If condition1 is true, then the code inside the outer if statement will be executed. However, if condition1 is false, the code inside the other block will be executed. Inside the outer if statement is an inner if statement that evaluates condition2. If condition2 is true, the code inside the inner if statement will be executed.
Nested-if statements can be extended with additional else-if and else blocks to create even more complex conditions. It is important to note that nested-if statements can become difficult to read and debug if nested too deeply. Therefore, using them judiciously and keeping them as simple as possible is recommended.
Program: Nested if Statement Java
package java_Basics; public class NestedIf_Example { public static void main(String[] args) { int i = 10; if (i == 10) { // First if statement if (i < 15) System.out.println("i is smaller than 15"); // Nested - if statement // Will only be executed if statement above // it is true if (i < 12) System.out.println("i is smaller than 12 too"); else System.out.println("i is greater than 15"); } } }
Output:
i is smaller than 15 i is smaller than 12 too
If Else Statements Java
The if Statement executes only certain sections of code. But there is also an else block present. The statement of the else block will be executed if the condition is false. The Syntax for if-else looks like this:
How to write an if-else statement in Java
if (condition) { // statement(s); } else { // statement(s); }
if-else statement in a Java example program
Program: If Else Statement Java
package java_Basics; public class IfElse_Example { public static void main(String[] args) { int i = 10; if (i < 15) System.out.println("i is smaller than 15"); else System.out.println("i is greater than 15"); } }
Output:
i is smaller than 15
If – Else If In Java
The if statement executes a particular section of code if the test expression is evaluated to be true. The if statement may have an optional else block. Statements inside the body of the else statement are executed if the test expression is evaluated to be false. The syntax for if..else..if it looks like below:
if (condition1) { // codes } else if(condition2) { // codes } else if (condition3) { // codes } . . else { // codes }
Note: In this control flow statement, one thing you have to keep in mind that when the condition is true at that time, the corresponding statement will be executed, and when there is none of the conditions is true at that time, the statements that are inside in else block will be executed.
Program: If Else If Java
package com.java.Softwaretestingblog; public class IfElse_Program { public static void main(String[] args) { // TODO Auto-generated method stub int marks=80; if(marks<50) { System.out.println("fail"); } else if(marks>=50 && marks<60) { System.out.println("D grade"); } else if(marks>=60 && marks<70) { System.out.println("C grade"); } else if(marks>=70 && marks<80) { System.out.println("B grade"); } else if(marks>=80 && marks<90) { System.out.println("A grade"); } else if(marks>=90 && marks<100) { System.out.println("A+ grade"); } else { System.out.println("Invalid!"); } } }
Output:
A grade
Do you have any questions about the topic we discussed in our post? Leave them in the comments section below, and we’ll do our best to provide a helpful answer.