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

While Loop

Loop control statements like while and for loops are used to repeatedly execute a block of code. The while loop checks a condition and executes the code block if true, repeating until the condition is false. It can include an else block that runs once if the condition is never true. An infinite while loop runs continuously until a break statement exits the loop. Nested while loops allow looping within other loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views

While Loop

Loop control statements like while and for loops are used to repeatedly execute a block of code. The while loop checks a condition and executes the code block if true, repeating until the condition is false. It can include an else block that runs once if the condition is never true. An infinite while loop runs continuously until a break statement exits the loop. Nested while loops allow looping within other loops.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Loop Control Statements

Loop control statements are used when a section of code may either be
executed a fixed number of times, or while some condition is true.
• While
• For
while Loop
The while loop keeps repeating an action until an associated condition returns false.

Syntax: Python Interpreter checks condition,


If condition is True, then
while (condition):
Statement Execute statements written after
colon (:)
Rest of the Code
Condition ?

True False

Statement

Exit

Rest of the Code


while Loop with else
This repeatedly tests the condition and, if it is True, executes the Statement 1; if
the condition is False (which may be the first time it is tested) the Statement 2
of the else clause, is executed and the loop terminates. The else suite will be
always executed irrespective of the statements in the loop are executed or not.

Syntax:
while (condition):
Statement 1
else:
Statement 2
Rest of the Code
Condition ?

True False

Statement 1

Else

Statement 2

Exit

Rest of Code
Condition ?

True False

Statement 1

Else

Statement 2

Exit

Rest of Code
Infinite while Loop
Syntax:
while (True):
Statement
Rest of the Code

while (True):
Statement
if(condition):
break
Rest of the Code
Nested While Loop
while(condition):
Statements
while(condition):
Statements
Statements
Rest of Code

You might also like