if Statement
public class IfStatementExample {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are an adult.");
}
}
}
It executes a block of code only if the specified condition is true
The condition is put in ()
paranthesis. Condition is always a boolean expression (i.e. expression results in true/false value)
Braces {}
is not needed if only one statement is there in if
block.
int x = 10;
if(x > 10)
System.out.println("hello");
System.out.println("Hii");
If-Else Statement
public class IfElseExample {
public static void main(String[] args) {
int number = 10;
if (number % 2 == 0) {
System.out.println(number + " is even.");
} else {
System.out.println(number + " is odd.");
}
}
}
It executes first block of code if the condition is true
, and second block if the condition is false
.
If-Else-If Statement
public class ElseIfExample {
public static void main(String[] args) {
int marks = 75;
if (marks >= 90) {
System.out.println("Grade: A");
} else if (marks >= 80) {
System.out.println("Grade: B");
} else if (marks >= 70) {
System.out.println("Grade: C");
} else if (marks >= 60) {
System.out.println("Grade: D");
} else {
System.out.println("Grade: F");
}
}
}
The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed
Please note: else
block is optional.
Nested IFs
if
statement under other if
statements.
public class NestedIfExample {
public static void main(String[] args) {
int age = 25;
boolean isStudent = false;
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 a minor.");
}
}
}
Switch Statements
It evaluates an expression and executes code blocks based on matching case values. The break
statement terminates the switch
block.
The value of the expression is compared with each of the values in the case statements. The case
labels must be compile-time constant expressions (constants or literals). If a match is found, the code sequence following that case statement is executed. If none of the constants matches the value of the expression, then the default statement is executed. However, the default statement is optional. If no case matches and no default is present, then no further action is taken.
public class SwitchExample {
public static void main(String[] args) {
int dayOfWeek = 3;
String dayName;
switch (dayOfWeek) {
case 1:
dayName = "Sunday";
break;
case 2:
dayName = "Monday";
break;
case 3:
dayName = "Tuesday";
break;
case 4:
dayName = "Wednesday";
break;
case 5:
dayName = "Thursday";
break;
case 6:
dayName = "Friday";
break;
case 7:
dayName = "Saturday";
break;
default:
dayName = "Invalid day";
}
System.out.println("Day of the week: " + dayName);
}
}
Expression: It has a value which will be compared against each case
label. It can be of type byte
, short
, int
, char
, String
, enumeration
. Object reference can also be used in expression (will discuss it later). Each value specified in the case statements must be a unique constant expression (such as a literal value). Duplicate case values are not allowed. The type of each value must be compatible with the type of expression.
When the code flow goes inside the case and when it sees break;
the code jumps out the the switch block. Please note, until it sees break;
the code will keep on executing for next case also. It is sometimes desirable to have multiple cases without break statements between them.
Nested Switch Statements
Switch can also be used inside the switch statements.
public class NestedSwitchExample {
public static void main(String[] args) {
int category = 1;
int subcategory = 2;
switch (category) {
case 1:
System.out.println("Category: Electronics");
switch (subcategory) {
case 1:
System.out.println("Subcategory: Mobile Phones");
break;
case 2:
System.out.println("Subcategory: Laptops");
break;
default:
System.out.println("Unknown subcategory");
}
break;
case 2:
System.out.println("Category: Clothing");
switch (subcategory) {
case 1:
System.out.println("Subcategory: Men's Clothing");
break;
case 2:
System.out.println("Subcategory: Women's Clothing");
break;
default:
System.out.println("Unknown subcategory");
}
break;
default:
System.out.println("Unknown category");
}
}
}
Significance of Switch Statements
- They offer an alternative to lengthy if-else chains when there are multiple conditions to check against a single variable.
- When you’re faced with choosing from a considerable range of values, opting for a switch statement offers a significant speed advantage over crafting the equivalent logic using a series of if-else statements. This enhanced performance stems from the compiler’s ability to efficiently handle the comparison process. In a switch statement, the compiler recognizes that the case constants share the same type and only require equality comparison with the switch expression. Conversely, with a lengthy list of if expressions, the compiler lacks the contextual understanding available in switch statements, resulting in potentially slower execution.