a conditional statement is a programming construct that allows you to execute different blocks of code depending on whether a particular condition is true or false.
There are four types of conditional statements in Java:
- if statement
- if-else
- if-else-if
The if statement is a conditional statement that allows you to execute a block of code if a particular condition is true. The syntax for the if statement is:
if (boolean_expression)
{
// code to execute if the boolean expression is true
}
Here’s an example that demonstrates the use of an if statement:
int num = 5;
if (num > 0)
{
System.out.println("num is positive");
}
In this example, the program evaluates the expression num > 0
and because it is true, it executes the block of code within the if statement. The output of the program is “num is positive”.
You can also use the if statement with logical operators (&&, ||) to create more complex conditions. For example:
int num1 = 5;
int num2 = 10;
if (num1 > 0 && num2 > 0)
{
System.out.println("Both numbers are positive");
}
In this example, the program evaluates the expression num1 > 0 && num2 > 0
and because both conditions are true, it executes the block of code within the if statement. The output of the program is “Both numbers are positive”.
The if-else statement is a type of conditional statement that allows you to execute different blocks of code depending on whether a condition is true or false.
The syntax for the if-else statement is as follows:
if (condition)
{
// block of code to be executed if the condition is true
}
else
{
// block of code to be executed if the condition is false
}
Here’s an example that demonstrates the use of the if-else statement:
int num = 5;
if (num > 10)
{
System.out.println("num is greater than 10");
}
else
{
System.out.println("num is less than or equal to 10");
}
In this example, the program first evaluates the expression num > 10
. Since this expression is false, the block of code inside the else statement is executed, which prints the message “num is less than or equal to 10”.
The if-else-if statement is used to execute different blocks of code depending on multiple conditions. The syntax for the if-else-if statement is:
if (condition1)
{
// code to execute if condition1 is true
}
else if (condition2)
{
// code to execute if condition2 is true
}
else if (condition3)
{
// code to execute if condition3 is true
}
else
{
// code to execute if none of the conditions are true
}
The conditions are evaluated from top to bottom, and the first condition that is true causes the corresponding block of code to execute. If none of the conditions are true, then the else block is executed.
Here’s an example that demonstrates the use of an if-else-if statement:
int num = 10;
if (num < 0)
{
System.out.println("num is negative");
}
else if (num == 0)
{
System.out.println("num is zero");
}
else
{
System.out.println("num is positive");
}
In this example, the program evaluates the conditions in order. Because none of the conditions are true except for the last one, the program executes the block of code within the else statement. The output of the program is “num is positive”.
Nested if
It is a conditional statement that is placed inside another conditional statement. This allows for more complex decision making in your program, as you can test multiple conditions at once.
The syntax for a nested if statement is:
if (condition1)
{
// code to execute if condition1 is true
if (condition2)
{
// code to execute if both condition1 and condition2 are true
}
}
In this example, the code within the inner if statement will only be executed if both condition1 and condition2 are true.
Here’s an example that demonstrates the use of a nested if statement:
int age = 25;
boolean isStudent = true;
if (age >= 18)
{
System.out.println("You are an adult");
if (isStudent)
{
System.out.println("You are a student");
}
else
{
System.out.println("You are not a student");
}
}
else
{
System.out.println("You are not an adult");
}
In this example, the program first evaluates the age of the person. If they are 18 or older, the program executes the code within the outer if statement. If the person is also a student, then the program executes the code within the inner if statement. If the person is not a student, then the program executes the code within the else statement. If the person is younger than 18, then the program executes the code within the else statement.