Java if
statement with
Examples
This article explains the Java if statement, a fundamental
decision-making statement in programming. It allows you
to execute specific code blocks based on whether a
condition is true or false.
by Tariq Atlas
Decision Making in Java
Decision Making in Java is a crucial concept that enables programs to make choices and
execute different code paths based on specific conditions.
1 if statement
The if statement is the most basic decision-making statement in Java. It allows you
to execute a block of code only if a certain condition is true.
2 if-else statement
The if-else statement provides an alternative path to execute if the condition in the if
statement is false.
3 switch statement
The switch statement allows you to select one block of code to execute from
multiple options based on the value of a variable.
4 break and continue statements
The break and continue statements are used to control the flow of execution within
loops, allowing you to exit a loop prematurely or skip to the next iteration.
Java if statement
The Java if statement is a simple decision-making statement that executes a block of code only if a specified
condition is true.
Syntax Working Flowchart
1. Control falls into the if block.
if(condition) {
2. The flow jumps to Condition.
// Statements to
execute if 3. Condition is tested.
// condition is true 4. If Condition yields true, the
} if-block is executed.
5. If Condition yields false,
the if-block is skipped.
Example 1
This example demonstrates a basic if statement that checks if the
variable 'i' is less than 15. If true, it prints a message to the
console.
// Java program to illustrate If statement
class IfDemo {
public static void main(String args[]) {
int i = 10 ;
if (i < 15 )
[Link]( "10 is less than 15"
);
[Link]( "Outside if-block" );
// both statements will be printed
}
}
Example 2
This example demonstrates an if statement that checks if the variable 'i' is equal to 4. If true, it increments 'i' and prints a message to the console.
// Java program to illustrate If statement
class IfDemo {
public static void main(String args[]) {
String str = "GeeksforGeeks" ;
int i = 4 ;
// if block
if (i == 4 ) {
i++;
[Link](str);
}
// Executed by default
[Link]( "i = " + i);
}
}
Example 3: Implementing if else for
Boolean values
This example demonstrates an if-else statement that checks the values of two boolean variables, 'a' and
'b'. It prints messages to the console based on the truth values of the variables.
public class IfElseExample {
public static void main(String[] args) {
boolean a = true ;
boolean b = false ;
if (a) {
[Link]( "a is true" );
} else {
[Link]( "a is false" );
}
if (b) {
[Link]( "b is true" );
} else {
[Link]( "b is false" );
}
}
}
Advantages of If else statement
The if-else statement offers several advantages in programming, making it a fundamental tool for controlling the flow of execution.
Conditional Execution Readability
The if-else statement allows code to be executed conditionally The if-else statement makes code more readable by clearly
based on the result of a Boolean expression. indicating when a particular block of code should be executed.
Reusability Debugging
By using if-else statements, developers can write code that can be The if-else statement can help simplify the debugging process by
reused in different parts of the program. making it easier to trace problems in the code.
Related Articles
This section provides links to related articles that delve deeper into decision-making concepts in Java
and related topics.
• Decision Making in Java
• Java if-else statement with Examples
• Java if-else-if ladder with Examples
• Switch Statement in Java
• Break statement in Java
• return keyword in Java