0% found this document useful (0 votes)
80 views

CL-8 IT CH-8 Iterative Statements in Python (2)

Uploaded by

Ruchi Kalra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
80 views

CL-8 IT CH-8 Iterative Statements in Python (2)

Uploaded by

Ruchi Kalra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

MOUNT CARMEL SCHOOL, CHANDIGARH

SESSION: 2021 – 2022

SUBJECT – IT

CLASS – VIII

CHAPTER – 8
(ITERATIVE STATEMENTS IN PYTHON)
A while loop statement in Python
programming language repeatedly
executes a target statement as long as a
given condition is true.
Components of while loop;
1.Initialization
2.Condition/Text Expression
3.Loop Body
4.Step Value
Syntax
The syntax of a while loop in Python programming
language is −
while expression: statement(s)
Here, statement(s) may be a single statement or a
block of statements. The condition may be any
expression, and true is any non-zero value. The loop
iterates while the condition is true.
When the condition becomes false, program control
passes to the line immediately following the loop.
In Python, all the statements indented by the same
number of character spaces after a programming
construct are considered to be part of a single block
of code. Python uses indentation as its method of
grouping statements.
Flow Diagram

Here, key point of the while loop is that the loop might not ever run.
When the condition is tested and the result is false, the loop body will
be skipped and the first statement after the while loop will be executed.
Example
count = 0
while (count < 9):
print 'The count is:', count
count = count + 1
print "Good bye!“

When the above code is executed, it produces the following result −

The count is: 0


The count is: 1
The count is: 2
The count is: 3
The count is: 4
The count is: 5
The count is: 6
The count is: 7
The count is: 8
Good bye!
The Infinite Loop
A loop becomes infinite loop if a condition never
becomes FALSE. You must use caution when using
while loops because of the possibility that this
condition never resolves to a FALSE value. This
results in a loop that never ends. Such a loop is
called an infinite loop.
An infinite loop might be useful in client/server
programming where the server needs to run
continuously so that client programs can
communicate with it as and when required.
Syntax:
var = 1
while var == 1 :# This constructs an infinite loop
num = raw_input("Enter a number :")
print "You entered: ", num
print "Good bye!"
THANK YOU

You might also like