The if
keyword in Java is used to create a conditional statement that executes a block of code only if a specified condition is true. It helps in making decisions in the code based on certain conditions.
Table of Contents
- Introduction
if
Keyword Syntax- Understanding
if
- Examples
- Basic Usage
- Using
if-else
- Using
if-else if-else
- Real-World Use Case
- Conclusion
Introduction
The if
keyword allows the program to make decisions and execute different blocks of code based on whether a condition is true or false. This helps in controlling the flow of the program.
if Keyword Syntax
The syntax for the if
statement is as follows:
if (condition) {
// code to be executed if the condition is true
}
Example:
if (age >= 18) {
System.out.println("You are an adult.");
}
Understanding if
The if
statement evaluates a condition inside parentheses. If the condition is true, the block of code inside the curly braces {}
is executed. If the condition is false, the code inside the curly braces is skipped.
Key Points:
- The condition must be a boolean expression (returns true or false).
- Curly braces
{}
can be omitted if there is only one statement to execute.
Examples
Basic Usage
To demonstrate the basic usage of the if
keyword, we will check if a number is positive.
Example
public class IfExample {
public static void main(String[] args) {
int number = 10;
if (number > 0) {
System.out.println("The number is positive.");
}
}
}
Output:
The number is positive.
Using if-else
The if-else
statement is used to execute one block of code if the condition is true and another block of code if the condition is false.
Example
public class IfElseExample {
public static void main(String[] args) {
int number = -5;
if (number > 0) {
System.out.println("The number is positive.");
} else {
System.out.println("The number is not positive.");
}
}
}
Output:
The number is not positive.
Using if-else if-else
The if-else if-else
statement is used to check multiple conditions. It executes different blocks of code based on which condition is true.
Example
public class IfElseIfExample {
public static void main(String[] args) {
int number = 0;
if (number > 0) {
System.out.println("The number is positive.");
} else if (number < 0) {
System.out.println("The number is negative.");
} else {
System.out.println("The number is zero.");
}
}
}
Output:
The number is zero.
Real-World Use Case
Checking User Age
In real-world applications, the if
statement can be used to check user inputs, such as determining if a user is eligible to vote.
Example
public class VotingEligibility {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}
}
}
Output:
You are eligible to vote.
Conclusion
The if
keyword in Java is a fundamental part of controlling the flow of a program. It allows you to execute certain blocks of code based on whether a condition is true or false. By understanding and using the if
keyword, you can make your Java programs more dynamic and responsive to different conditions.