0% found this document useful (0 votes)
21 views

Lecture 11 - Flow Controls

Uploaded by

devmith2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Lecture 11 - Flow Controls

Uploaded by

devmith2005
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

ICT 1411

Object Oriented Programming


Lecture 3
Flow Controls - Selection
Control Flow Statements

• Java compiler executes the code from top to bottom.


• The statements in the code are executed according to the order in which
they appear.
• Java provides statements that can be used to control the flow of Java
code.
• Such statements are called control flow statements.
• Three types of control flow statements.

1. Decision Making statements

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 }

public class IfExample {


public static void main(String[] args) {
int number = 10;

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 statements in programming languages provide many benefits, including:


• Switch statements reduce the complexity of the code, thereby increasing its
readability.
• Switch statements increase code maintenance. It is easier to update the code and
make changes to it.
• The switch statements execute faster than if else statements.
• Switch case statements are more advantageous when there are more conditions.
Limitations of 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 ???

You might also like