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

Lecture 11 - Flow Controls

Uploaded by

devmith2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Lecture 11 - Flow Controls

Uploaded by

devmith2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

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.
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

while (testExpression) { Here,


// body of loop
} • A while loop evaluates the textExpression inside the parenthesis ().
• If the textExpression evaluates to true, the code inside the while loop is
executed.
• The textExpression is evaluated again.
• This process continues until the textExpression is false.
• When the textExpression evaluates to false, the loop stops.
public class WhileLoopExample {
public static void main(String[] args) { int
count = 1;

while (count <= 5) {


System.out.println("Count is " + count);
count++;
}
}
}
do-while loop
• Checks the condition at the end of the loop after executing the loop statements.
• When the number of iteration is not known and we have to execute the loop at
least once, we can use do-while loop.
• It is also known as the exit-controlled loop since the condition is not checked in
advance.
do
do- {
//statements
while } while (condition);
syntax
while Vs. do- while
Control Flow Statements
3. Jump Statements

• Transfer the execution control to the other part of the program.


• There are two types of jump statements in Java.
• break
• continue.
break Statement
• Used to break the current flow of the program and transfer the
control to the next statement outside a loop or switch statement.
• However, it breaks only the inner loop in the case of the nested loop.
• The break statement cannot be used independently in the Java
program (It can only be written inside the loop or switch statement)
public class BreakExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
System.out.println("Breaking the loop at i = 3");
break;
}
System.out.println("i = " + i);
}
}
}
continue Statement
• Doesn't break the loop, whereas, it skips the specific part of the loop and
jumps to the next iteration of the loop immediately.
public class ContinueExample {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
System.out.println("Skipping the iteration at i = 3");
continue;
}
System.out.println("i = " + i);
}
}
}
END

ELSE ???

You might also like