0% found this document useful (0 votes)
43 views21 pages

Flow of Control: Loops: Introduction To Programming and Problem Solving

The document covers loop control structures in programming, specifically while loops that repeat a block of code while a condition remains true, for loops that iterate over a sequence like a list or string, and nested loops where an inner loop executes each time the outer loop iterates. It includes Python examples of while
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views21 pages

Flow of Control: Loops: Introduction To Programming and Problem Solving

The document covers loop control structures in programming, specifically while loops that repeat a block of code while a condition remains true, for loops that iterate over a sequence like a list or string, and nested loops where an inner loop executes each time the outer loop iterates. It includes Python examples of while
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

Introduction to Programming and Problem Solving

Flow of Control: Loops


Loop Control Flow Statements
• This is a control structure that allows the execution
of a block of statements multiple times until a loop
termination condition is met.
• A portion of a program that repeats a statement or
a group of statements is called a loop.
• The statement or group of statements to be
repeated is called the body of the loop.
• Loop Control Flow Statements are also called
Repetition statements or Iteration statements.
Python Loop Statements

• The while statement


• The for Statement
The while Statement

• Also called a while loop


• A while statement repeats while a
controlling boolean expression remains true
• The loop body typically contains an action
that ultimately causes the controlling
boolean expression to become false.
The while Statement (cont.)
Syntax:
The while Statement (cont).
Syntax:

while Boolean_Expression:
Body_Statement

or

while Boolean_Expression:
First_Statement
Second_Statement

The while Statement (cont).
Semantics of the while statement:
1. The Boolean expression is
evaluated before the statements in
the while loop block is executed.
• If the Boolean expression
evaluates to False, then the
statements in the while loop block
are never executed.
• If the Boolean expression evaluates to
True, then the while loop block is
executed.
2. After each iteration of the loop block,
the Boolean expression is again
checked, and if it is True, the loop is
iterated again.
• This process continues until the Boolean
expression evaluates to False and at this
point the while statement exits.
Sample Program using
while Statement
Write a Python program to display the
first 10 numbers using while loop starting
from 0.

i=0
while i < 10:
print(f"Current value of i is {i}")
i=i+1
Sample Program using
while Statement (cont).
Write a Python program that reads a number (num).
Find and display all odd numbers and the sum of these
numbers from 1 to num using while loop.

num = int(input("Enter a number: "))


count = 1
sum = 0
while count <= num:
    print(count)
    sum = sum + count
    count = count + 2
print(f"The sum of all numbers is {sum}")
Practice Exercise:
1. Write a Python program using while
statement that will ask the user to
enter an integer number. Find and
display the factorial of the number.

2. Write a Python program using while


statement that will find all numbers
divisible by 8 from 101 to 201 and
display the sum of these numbers.
The for Statement
• A for loop is used for iterating over a
sequence (that is either a list, a tuple, a
dictionary, a set, or a string).

• With the for loop we can execute a set of


statements, once for each item in a list, tuple,
set, string, etc.

• The for loop does not require an indexing


variable to set beforehand.
The for Statement (cont.)
Syntax:
The for Statement (cont.)
Looping through a List (example):

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)

Looping through a String (example):

for x in "banana":
  print(x)
The for Statement (cont.)
The break Statement
• With the break statement we can stop the loop
before it has looped through all the items:

Example 1: Exit the loop when x is “banana”

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  print(x)
  if x == "banana":
    break
The for Statement (cont.)
The break Statement (cont.)

Example 2: Exit the loop when x is "banana",


but this time the break comes before the print

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    break
  print(x)
The for Statement (cont.)
The continue Statement
• With the continue statement we can stop the
current iteration of the loop, and continue with
the next:

Example: Do not print “banana”

fruits = ["apple", "banana", "cherry"]
for x in fruits:
  if x == "banana":
    continue
  print(x)
The for Statement (cont.)
The range() function
• The range() function generates a sequence of
numbers which can be iterated through using for
loop
• The range() function returns a sequence of
numbers, starting from 0 by default, and increments
by 1 (by default), and stops at a specified (but not
included) number.
• Syntax:
range([start ,] stop [, step])
Both start and step arguments are optional and the range
argument value should always be an integer.
range() function: Examples
• Using the stop argument
for x in range(10):
  print(x)

• Using the start and stop arguments


for x in range(1, 10):
  print(x)

• Using the start, stop, and step arguments


for x in range(1, 10, 2):
  print(x)
Else in For Loop
• The else keyword in a for loop specifies a
block of code to be executed when the
loop is finished.
• Example:
sum = 0
for x in range(1, 10, 2):
sum = sum + x
print(sum)
else:
print("Finally finished!")
Nested Loops
• A nested loop is a loop inside a loop.
• The "inner loop" will be executed one time
for each iteration of the "outer loop“.
• Example:
adj = ["red", "big", "tasty"]
fruits = ["apple", "banana", "cherry"]

for x in adj:
for y in fruits:
print(x, y)
Practice Exercise
1. Write a Python program using for
statement that will display all even
numbers from 21 to 59.

2. Write a Python program using for


statement that will calculate and
display the product of all integer
numbers from 13 to 31 .

You might also like