0% found this document useful (0 votes)
14 views19 pages

Chapter 11 8

This chapter covers iterative statements in Python, focusing on loops that allow repeated execution of code until a specified condition is met. It explains the two main types of loops: for loops, which are used when the number of iterations is known, and while loops, which continue until a condition becomes false. Additionally, it discusses the range() function, membership operators, and the concept of infinite loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views19 pages

Chapter 11 8

This chapter covers iterative statements in Python, focusing on loops that allow repeated execution of code until a specified condition is met. It explains the two main types of loops: for loops, which are used when the number of iterations is known, and while loops, which continue until a condition becomes false. Additionally, it discusses the range() function, membership operators, and the concept of infinite loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

More About Python Chapter 11 Book 8

Learning Objectives

After studying this chapter, you will be able to:

 Know about iterative statements


 Know about infinite loop

Let’s Connect

In the previous chapter, we learnted learned about some concepts of


Python such as operators, modes, conditional statements, etc. In this
chapter, we will learn about iterative statements in Python.
Sometimes a program requires multiple executions of a statement. This can be done
with the use of loops. Loop helps us repeat one or more than one statement many times
without actually having to type it multiple times. This benefits the user as loops reduce
the number of instructions to be written for a program. Let's read about iterative or
looping statements.

ITERATIVE STATEMENTS
Iterative or looping statements are those statements whose execution
keeps on repeating till the given condition remains valid. A control
variable is essential for the working of a loop. The value of the variable
is increased or decreased with each statement until the final value is
reached. There are two types of iterative statements- for and while.
For Loop
We use the for loop statement when we know the number of times a
statement will be executed in a loop. This statement is also called the
definite loop.
Syntax:
for<variable> in<value>
loop (statements)
Where ‘in’ is a membership operator.

InfoByte

A control variable is also known as counter variable.

Membership Operators
Membership operators are important in controlling how a loop
executes a statement. ‘in’ and ‘not in’ are two membership operators
which are used in programming out of which, ‘in’ is the operator which
is primarily brought to use. Let’s see how the ‘in’ operator works:
‘in’ Operator: The function of the ‘in’ operator in a statement is to
determine the sequence of the given values. It gives output in the form
of ‘true’ and ‘false’ depending upon whether a given value is in
sequence or not.
Syntax:
‘value of variable’ in (sequence of values).

Example 1:
‘in’ and ‘for’ loop: In the following program the operator ‘in’ finds the value for the
variable ‘x’ in the range of the values given. When the program is executed, it
determines the presence of any value in the range. If a value is present then the ‘in’
operator assigns a value to the variable. When the program is executed, the values will
be displayed as output till the last value of the sequence is printed.

Example 2:
Range() Function
The range() function in Python creates a range of numbers in the form
of a list.
Syntax: range(starting value, final value, step value).
The starting value is an integer number which that defines the
starting position of the variable, the final value defines the ending
position and it is always less than the last value. The step value
defines an increment or decrement in value by one until the final value
is reached.
In the following program, we will specify a the starting value of the
variable and its final value. The program will determine whether the
given variable is a part of the range of numbers given or not. If the
variable is a part of the range, the output will be ‘true’ but if the
variable is not a part of the range then the output will be ‘false’.
Example 3:
Use of Range() Function with For Loop
We use the range() function with for loop to create an array of output.
An array is a data structure that holds the same type of data values.
The for loop statement assigns values present in the range to the
variable by executing the command in increasing order until the last
number of the range is reached.

Example 4:
Let’s have a look at a few more examples of using the range() function with for loop
command:

Example 5:
In the above program, the value of x is increased by two, each time the statement is
executed. The loop continues until the last number of the range incremented by 2 is
reached and assigned to the variable.
Example 6:

Here, the range of numbers is reversed from 30 to 0 and is decreased by 2 as the


statement is executed in the loop.

While Loop
While the loop keeps executing a statement till a condition is met.
Unlike for loop, here we don't need to know the number of times the
loop is to be executed beforehand. There can be a single statement or
a block of statements in a while loop.
Let’s discuss further about while loop:
Statements which that repeat another statement as long as the
condition remains valid are a part of the while loop.
A variable is assigned an initial value according to the condition
present. This variable is also known as a control variable.
It checks whether a statement is true according to the condition and
keeps executing it till the statement remains true.

Syntax:
while (condition)
statement
Let’s look at a few examples:
Example 1: In the following program the value 20 is assigned to the
variable x. The condition of the program is x <=50 with the value
increasing by 2. The program will continue to execute the statement
until the value of x becomes more than 50 and the statement becomes
untrue or invalid according to the condition.

Example 7:
Example 8: In the given program, the variable x is assigned the initial value 40 with the
condition being x<50. If the condition is valid then the statement given after print is
executed. The value of x increases by one each time the statement is executed.
Example 9: In the given program we find the sum of multiples of 4 between 30 and 60.
Then we multiply the sum by two and also increase the value of each multiple by one.
Example 10: The following program is to print the table of 5. It can be used to print the
multiplication table of any number by replacing the value assigned to the variable x.
Example 11: A statement in Python can be false according to the condition present in
the program and thus the loop terminates without printing the output. Observe the
given program for example:
Else Statement
The else statement is added along with for loop and while loop as a
method to check whether a loop has ended or terminated properly. It is
not essential for a program to run but acts as an added measure for
the execution of the program.

Program 12:
Let’s Revise

Answer the following questions orally:

1. What are loops in Python?

2. What is the function of 'in' operator?

INFINITE LOOP
This is an endless loop which that consists of statements that get
executed endlessly. This means that the condition present in the
program cannot become false and thus the program will keep on
running. This will continue unless the program is terminated.
The condition present in the following program is that the value of y
should be more than one, since the value keeps increasing by 1, the
value of y will always be more than one. So, the loop will keep
executing the program because the condition will never be false.

Example 14:
To terminate the loop press Ctrl + C. One such example of the infinite loop’s usage is the
client/ server program, where the server is required to endlessly wait and listen to the
clients when required.
Summary

 Loop helps us repeat one or more than one statement many times without
actually having to type it multiple times.
 Iterative or looping statements are those statements whose execution
keeps on repeating till the given condition remains valid.
 Membership operators are important in controlling how a loop executes a
statement. ‘in’ and ‘not in’ are two membership operators.
 The range() function in Python creates a range of numbers in the form of a
list.
 While the loop keeps executing a statement till a condition is met.
 The else statement is added along with for loop and while loop as a
method to check whether a loop has ended or terminated properly.
 Infinite An infinite loop is an endless loop which that consists of
statements that get executed endlessly.

Test Yourself

A. Multiple choice questions.

1. While loop in Python is used for what type of iteration?


(i) Indefinite (ii) Definite (iii) Intermediate
2. Which of the following is not a membership operator?
(i) in (ii) not in (iii) into

3. Which of the following statements is added to check the termination of a


loop?
(i) else (ii) otherwise (iii) if
4. ____________ is a data structure that holds the same type of values.
(i) Variable (ii) Operator (iii) Array
5. Which of the following loops have has a condition that is never met?
(i) Infinite loop (ii) Finite loop (iii) Definite Loop

B. Fill in the blanks.

• control variable • While • range () • membership operators • Infinite loop


1. There can be a block of statement in ____________ loop.
2. A ____________ is needed to control the execution of a looping statement.
3. 'in' and 'not in' are types of ____________.
4. ____________ is an endless loop.
5. A ____________ function creates a range of numbers in the form of a list.

C. State True or False.

1. Loops require the statement, to be executed, and be written


multiple times.
2. While loop is used when you know the number of times a
statement is to be executed.
3. The function of the ‘in’ operator in a statement is to determine the
sequence of the given values.
4. Ctrl + C key combination is pressed to terminate an infinite loop.
5. The syntax of the while loop is – while (condition).

D. Answer the following questions.

1. Write about the usage of loops in Python.


2. What are membership operators?
3. Why is the range () function used with for loop?
4. What is an infinite loop?
5. Differentiate between the working of for loop and while loop with examples.

E. Life Skills
Suhani wrote a program in Python. When she ran the program she noticed that it kept on running
and does not terminate. She wants to terminate the program to modify it, but doesn't know how.
Suggest to her the solution for her problem.

Activity Corner

Fun in Searching

Find and encircle 4 meaningful terms extracted from this chapter. See across, downwards, and
upwards.
W H I L E
O G
P Y T H O N
P A
R

Fun in Exploring

Explore information about the topic ‘How loops make programming easier’. Make a PowerPoint
presentation to explain the given topic.

Fun in the Lab

Go to the computer lab and do the following with the help of your
teacher:
• Write a program in Python that prints even numbers from 1 to 10.
• Write a program in Python to print a table of 2 using loops.
• Write a program displaying the increasing value of a variable by 4 till it reaches 40.

Art Integrated Activity

Make a chart showing different types of loops in Python.

Teacher’s Notes

 Recall the concept of Python taught in this chapter.


 Explain the concept of for loop and while loop to the students.
 Demonstrate the programs in the lab session for a better understanding of the concept taught.
 Ask questions to check the understandability of the students regarding the topic
covered.

You might also like