0% found this document useful (0 votes)
9 views63 pages

JD05 - Loops Presentation Slides

Uploaded by

wapete4789
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)
9 views63 pages

JD05 - Loops Presentation Slides

Uploaded by

wapete4789
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/ 63

Loops Part-1

For Loop Part-1


Looping Statements

Control Flow
Statements

Selection Looping Branching


Statements Statements Statements

if statement for loop break

if-else statement while loop continue

switch statement do-while loop return

Conditional Statements
Loops

Loops allow us to
execute code again and
again without the need
for manual repetition
as long as a specified
condition remains true. for loop
while loop
do-while loop
“for” Loop

Each execution
“for” loop allows of the loop body Begin
repeated execution is called an
iteration. initialization
of code block based
on a certain number false
boolean
of iterations. expression
End

true
iteration
statements

iterator
“for” Loop Syntax
counter
declaration condition

Begin

initialization

boolean false
End
expression for (initialization; boolean expression; iterator) {
true // statements
statements }

iterator

updating counter
to make the codes to repeat
condition false ( body )
Practice-1 Print Numbers

Print the numbers which starts from 0 and ends at 10 (inclusive).

0 1 2 3 4 5 6 7 8 9 10
For Loop – Part 2
Practice-1 Different Start & End

1. Print the numbers which start from 5 and end at 20 (inclusive)


in the same line with a space between the numbers.

2. Print the numbers which start from 20 and ends at 10 (exclusive).

3. Print the numbers which start from 10 and ends at 100 (inclusive)
with an increment of 10.

4. Print the numbers which start from 50 and ends at 10 (exclusive) with
a decrement of 4.

5. Print "Hello" five times.


Key Takeaways

Loops are used to automate repetitive tasks.

for loop should terminate after a specified number of iterations.


Lab 1
Task-1 Even Numbers

Print the even numbers which starts from 0 and ends at 10 (inclusive)
in the same line with a space between the numbers.

Begin

number = 0
2 4 6 8 10
false
If number < 11 End

true

Print number

number = number + 2
Task-2 Odd Numbers

Print the even numbers which starts from 1 and ends at 19 in the
same line with a dash between the numbers.

Begin

number = 1 1-3-5-7-9-11-13-15-17-19

false
If number <= 19 End

true

Print number

number = number + 2
Lab 2
Task-1 Sum of Even and Odd Numbers

Calculate the sum of even and odd numbers between 1 and 100
(exclusive) then print the result as below.

Sum of even number = 2450


Sum of odd number = 2500
Lab 3
Task-1 Square of Numbers
Print the square of numbers which starts from 1 and ends at 10
as below.

Number | Square of Number


------------------------
1 | 1
2 | 4
3 | 9
4 | 16
5 | 25
6 | 36
7 | 49
8 | 64
9 | 81
10 | 100
Lab 4
Task-1 KPH to MPH

Print KPH & MPH conversion table like you see


on the right side. KPH MPH
-------------
Your friend Mike Smith just rented a European sports car. 20 12
Mike lives in the USA, and he is afraid he will get a 30 18
speeding ticket because the car’s speedometer indicates 40 24
50 31
kilometers per hour (KPH). He has asked you to write a
60 37
program that displays a table of speeds in kilometers per 70 43
hour with their values converted to miles per hour (MPH) 80 49
90 55
(1 mile = 0.62 kilometer).
100 62
110 68
The table that your program displays should show speeds
120 74
from 20 kilometers per hour thru 140 kilometers per 130 80
hour, in increments of 10, along with their values 140 86
converted to miles per hour.
Lab 5
Task-1 Multiplication Table
Write a program that prints the multiplication table for any
given number.
Example-1: Example-2
Input: 7 Input: 10
Output: Output:

Multiplication table for 7 Multiplication table for 10


--------------------------- ---------------------------
7 x 1 = 7 10 x 1 = 10
7 x 2 = 14 10 x 2 = 20
7 x 3 = 21 10 x 3 = 30
7 x 4 = 28 10 x 4 = 40
7 x 5 = 35 10 x 5 = 50
7 x 6 = 42 10 x 6 = 60
7 x 7 = 49 10 x 7 = 70
7 x 8 = 56 10 x 8 = 80
7 x 9 = 63 10 x 9 = 90
7 x 10 = 70 10 x 10 = 100
Loops Part-2
“for” Loop Rules
Important Rules of “for” Loop

Avoid infinite loops which execute the loop body


infinitely.

Multiple variables of same data type can be initialized


and updated in control block.

We can re-assign a variable in the initialization block.

Variables initialized inside the initialization block are


not accessible out of the loop.
Nested For Loop
Nested Loops

A nested loop is a loop


that is defined inside the
body of another loop.
This allows us to
perform repeated actions
within the context of
another repeated action.
for loop
while loop
do-while loop
Nested For Loop
// outer loop
for (initialization1; condition1; iterator1) {
// inner loop
for (initialization2; condition2; iterator2) {
// statements
}
}
Begin

initialization1
Inner loop
true false
initialization2 condition1 End

true
statements condition2 iterator1
false

iterator2
Outer loop
Practice-1 Nested Loops
Write a program that prints the statements as below using nested
loops and debug it.

Outer Loop Iteration 1


i = 1 | j = 1
i = 1 | j = 2
i = 1 | j = 3
Outer Loop Iteration 2
i = 2 | j = 1
i = 2 | j = 2
i = 2 | j = 3
Outer Loop Iteration 3
i = 3 | j = 1
i = 3 | j = 2
i = 3 | j = 3
Outer Loop Iteration 4
i = 4 | j = 1
i = 4 | j = 2
i = 4 | j = 3
Real Life Examples Of Nested Loops

Multiplication table
Car odometer
Calendar Generation

Clock
Patterns with two or more dimensions like
checkerboard, pyramid, diamond, etc.
Sorting a list
Image processing
Grid-based games or simulations
Practice-2 Number Triangle

Write a program that prints the number triangle which starts from
1 and ends at given number.
Example:
Input: 5
Output:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Key Takeaways

Follow rules of “for” loop.

Avoid infinite loops.

Nested loop is used to solve complex problems.


Lab 1
Task-1 Square Pattern
Write a program that prints the square pattern which has the
same number of stars and number of rows with any given number.
Hint: use two spaces between the stars.
Example:
Input: 6
Output:

* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
Task-2 FIZZBUZZ pattern
Write a program that generates and prints the FIZZBUZZ pattern starting from 1
to a given number (inclusive).
The FIZZBUZZ pattern is a sequence of numbers where certain numbers are replaced with
specific words. The pattern follows these rules:
If a number is a multiple of 3 (but not 5), replace it with "FIZZ"
If a number is a multiple of 5 (but not 3), replace it with “BUZZ"
If a number is a multiple of both 3 and 5, replace it with " FIZZBUZZ"
All other numbers remain unchanged.
Example:
Input: 20 Output:
1 2 FIZZ 4 BUZZ FIZZ 7 8 FIZZ BUZZ
11 FIZZ 13 14 FIZZBUZZ 16 17 FIZZ
19 BUZZ
Lab 2
Task-1 Pattern

Write a program that prints the pattern like below for any given
number.
Example:
Input: 7
Output:

##
# #
# #
# #
# #
# #
# #
Task-2 Fibonacci Series
Write a program that generates and prints the Fibonacci series up to a
specified number of terms.
The Fibonacci series is a sequence of numbers where each number is the sum of
the two preceding ones. It starts with 0 and 1, and each subsequent number is
the sum of the two previous numbers. The series continues indefinitely.
Example:
Input: 8
Output:

0 1 1 2 3 5 8 13
Loops – Part 3
“while” & “do - while” Loops
“while” Loop

“while” loop
repeatedly
executes the same statement(s)
true
condition
set of statements
as long as false
condition is
Condition is
true. checked before
each iteration
of the loop.
“while” Loop Syntax

condition

while (boolean expression) {


true // statement(s)
statement(s) boolean expression
}
false

codes to repeat
(body)
Practice-1 While Loop

Print the numbers which starts from 0 and ends at 10 (inclusive).

0 1 2 3 4 5 6 7 8 9 10
“for” Loop vs “while” Loop

initial value condition


(boolean expression)

for (int i = 1; i < 5; i++) { int number = 1;


System. out .println("Hi"); while (number < 5) {
} System. out .println("Hi");
number++;
}

Statements update of condition


(body)
“do - while” Loop

The code block


do - while loop is is executed
another type of first, and then
loop that is similar the condition is
checked.
to the while loop,
but with a slight
difference. statement(s)

true
boolean expression

false
“do - while” Loop Syntax

codes to repeat (body)

do {
statement(s)
// statements
true
boolean expression } while(boolean expression);

false

condition
Practice-2 Do While Loop

Print the numbers which starts from 0 and ends at 10 (inclusive).

0 1 2 3 4 5 6 7 8 9 10
Lab 1
Task-1 Square Pattern
Write a program (using “while” loop) that prints the square pattern
which has the same number of stars and number of rows with any
given number.
Hint: use two spaces between the stars.
Example:
Input: 6
Output:
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
Task-2 FINRA pattern
Write a program (using “do-while” loop) that generates and prints the FINRA
pattern up to a given number.
The FINRA pattern is a sequence of numbers where certain numbers are replaced with
specific words. The pattern follows these rules:
Numbers divisible by 3 are replaced with "FIN"
Numbers divisible by 5 are replaced with "RA"
Numbers divisible by both 3 and 5 are replaced with "FINRA"
All other numbers remain unchanged
Example:
Input: 20 Output:
1 2 FIN 4 RA FIN 7 8 FIN RA 11 FIN
13 14 FINRA 16 17 FIN 19 RA
“for” Loop vs “while” Loop
“for” Loop vs “while” Loop

Most of the time loops can be used


interchangeably

If the number of iteration is fixed, use “for” loop

If the number of iteration is not fixed, use “while” loop

If the number of iteration is not fixed and you need to


execute the loop body at least once, use “do-while” loop
Practice-3
Which loop to choose?
Print the even numbers which starts from 0 and ends at 10 (inclusive)
in the same line with a space between the numbers.

Begin

number = 0

false
“for” loop
If number < 11 End

true the number of iteration is fixed


Print number

number = number + 1
Practice-4
Which loop to choose?
Write a program to find the largest number in a list of numbers.

Begin

maximum_number = first number of the list

No
“while” loop
If there is next Print
number maximum_number

Yes
The number of iteration is
No next_number > End not fixed, and we need to
maximum_number
check the condition before
Yes executing the loop body.

maximum_number = first number of the list


Practice-5
Which loop to choose?
Find the sum of numbers until the user quits.

Begin

“do-while”
sum = 0

The number of iteration is


Read the number not fixed, and we need to
execute the loop body at
least once.
sum = sum + number
No

Do you want
Yes
Print sum
to quit?

End
Branching Statements Part-1
Branching Statements

Control Flow
Statements

Selection Looping Branching


Statements Statements Statements

if statement for loop break

if-else statement while loop continue

switch statement do-while loop return

Conditional Statements
“break”

A “break” statement
is used to terminate while (condition) {
the execution of a loop statement1;
or switch statement statement2;
and continues with break;
the next statement statement3;
statement4;
}
statement5;
Jumps out of
the loop
“continue”

for (initialization; condition; iterator) {

A “continue” statement1;
statement2;
statement is used to continue;
skip the remaining statement3;

code inside a loop statement4;


}
iteration and move to statement5;
the next iteration
do { These statements are skipped
statement1; for current iteration
statement2;
continue;
statement3;
statement4;
} while (condition);
statement5;
Labeled Statements
Labeled statements are used to provide a way to identify and refer
to a specific statement within nested loops to “break” out of or
“continue” to this statement.

Loops Without Label Loops With Label

for (initialization; condition; iterator) { label1:


statement1; for (initialization; condition; iterator) {
for (initialization; condition; iterator) statement1;
{ for (initialization; condition; iterator) {
statement2; statement2;
break; break label1;
statement3; statement3;
} }
statement4; statement4;
} }
Branching Statements Part-2
Branching Statements

Branching
Statements

break continue return

Terminates the Skips the


execution of a loop remaining code
Exits a method and
or switch inside a loop
can also return a
statement and iteration and
value
continues with the moves to the next
next statement iteration
“return”

These statements are not


executed.

public static void main(String[] args) {


public static void main(String[] args) {
do {
statement1;
statement1;
statement2; statement2;
return; return;
statement3;
statement3;
statement4;
statement4;
} while (condition);
} statement5;
}
Key Takeaways

Loops provide a way to automate repetitive tasks and


perform iterative operations.

Most of the time loops can be used interchangeably.

Branching Statements are powerful tools for manipulating


the execution flow and implementing specific logic.

You might also like