Python supports three types of Loop statements. Those are While Loop, For Loop and Nested Loop. Loops also called iterations. For example to display !n (factorial n) you need control statements to be executed in a loop.
In my other blog posts, I have added examples on how to Calculate area of Circle and Factorial ‘n’.
The key aim of this blog is to know what are the top control statements present in Python and their syntax.
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 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
Checks Items in the List. If Found, executes Statements
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.