Geek University

Nested loop statements

You can place a loop statement inside another loop statement. This process is called nesting. Consider the following example:

x = int(input('Enter a number: '))

while x != 0:
    for y in range (1, x):
        print (y)
        y+=1
    x = int(input('Enter a number: '))

The simple example above asks the user to enter a number and then prints all numbers less than that number. The process will continue until the user enters 0:

>>> 
Enter a number: 5
1
2
3
4
Enter a number: 12
1
2
3
4
5
6
7
8
9
10
11
Enter a number: 0
>>>

Subscribe to our Newsletter

Like us on Facebook

Share on Twitter

top
Developed by Signum Soft