Control flow statements in Java allow you to control the order in which statements are executed in your program based on certain conditions or criteria.
Definition
If-else statements allow you to execute different blocks of code based on whether a specified boolean condition is true or false.
Syntax
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
Example
int x = 5;
if (x > 10) {
System.out.println("x is greater than 10");
} else {
System.out.println("x is less than or equal to 10");
}
Multiple If-Else Statements
int score = 85;
if (score >= 90) {
System.out.println("A grade");
} else if (score >= 80) {
System.out.println("B grade");
} else if (score >= 70) {
System.out.println("C grade");
} else {
System.out.println("Failing grade");
}
Definition
Switch statements provide a way to execute different parts of code based on the value of an expression. It’s an alternative to long if-else-if chains when dealing with multiple conditions on a single variable.
Concepts:
[!NOTE] Case: Each possible value for the switch expression. Break: Used to exit the switch block after a case is executed. Default: Executes if none of the cases match the switch expression.
Example:
int day = 3;
String dayString;
switch (day) {
case 1:
dayString = "Monday";
break;
case 2:
dayString = "Tuesday";
break;
case 3:
dayString = "Wednesday";
break;
// More cases...
default:
dayString = "Invalid day";
}
System.out.println(dayString);
[!IMPORTANT]
- The
if
statement can be used alone or with else if and else clauses.- Multiple conditions can be chained using
else if
.- The
switch
statement is often more efficient than multipleif-else
statements when comparing a single variable against multiple values.- In a
switch
statement, thebreak
keyword is used to prevent fall-through to the next case.- The
default
case in aswitch
statement is optional and executed when no other case matches.
int x = 5; if (x > 10) { System.out.println("x is greater than 10"); } else { System.out.println("x is less than or equal to 10"); }
A) Greater than 10
B) Equal to 10
C) Less than 10
D) No output
Which of the following is true about switch statements?
A) They can handle ranges of values.
B) They work with variables of typesint
,char
,byte
,short
,String
, andenum
.
C) The default case is mandatory.
D) A break statement is not necessary after each case.Answer
(B is correct. The `default` is not required; The `Break` statements are optional but recommended to prevent fall-through to the next case. )
What is the output of the following code?
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"); break; }
A) Monday
B) Tuesday
C) Wednesday
D) Invalid dayAnswer
C) Wednesday.
Answer
If the break statement is omitted, the program will continue executing the code of the following case(s) until it finds a break statement or reaches the end of the switch block. This is known as "fall-through."
Write a program that asks the user for their age and prints “You are eligible to vote” if they are 18 or older, and “You are not eligible to vote” if they are under 18.
Answer
[VotingAge.java](/learnJava/03_ControlFlowStatement/coding/VotingAge.java)
Write a switch statement that checks a char variable grade for grades ‘A’, ‘B’, ‘C’, ‘D’, or ‘F’ and prints out “Excellent”, “Good”, “Average”, “Poor”, or “Invalid Grade” respectively. Handle the default case for any other input.
Answer
[heckGrades.java](/learnJava/03_ControlFlowStatement/coding/CheckGrades.java)