Week 9 - Loop _ Nested Loop
Week 9 - Loop _ Nested Loop
Week 9
Iteration in Python
Introduction to Iteration
Outline While Loops
Practice I
For Loops
Practice II
Keywords
Nested Loop
Outline
Nested While
(cont'd)
Practice III
Practice IV
Submission
References
Introduction to Iteration
What is Iteration is a repeating process to
generate outcomes
Iteration? Iteration is also called loop. Loop is
a programming structure that
repeats a sequence of instructions
until a specific condition is met,
usually until a variable reach certain
value.
Example Of Iteration In Real Life
You are playing basketball. Then your coach asking you to score the hoops 5 times. This
means, you will repeatedly shooting the ball until you can score 5 times.
While Loops
While Loops is an iteration method that will repeat
the code inside based on its condition / expression.
You are given a task to create a 1. Take the input from the user and store it into a variable
program to count up starting 2. Create a while loop with condition the number is less or
from number inputed by the user
equal to 10
until 10. This means, when the
number is above 10, the program 3. While the loop is running, print out the number value
will be terminated
4. Increment the value of the variable by 1
Result
Be Careful!
There is something called Infinite
loop. Infinite loop can happen
because of the condition of the loop
is always true (will never change to
false)
This code is the example of infinite loops, because the
number value will always be 5 (always less than 10)
Practice I
For student whose last digit of student id is odd
Recreate previous program, by changing the maximum number is 15 , and the output will have your name
similar to the following picture
For student whose last digit of student id is even
Recreate previous program, by changing the maximum number is 8 , and the output will have your last 2
student digits, similar to the following picture
For Loops
For Loops is used for iterating over a sequence.
Different than while loops, for loops by default has an
increment of the condition variable by 1. So if we
want to have an increment by 1 we don’t need to do
anything, but if we want the increment by other
values, we can define it as our liking
range()
range() is a function used in for loops to iterate over numbers inside the function. range() function can
hold 1, 2, and 3 parameters.
● range(x) [with 1 parameter] means we will iterate from 0 to x-1, with increment by 1
● range(x,y) [with 2 parameters] means we will iterate from x to y-1, with increment by 1
● Range(x,y,z) [with 3 parameters] means we will iterate from x to y-1, with increment by z
*X: Stop
Example of range(x)
Source Code
Output
*X: Start , Y: Stop
Output
*X: Start , Y: Stop , X: Step
Example of range(x,y,z)
Source Code
Output
Keywords
break
break keyword is used to forcefully exit the loop even though the condition is still true
Loop?
Loop can be nested in python. When a
loop is nested inside another loop, the
inner loop runs many times inside the
outer loop. In each iteration of the outer
loop, the inner loop will be re-started.
Nested While Loop
Nested While
Loop
is facing down.
Steps to solve (Algorithm)
1. Create a variable for the outer long
Output
Source Code
Practice III:
For student who have the last digit of student ID is odd, create a code to generate output as picture 1.
For student who have the last digit of student ID is even, create a code to generate output as picture 2.
Picture 1 Picture 2
8 lines 10 lines
Nested For Loop
Nested For Loop
For student who have the last digit of student ID is even, create a code to generate output as picture 2.
Picture 1 Picture 2
All loop types can be used inside other
loops with different types too
For loop can be used inside while loop, and vice versa
Additional Practice
(Optional)
Optional Excercise
Create a program to do login validation with username and password. In this case, the correct
username and password are admin. If the password and username are not correct and not
matched, it will do an iteration output to do login attempts (Max 3 times). This exercise will
need a combination between conditional statements and looping.
*Note: You will need to learn the break statement to help this exercise.
You may have different output but make sure you use looping to do this exercise.
Optional Exercise
Expected Output
Wrong Answer!
Correct Answer!
Submission
Submission
Screenshot your codes and terminals, put them into pdf file. Don’t forget to put your name and
student id in the top of the file.
Practice I->Screenshot your program code and output depending on your student id last digit
Practice II->Screenshot your program code and output depending on your student id last digit
Practice III->Screenshot your program code and output depending on your student id last digit
Practice IV->Screenshot your program code and output depending on your student id last digit
Questions?
References
Python Loops - For, While, Nested Loops With Examples (softwaretestinghelp.com)
Python While Loops
Python For Loops