JD05 - Loops Presentation Slides
JD05 - Loops Presentation Slides
Control Flow
Statements
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
0 1 2 3 4 5 6 7 8 9 10
For Loop – Part 2
Practice-1 Different Start & End
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.
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.
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.
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
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
* * * * * *
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
codes to repeat
(body)
Practice-1 While Loop
0 1 2 3 4 5 6 7 8 9 10
“for” Loop vs “while” Loop
true
boolean expression
false
“do - while” Loop Syntax
do {
statement(s)
// statements
true
boolean expression } while(boolean expression);
false
condition
Practice-2 Do While Loop
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
Begin
number = 0
false
“for” loop
If number < 11 End
number = number + 1
Practice-4
Which loop to choose?
Write a program to find the largest number in a list of numbers.
Begin
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.
Begin
“do-while”
sum = 0
Do you want
Yes
Print sum
to quit?
End
Branching Statements Part-1
Branching Statements
Control Flow
Statements
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”
A “continue” statement1;
statement2;
statement is used to continue;
skip the remaining statement3;
Branching
Statements