Page 309 - AI Ver 1.0 Class 9
P. 309
Brainy Fact
Comparison operators (<, >, <=, >=, !=, ==) and logical operators (and, or) returns True or
False.
The while Loop
The while loop is used to repeat a set of instructions as long as the condition is true. It means when the
number of iterations are not fixed/indefinite before we start with the execution of a loop. It is therefore known
as indefinite loop. Indentation of statements is must to specify the block of statements to be repeated using
while loop.
This loop is also called an entry-controlled loop as it checks for the condition in the beginning only. If the
condition is true then the body of the loop will be executed. If the condition is false then it will not be allowed
to enter within the loop and it stops. Syntax of the while loop is:
while <condition>
Statements
Where,
• while is a keyword.
• condition is a criterion to repeat the instructions. The instructions repeat till the condition is True. As soon as
the condition is False, the control exits from the loop.
• Statements are always indented can be single or a block and are repeated till the condition is True.
Brainy Fact
When there is no change of condition in while loop then it becomes infinite loop, For example,
i=1
while i<6:
print(i)
In the above code, the value of i will always be 1, and due to this, the condition will always be
True and will keep on executing the statement inside for an infinite number of times.
Different examples of while loop are:
Commands Output
count=1 hello
while count<=5: hello
print("hello") hello
count+=1 hello
print("Program ends") hello
Program ends
Introduction to Python 307

