Conditional statements in Java are used to control the flow of execution based on conditions. These statements evaluate a boolean expression and execute different blocks of code depending on whether the expression evaluates to true
or false
. Conditional statements enable decision-making in a program.
Types of Conditional Statements in Java:
if
Statement:- The
if
statement executes a block of code only if the given condition evaluates totrue
.
Syntax:
java
if (condition) {
// Code to be executed if the condition is true
}
Example:
java
int number = 10;
if (number > 5) {
System.out.println("The number is greater than 5");
}
In this example, the message is printed only if the condition number > 5
is true
.
if-else
Statement:- The
if-else
statement executes one block of code if the condition istrue
, and another block if the condition isfalse
.
Syntax:
java
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
Example:
java
int age = 20;
if (age >= 18) {
System.out.println("You are an adult");
} else {
System.out.println("You are a minor");
}
In this example, if the condition age >= 18
is true
, it prints "You are an adult"; otherwise, it prints "You are a minor."
if-else if-else
Statement:- The
if-else if-else
statement is used when there are multiple conditions to test. The first condition that evaluates totrue
will execute its associated block of code, and the rest will be skipped.
Syntax:
java
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else {
// Code if none of the conditions are true
}
Example:
java
int score = 85;
if (score >= 90) {
System.out.println("Grade: A");
} else if (score >= 80) {
System.out.println("Grade: B");
} else {
System.out.println("Grade: C");
}
In this example, if score >= 90
, it prints "Grade: A". If the first condition is false but score >= 80
is true, it prints "Grade: B". Otherwise, it prints "Grade: C".
switch
Statement:- The
switch
statement tests a variable against multiple possible values (cases) and executes the code block corresponding to the first matching case.
Syntax:
java
switch (expression) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if no cases match
}
Example:
java
int day = 3;
switch (day) {
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Invalid day");
}
In this example, the variable day
is matched against the case values. Since day = 3
, it prints "Wednesday".
How Conditional Statements Work in Java:
- Condition Evaluation:
Conditional statements evaluate a boolean expression (i.e., true
or false
). The result of this evaluation determines which block of code will be executed.
2. Execution Based on Condition:
3. If the condition is true
, the corresponding block of code will be executed.
4. If the condition is false
, execution moves to the else
block or subsequent conditions in an if-else if
chain or switch
case.
5. Multiple Conditions:
Java allows nesting of if-else
and multiple conditions to create complex decision-making structures. The switch
statement is an alternative to if-else
chains for testing a variable against multiple values.
Example of Nested if-else
:
int num = 10; if (num > 0) { if (num % 2 == 0) { System.out.println("Positive even number"); } else { System.out.println("Positive odd number"); } } else { System.out.println("Non-positive number"); }
In this nested structure, the program first checks if the number is positive and then checks if it's even or odd.
Key Points:
if
,if-else
,if-else if
: Used for decision-making based on conditions.switch
: Used for multiple value-based conditions for a single variable.- Conditions must evaluate to a boolean expression.
- Parentheses
()
are required around the condition.
Summary:
- Conditional statements allow Java programs to make decisions based on conditions.
- They include
if
,if-else
,if-else if
, andswitch
. - These statements evaluate conditions and execute different blocks of code depending on the result of the condition.
In conclusion, conditional statements in Java are essential for controlling the flow of a program, enabling it to make decisions and execute specific code paths based on conditions.