Lecture 11 - Flow Controls
Lecture 11 - Flow Controls
2. Loop statements
3. Jump statements
1. Decision Making statements
• if statements
• switch statement
2. Loop statements
• do while loop
• while loop
• for loop
• for-each loop
3. Jump statements
• break statement
• continue statement
Control Flow Statements
1.Decision Making
• Decision-making statements decide which statement to execute and when.
• Decision-making statements evaluate the Boolean expression and control
the program flow depending upon the result of the condition provided.
• There are two types of decision-making statements in Java.
• If statement
• switch statement.
if Statements
• The "if" statement is used to evaluate a condition.
• The control of the program is diverted depending upon the specific condition.
• The condition of the If statement gives a Boolean value, either true or false.
• In Java, there are four types of if-statements given below.
• Simple if statement
• if-else statement
• if-else-if ladder
• Nested if-statement
if if(condition) {
statement 1; //executes when condition is true
syntax }
if (number > 5) {
System.out.println("Number is greater than 5.");
}
}
}
Source: https://data-flair.training/blogs/decision-making-in-java/
if-else
Syntax
if(condition) {
statement 1; //executes when condition is true
}
else{
statement 2; //executes when condition is false
}
Source: https://data-flair.training/blogs/decision-making-in-java/
public class IfElseExample {
public static void main(String[] args) {
int number = 3;
if (number % 2 == 0) {
System.out.println("Number is even.");
} else {
System.out.println("Number is odd.");
}
}
}
if(condition 1) {
statement 1; //executes when condition 1 is true
}
if-else-if
else if(condition 2) {
statement 2; //executes when condition 2 is true
syntax }
else {
statement 2; //executes when all the conditions are
false
}
Source: https://data-flair.training/blogs/decision-making-in-java/
public class IfElseIfLadderExample {
public static void main(String[] args) {
int score = 85;
if (score >= 90) {
System.out.println("Excellent!");
} else if (score >= 80) {
System.out.println("Very good.");
} else if (score >= 70) {
System.out.println("Good.");
} else {
System.out.println("Needs improvement.");
}
}
}
if(condition 1) {
statement 1; //executes when condition 1 is true
if(condition 2) {
statement 2; //executes when condition 2 is true
Nested if }
else{
statement 2; //executes when condition 2 is false
}
}
Source: https://data-flair.training/blogs/decision-making-in-java/
public class NestedIfExample {
public static void main(String[] args) {
int x = 5;
int y = 10;
if (x < 0) {
if (y > 0) {
System.out.println("Both x and y are positive.");
} else {
System.out.println("x is positive, but y is not.");
}
} else {
System.out.println("x is not positive.");
}
}
}
Switch Statement
• Similar to if-else-if statements.
• The switch statement contains multiple blocks of code called cases and a single case
is executed based on the variable which is being switched.
• The switch statement is easier to use instead of if-else-if statements.
Important
• The case variables can be int, short, byte, char, or enumeration.
• Cases cannot be duplicate
• Default statement is executed when any of the case doesn't match the value of expression.
It is optional.
• Break statement terminates the switch block when the condition is satisfied.
• It is optional, if not used, next case is executed.
• While using switch statements, we must notice that the case expression will be of the
same type as the variable. However, it will also be a constant value.
switch (expression){
case value1:
statement1;
break;
.
switch .
.
case valueN:
statementN;
break;
default:
default statement;
}
Source: https://data-flair.training/blogs/decision-making-in-java/
public class SimpleSwitchExample {
public static void main(String[] args) {
int choice = 2;
String message;
switch (choice) {
case 1:
message = "You selected option 1.";
break;
case 2:
message = "You selected option 2.";
break;
default:
message = "Invalid choice.";
break;
}
System.out.println(message);
}
}
Benefits of Using Switch
• Switch case can compare only integer and character values. Conditions involving
complex data types like string, float, and double cannot be evaluated using switch
statements, resulting in limited flexibility of switch statements.
• Switch case statements can be used to check for only equality conditions. They do
not support complex conditions like greater than, less than, etc.
• Switch statements often result in code duplication.
END
ELSE ???