Repetition Control Structure
Repetition Control Structure
REPETITION
CONTROL
STRUCTURES
REPETITION CONTROL
STRUCTURES
TWO MAIN REPETITION CONTROL
STRUCTURES IN PYTHON
01
02
A REVISIT IN C for LOOP
• initialization: This is the initial value of the loop control variable. It is executed only once,
before the loop starts.
• condition: This is the condition that is checked at the beginning of each iteration. If the
condition is true, the loop continues; otherwise, the loop ends.
• increment/decrement: This is the operation that is performed at
the end of each iteration to update the loop control variable.
• code to be executed: This is the block of code that is executed
repeatedly as long as the condition is true.
Python for Loop
Output:
Python for Loop with Python range()
•
•
Example: for loop using range()
Output:
Example: for loop using range()
What about this one?
Output:
using for loop, write a program that will display the output:
Group Activity
2. Using for loop, write a program that will ask the name of the user, and will
display the name 10 times.
Python while Loop
while condition
condition True while
3. condition
condition False
condition False
Example of while loop
Write a program that will print the numbers from 1 to 5.
Program
Process
1.the condition is i <= 5.
Output
2.The loop will continue to execute as long as
i is less than or equal to 5
3.Inside the loop, the value of i will be
printed,
4.then increment it by 1 using i += 1.
Group Activity
1. Write a program that asks the user to enter a number, and then prints out
all the even numbers from 0 to that number.
2. Write a program that asks the user to enter a password, and keeps
prompting them until they enter the correct password (which you can hard-
code in the program).
3. Write a program that asks the user to enter a number, and then prints out
the sum of all the digits in that number.
Group Activity
Average:
1. Write a program that generates a random number between 1 and 100, and
asks the user to guess the number. If the user's guess is too high or too
low, the program should give a hint and continue prompting the user until
they guess the correct number.
Group Activity
Difficult:
1. Write a program that asks the user to enter a sentence, and then prints out
the longest word in the sentence.
2. Write a program that asks the user to enter a series of numbers, and then
finds the largest and smallest numbers in the series. The user should be
able to enter as many numbers as they like, and the program should keep
prompting the user until they enter a special character (such as a negative
number) to signal the end of the series.
THANK YOU