파이썬 while for - paisseon while for

In programming, loops are used to repeat a block of code. For example, if we want to show a message 100 times, then we can use a loop. It's just a simple example, we can achieve much more with loops.

In the previous tutorial, we learned about Python for loop. Here, we are going to learn about

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 loops.


Python

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 loop is used to run a specific code until a certain condition is met.

The syntax of

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 loop is:

while condition:
    # body of while loop

Here,

  1. A
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    0 loop evaluates the
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4
  2. If the
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4 evaluates to
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    6, the code inside the
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    0 loop is executed.
  3. # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4 is evaluated again.
  4. This process continues until the condition is
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    9.
  5. When
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    4 evaluates to
    # program to display numbers from 1 to 5
    
    # initialize the variable
    i = 1
    n = 5
    
    # while loop from i = 1 to 5
    while i <= n:
        print(i)
        i = i + 1
    9, the loop stops.

Flowchart for Python While Loop

파이썬 while for - paisseon while for
Flowchart for while Loop in Python

Example: Python while Loop

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1

Output

1
2
3
4
5

Here's how the program works:

VariableCondition:

1
2
3
4
5
2Action
1
2
3
4
5
3
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
1
2
3
4
5
6 is printed.
1
2
3
4
5
7 is increased to 2.
1
2
3
4
5
8
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')
1 is printed.
1
2
3
4
5
7 is increased to 3.
current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')
3
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')
6 is printed.
1
2
3
4
5
7 is increased to 4.
current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')
8
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends
1 is printed.
1
2
3
4
5
7 is increased to 5.
You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends
3
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6
You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends
6 is printed.
1
2
3
4
5
7 is increased to 6.
You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends
8
1
2
3
4
5
4
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
9The loop is terminated.


Example 2: Python while Loop to Display Game Level

current_level = 0
final_level = 5

game_completed = True

while current_level <= final_level:
    if game_completed: 
        print('You have passed level', current_level)
        current_level += 1

print('Level Ends')

Output

You have passed level 0
You have passed level 1
You have passed level 2
You have passed level 3
You have passed level 4
You have passed level 5
Level Ends

In the above example, we have used the

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 loop to check the current level and display it on the console.


Infinite while Loop in Python

If the

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4 of a loop is always
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6, the loop runs for infinite times (until the memory is full). For example,

# infinite while loop
while True:
    # body of the loop

In the above example, the

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4 is always
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
6. Hence, the loop body will run for infinite times.


Python While loop with else

A

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 loop can have an optional
# infinite while loop
while True:
    # body of the loop
7 block as well.

The

# infinite while loop
while True:
    # body of the loop
7 part is executed after the
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
4 in the while loop evaluates to
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
9. For example,

counter = 0

while counter < 3:
    print('Inside loop')
    counter = counter + 1
else:
    print('Inside else')

Output

Inside loop
Inside loop
Inside loop
Inside else

Here, we have used the counter variable to print the

counter = 0

while counter < 3:
    print('Inside loop')
    counter = counter + 1
else:
    print('Inside else')
1 string three times.

On the fourth iteration, the condition in

# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
0 becomes
# program to display numbers from 1 to 5

# initialize the variable
i = 1
n = 5

# while loop from i = 1 to 5
while i <= n:
    print(i)
    i = i + 1
9. Hence, the
# infinite while loop
while True:
    # body of the loop
7 part is executed.