Nested Loops:
- Defining a loop inside another loop.
- Inner loop condition evaluates only when outer condition is valid.
- Execution terminate only when outer condition fails.
Nested while loop codes:
- The following code snippets are used to analyze and understand the flow of nested loop.
- If outer loop is true, then only control checks inner loop condition.
- Inner loop executes as long as condition is valid before the second iteration of outer loop.
Snippet-1: Outer loop execute once and inner loop infinite times as condition always true.
while True:
print("Hi")
while True:
print("Hello")
Snippet-2: Loop executes many times where as block executes only once. Inner block executes once every time control enters into loop with outer condition.
while True:
print("Hi")
if True:
print("Hello")
Check the Output and analyze the code:
i=1
while i<=5:
print("Outer i :",i)
i=i+1
j=1
while j<=3:
print("Inner j :",j)
j=j+1