Python supports three types of Loop statements. Those are While, For and Nested Loop. Loops also called iterations.
In my previous posts, I have added an example of how to Calculate the Area of Circle and Factorial ‘n’.
In this post, I have explained the loop control statements that present in Python and their syntax.
For example, to display (factorial n), you need a logic to execute as a loop and to print factorial n values.
Python Loop Control Statements
- For Loop
- While Loop
- Nested Loop
Python While Loop Flow Chart
While Loop Enters When Test Condition is True
A loop becomes an infinite loop if a condition never becomes FALSE.
The programmer must be cautious when using while loops because of the possibility that the test_condition never evaluates to a FALSE value
Python WHILE Loop Syntax
while test_condition: body of loop
Python FOR Loop Flow Chart
It checks items in the List. If Found, executes.
Python FOR Loop Syntax
for val in sequence: statement(s)
Syntax for Nested WHILE and FOR Loops
Python Nested While Loop
while test_condition: while test_condition: statement(s) statement(s)
Python Nested For Loop
for val in sequence: for val in sequence: statement(s) statement(s)
In the above nested loop syntax of both WHILE and FOR, you can observe a Loop within another Loop.