Lecture 8 - 2 Loops

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 21

Lecture 8_2: Loops

Eng. Emanuel Rashayi


Faculty of Engineering, Dept of Electrical & Electronics Engineering , University of Zimbabwe
erashayi@eng.uz.ac.zw
Python 3 Loops - For / For-Else
The for statement is used whenever you want to iterate over a sequence and
execute a piece of code for all or some elements of that sequence - list or string, or
whatever sequence you have
Python 3 Loops - For / For-Else
Python 3 Loops - For / For-Else
Python 3 Loops - For / For-Else
What if we want to iterate over a list using list indexes?
Python 3 Loops - For / For-Else
using the enumerate() function in Python.
Python 3 Loops - For / For-Else
➢ you can also use an else statement with a for loop.
➢ The indented code below else will be executed only when the for
loop has finished iterating over the entire sequence
Python 3 Loops - While / While-Else

➢ Well, unlike a for loop, which executes a code block a number of


times, depending on the sequence it iterates over, a while loop
executes a piece of code as long as a user-defined condition is
evaluated as True.
➢ If the specified condition does not change, meaning it doesn't become
False, then the while loop will continue running forever and we end up
with an infinite loop.
Python 3 Loops - While / While-Else
Python 3 Loops - While / While-Else

➢ One last thing on while loops is that we can use the else statement
again, this time with a different meaning: the indented code below the
else clause will be executed only when the condition specified in the
while statement becomes False..
Python 3 Loops - While / While-Else
Python 3 Nesting - If / For / While

➢ You can use nesting with control flow statements like if, for and while,
to enable a certain behavior and logic inside your Python program.
➢ Think of nesting as using an if statement inside another if statement,
a for loop inside another for loop or a while loop within another
while loop.
➢ Lets look at examples:
Python 3 Nesting - If / For / While
Python 3 Nesting - If / For / While
Python 3 Nesting - If / For / While
Python 3 Nesting - If / For / While

➢ Lets look at the nested while structure:

What is the Result ?


Python 3 Nesting - If / For / While

➢ Lets look at nesting a structure inside a different structure.


➢ E.g nesting an if statement inside a for loop:
Python 3 - Break, Continue, Pass
➢ The break and continue statements are used to handle the flow of a
while or a for loop in a Python program, meaning the programmer can
interrupt or restart the execution of a loop structure, in certain
conditions.

break tells Python to stop the execution of


the loop right here, ignore the print function
below and completely quit this for loop
Python 3 - Break, Continue, Pass

➢ what if we have a for loop nested inside another for loop?


Python 3 - Break, Continue, Pass
➢ When Python stumbles upon a continue statement inside a for or while
loop, it ignores the rest of the code below, for the current iteration, goes
up to the top of the loop and immediately starts the next iteration.
Python 3 - Break, Continue, Pass
➢ Pass is the equivalent of "do nothing". It is actually just a placeholder, for
whenever you want to leave the addition of a piece of code for later and
move on to write other segments of your program.

You might also like