SSG Slide 8
SSG Slide 8
MICHEAL OGUNDERO
EMAIL – OGUNDEROAYODEJI@GMAIL.COM
LECTURE 8
MICHEAL OGUNDERO
EMAIL – OGUNDEROAYODEJI@GMAIL.COM
INTRODUCTION TO REPETITION STRUCTURES
Repetition structures, also known as loops, are fundamental in programming as they allow you to execute
a certain piece of code multiple times.
Looping statements are fundamental building blocks of programming, allowing you to execute code blocks
repeatedly until a specific condition is met. In Python, looping statements are primarily implemented using
the for and while loops. These statements enable you to automate repetitive tasks, iterate over sequences
of data, and control the flow of your program based on specific conditions.
LOOPING STATEMENT
Looping statements are fundamental building blocks of programming, allowing you to execute code blocks
repeatedly until a specific condition is met. In Python, looping statements are primarily implemented using
the for and while loops. These statements enable you to automate repetitive tasks, iterate over sequences
of data, and control the flow of your program based on specific conditions.
THE FOR LOOP
The for loop is used to iterate over a sequence of items, executing the code block within the loop for
each item in the sequence. The general syntax of the for loop is as follows:
For instance:
numbers = [1, 2, 3, 4, 5]
In this example, the for loop iterates over the list numbers, assigning each element of the list to the
variable num in each iteration. The code block within the loop prints the value of num, effectively printing
each number in the list.
LOOPING THROUGH STRINGS
Strings are iterable objects, they contain a sequence of characters. We can loop through the letters in a
word.
For instance:
To loop through a set of code a specified number of times, we can use the range() function
The range() function in Python is a built-in function used to generate a sequence of numbers for use in
loops. It's a powerful tool for iterating a specific number of times or within a specific range. Here's how it
works:
Parameters:
The else keyword in a for loop specifies a block of code to be executed when the loop is finished
for x in range(6):
print(x)
else:
print("Finally finished!")
Note: The else block will NOT be executed if the loop is stopped by a break statement.
THE WHILE LOOP
The while loop is used to repeat a code block as long as a specified condition is true. The general syntax
of the while loop is as follows:
while condition:
code block to execute as long as the condition is true
For instance:
num = 1
In this example, the while loop checks the condition num < 6 before each iteration. As long as the
condition is true, the code block within the loop prints the value of num and increments it by 1. Once the
condition becomes false (when num reaches 6), the loop terminates.
THE WHILE LOOP WITH ELSE BLOCK
With the else statement we can run a block of code once when the condition no longer is true:
For instance:
i=1
while i < 6:
print(i)
i += 1
else:
print("i is no longer less than 6")
DIFFERENCES BETWEEN FOR AND WHILE LOOPS
The primary difference between for and while loops lies in their control mechanisms. The for loop
iterates over a predefined sequence of items, advancing to the next item in each iteration. The while loop,
on the other hand, repeats the code block as long as a specified condition is true, relying on the condition
to control the loop's termination.
In a nutshell, use for loops when you have a well-defined sequence of items to iterate over, and use while
loops when you need to repeat code based on a dynamic condition that may change during the loop's
execution.
REAL LIFE APPLICATIONS OF LOOPING STATEMENTS
Looping statements find numerous applications in real-world scenarios, ranging from simple tasks to
complex computations. Here are a few examples:
• Processing lists of data: Looping through lists of data is a common task in programming, such as
iterating over a list of customer names or processing a list of sensor readings.
• Performing repeated calculations: Looping statements are often used to perform repetitive
calculations, such as calculating the sum of a series of numbers or applying a formula to multiple data
points.
• Automating repetitive tasks: Looping statements can automate repetitive tasks, such as sending email
notifications to a group of users or generating reports based on large datasets.
• Controlling user interactions: Looping statements are used in interactive applications to handle user
input and provide continuous feedback, such as updating a game screen or responding to user
actions in a graphical interface.
NESTED STATEMENTS: LOOPS
Nested statements are a powerful tool in programming that allows you to embed one control flow
statement within another. This enables you to create more complex decision-making logic and control the
flow of your program in a hierarchical manner.
Nesting Loops
Nesting loops involves placing a for or while loop within another loop. This allows you to iterate over
sequences of data within another loop's iteration. Consider the following nested for loops:
Here, the outer for loop iterates over the rows, and for each row, the inner for loop iterates over the
columns. This effectively prints the coordinates of a 3x5 grid.
ORDER OF EXECUTION IN NESTED STATEMENTS
The order of execution in nested statements follows a hierarchical pattern. Inner statements are
executed within the context of their outer statements. For nested loops, the inner loop completes its
iterations for each iteration of the outer loop. For nested conditionals, the inner conditional checks its
conditions only if the outer conditions are satisfied.
Indentation plays a crucial role in managing nested statements in Python. It visually depicts the hierarchical
structure of nested statements, ensuring clear readability and maintaining proper execution order. Each
level of nesting adds an indentation level, aligning code blocks accordingly.
NESTED STATEMENTS IN REAL-WORLD APPLICATIONS
Nested statements are frequently used in real-world programming scenarios to handle complex scenarios
and decision-making logic. Here are a few examples:
• Game Development: Nested statements are used in game development to control character
movement, manage game levels, and handle user interactions dynamically.
• Data Processing: Nested statements are often used in data processing tasks to filter, manipulate, and
analyze data according to specific criteria.
• User Interface Design: Nested statements are employed in user interface design to create interactive
elements, respond to user input, and update graphical components accordingly.
Nested statements provide a powerful mechanism for creating complex control flow logic in Python,
enabling programmers to handle intricate decision-making and repetitive tasks efficiently.
End of Lecture 8