In programming, decision-making statements help the computer choose what to do based on conditions. It’s like making choices in real life.
For example, if it’s raining, you take an umbrella. Otherwise, you don’t.
In Java, we use the following decision-making statements:
if Statement
It checks a condition. If true, it runs the code inside.
Example:
public class MasterBackend {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
System.out.println("You can vote.");
}
}
}
If age
is 18 or more, it prints "You can vote."
If-Else Statement
The if-else
statement is used to check a condition and execute different blocks of code based on whether the condition is true
or false
.
Syntax:
if (condition) {
// Code to execute if the condition is true
} else {
// Code to execute if the condition is false
}
Example:
public class MasterBackend {
public static void main(String[] args) {
int age = 18;
if (age >= 18) {
System.out.println("You can vote.");
} else {
System.out.println("You cannot vote.");
}
}
}
Explanation:
The program checks if the value of
age
is greater than or equal to 18.If the condition is
true
, it prints "You can vote."If the condition is
false
, it prints "You cannot vote."
If-Else If-Else Statement
The if-else if-else
statement is used when multiple conditions need to be checked sequentially.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if none of the conditions are true
}
Example:
public class MasterBackend {
public static void main(String[] args) {
int marks = 85;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 75) {
System.out.println("Grade: B");
} else if (marks >= 50) {
System.out.println("Grade: C");
} else {
System.out.println("Grade: F");
}
}
}
Explanation:
The program checks multiple conditions for
marks
.If
marks
is 90 or above, it prints "Grade: A".If
marks
is between 75 and 89, it prints "Grade: B".If
marks
is between 50 and 74, it prints "Grade: C".If none of the conditions match, it prints "Grade: F".
If else is the one of the control flow statement. here we can regulate the flow of the statement which will be executed. where ever we need to take decision we go for else.
Common Mistakes in If-Else Statements
A common mistake in if
statements is using the assignment operator =
instead of the comparison operator ==
. This results in a compile-time error.
Example of a Mistake:
public class MasterBackend {
public static void main(String[] args) {
int a = 10;
int b = 10;
if (a = b) { // Compilation Error
System.out.println("Equal");
} else {
System.out.println("Unequal");
}
}
}
Explanation:
The statement
if (a = b)
results in a compile-time error because=
is an assignment operator, not a comparison operator.The correct way to write this condition is
if (a == b)
, which correctly compares the two values.Always use
==
for comparisons, otherwise, it will lead to compilation errors.
If-Else Without Braces
When there is only a single statement inside an if
or else
block, the braces {}
can be omitted.
Example:
public class MasterBackend {
public static void main(String[] args) {
int age = 18;
if (age >= 18)
System.out.println("You can vote.");
else
System.out.println("You cannot vote.");
}
}
Explanation:
Since there is only one statement inside both the
if
andelse
blocks, we do not need to use braces{}
.However, using braces is recommended for better readability and to avoid potential errors when adding more statements in the future.
Switch-Case Statement
The switch-case
statement is used when a variable is compared against multiple possible values. It provides a cleaner alternative to multiple if-else if
statements.
Syntax:
switch (variable) {
case value1:
// Code to execute if variable == value1
break;
case value2:
// Code to execute if variable == value2
break;
default:
// Code to execute if no case matches
}
Example:
public class MasterBackend {
public static void main(String[] args) {
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;
case 4:
System.out.println("Thursday");
break;
case 5:
System.out.println("Friday");
break;
case 6:
System.out.println("Saturday");
break;
case 7:
System.out.println("Sunday");
break;
default:
System.out.println("Invalid day");
}
}
}
Explanation:
The variable
day
is checked against multiple cases.If
day
matches a case value, the corresponding block executes.The
break
statement prevents fall-through to the next case.The
default
block executes if no case matches.