Spring Semester
2024-2025
CS112
Programming Languages Lecture 7
Mahmoud Bassiouni, PhD
[email protected]
Table of Contents
• Infinite Loops
• Break and Switch Statements
• Nested If and Nested Loops
• More Examples on the loops
2
Infinite Loops
• Infinite Loops: An infinite loop in Java is a loop that never ends unless the program is
forcefully terminated.
Examples on Infinite loop
while (true) { do { for ( ; ; ){
// loop body
} } while (true); }
3
Infinite Loops
• What will have in the following cases:
for ( int x = 1; ; x++ ){
Infinite Loop
}
for ( int x = 1; x <=5 ; ){
Infinite Loop
4
Break Statement
• The break statement, when executed in a:
while, for, do..while, or switch..case statement
Causes an immediate exist from that statement.
• Program execution continuous with the next/following statement.
• Common uses of the break statement are to escape early from a loop, or to skip the
remainder of a switch statement.
5
Break Statement
• Let's consider the following example:
for (int i = 1; i <= 5; i++) {
if( i == 3){
break;
}
System.out.print(“Welcome”);
} Output:
Welcome
Welcome
6
Continue Statement
• The continue statement, when executed in while, for, or a do..while statement, skips
the remaining statements in the body of that control statement, and then it performs
the next iteration of the loop.
for (int i = 1; i <= 5; i++) {
if ( i == 3) {
continue;
} Output:
System.out.print(“Welcome”); Welcome
} Welcome
Welcome
Welcome
7
Nesting If
• Nesting mean putting something inside another something.
• A nested if means putting one if statement inside another.
if (condition1) {
if (condition2) {
// code runs if both conditions are true
}
} int age = 20;
int score = 90;
if (age > 18) {
if (score > 80) {
System.out.println("Qualified");
}
} 8
Nesting If
int age = 20;
int score = 90;
if (age > 18 && score > 80) {
Can be replaced with
if (age > 18) {
System.out.println("Qualified");
if (score > 80) {
}
System.out.println("Qualified");
}
}
if (age > 18) {
System.out.println("Age passed");
It will not be the
same if we
if (score > 80) {
need to print
something at the System.out.println("Also qualified by score");
age }
}
9
Nested Loop
• A nested loop is a loop inside another loop.
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 2; j++) {
// inner loop runs completely for each outer loop iteration
}
}
• The first loop is called the outerloop, while the second loop is called the innerloop.
• Each iteration in the outerloop will cause the innerloop to run completely.
• It is important to mention we can nest the loop as much as we can, but it will increase
the complexity so it is not recommended.
10
Nested Loop
How many times the following code will iterate?
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
} 25 times
}
How many times the following code will iterate?
for (int i = 1; i <= 5; i++) {
}
10 times
for (int j = 1; j <= 5; j++) {
}
11
Example1 (Summation of numbers)
• Write a program that computes the summation from 1 to a number N entered from
the user
12
Example2 (Factorial)
• Write a program that computes the factorial of any number entered using loops
13
Example 3 (Power)
• Write a program that computes the result based on the entered base and power
from the user.
14
Example 4 (Grading)
• Write a program that enter 5 grades and then it should check if each grade has
passed or not. (use while loop)
15
Example 5 (Menu)
• Consider you have a menu with the following options (1. Burger $5.99, 2. Pizza -
$8.49, 3. Salad $4.25, 4. Exit and Pay) using do-while loop. Need to select an
option from the menu a message with “item is added” till the user ended his
choice with 4 (order complete). The total price of the selected items should be
printed.
16
Example 6 (Multiplication Table)
• Write a program that generates the multiplication table of a number entered by
the user
16
Example 7 (Draw Square)
• Write a program that draw a square in the form of stars. The height and the width
of the square depends on a number entered by the user.
17
Example 8 (Right angle Triangle)
• Write a program that computes the result based on the entered base and power
from the user?
18