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.
Control Flow Statements
2. Loops
• Used to execute the set of instructions in a repeated order.
• The execution of the set of instructions depends upon a particular condition.
• Three types of loops that execute similarly.
• But there are differences in their syntax and condition checking time.
• for loop
• while loop
• do-while loop
for loop
• It enables us to initialize the loop variable, check the condition, and
increment/decrement in a single line of code.
• Use the for loop only when we exactly know the number of times, we want to execute
the block of code.
for(initialExpression; condition; updateExpression)
for {
//block of statements
syntax
}
• Here,
• The initialExpression initializes and/or declares variables and executes only once.
• The condition is evaluated. If the condition is true, the body of the for loop is executed.
• The updateExpression updates the value of initialExpression.
• The condition is evaluated again. The process continues until the condition is false.
For-each loop
• An enhanced for loop to traverse the data structures like array or collection. In the
for-each loop, we don't need to update the loop variable.
Syntax
for(data_type variable : array_name/collection_name){
//statements
}
while loop
• Used to iterate over the number of statements multiple times.
• If we don't know the number of iterations in advance, it is recommended to use a while
loop.
• Unlike for loop, the initialization and increment/decrement doesn't take place inside the
loop statement in while loop.
• Also known as the entry-controlled loop since the condition is checked at the start of the
loop.
• If the condition is true, then the loop body will be executed; otherwise, the statements
after the loop will be executed.
While Java while loop is used to run a specific code until a certain condition is
met.
syntax
ELSE ???