Page 137 - TechPluse_C8_Flipbook
P. 137
THE WHILE STATEMENT
The while statement executes a set of statements repeatedly, until the logical expression evaluates
to true. When the condition becomes false, the control comes out of the loop. The syntax of
while statement is given below.
Syntax:
False
Test Expression
while (test expression):
Statements
True
increment/decrement
Execute while Block
Program 13: To find the sum of first 5 natural numbers.
On running the above program, we get the following output:
Tech
Funda
Any non-zero value in the while loop indicates always a true
condition, whereas zero indicates always-false condition.
Infinite Loop
If the condition given in a loop never becomes false, then the loop will never terminate and run
indefinitely. This type of loops is called an infinite loop.
Example:
while(1)
print(“Hello”)
Output: Hello will be printed infinite times.
To come out of the infinite loop, we can either close the program window or press Ctrl + C. This
will break the running of the program. In fact, at any time during the execution of a program in
Python, you can press the key combination Ctrl + C to stop running the program.
Conditional and Looping Statements in Python 135

