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’.
FOR, WHILE and NESTED LOOPS 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.
- WHILE Loop
- Nested Loop
- For Loop
1. While Loop Flow Chart
While Loop Enters When Test Condition is True
Python WHILE Loop Syntax
while test_condition: body of loop
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
2. 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, WHILE and FOR nested-loops, you can find a Loop within the Loop.
3. FOR Loop Flow Chart
It checks items in the List. If Found, executes.
Python FOR Loop Syntax
for val in sequence: statement(s)