Cara menggunakan python infinite increment

While loops are very powerful programming structures that you can use in your programs to repeat a sequence of statements.

In this article, you will learn:

  • What while loops are.
  • What they are used for.
  • When they should be used.
  • How they work behind the scenes.
  • How to write a while loop in Python.
  • What infinite loops are and how to interrupt them.
  • What
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    5 is used for and its general syntax.
  • How to use a
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    6 statement to stop a while loop.

You will learn how while loops work behind the scenes with examples, tables, and diagrams.

Are you ready? Let's begin. πŸ”…

πŸ”Ή Purpose and Use Cases for While Loops

Let's start with the purpose of while loops. What are they used for?

They are used to repeat a sequence of statements an unknown number of times. This type of loop runs while a given condition is

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
7 and it only stops when the condition becomes
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8.

When we write a while loop, we don't explicitly define how many iterations will be completed, we only write the condition that has to be

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
7 to continue the process and
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8 to stop it.

πŸ’‘ Tip: if the while loop condition never evaluates to

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8, then we will have an infinite loop, which is a loop that never stops (in theory) without external intervention.

These are some examples of real use cases of while loops:

  • User Input: When we ask for user input, we need to check if the value entered is valid. We can't possibly know in advance how many times the user will enter an invalid input before the program can continue. Therefore, a while loop would be perfect for this scenario.
  • Search: searching for an element in a data structure is another perfect use case for a while loop because we can't know in advance how many iterations will be needed to find the target value. For example, the Binary Search algorithm can be implemented using a while loop.
  • Games: In a game, a while loop could be used to keep the main logic of the game running until the player loses or the game ends. We can't know in advance when this will happen, so this is another perfect scenario for a while loop.

πŸ”Έ How While Loops Work

Now that you know what while loops are used for, let's see their main logic and how they work behind the scenes. Here we have a diagram:

Cara menggunakan python infinite increment
While Loop

Let's break this down in more detail:

  • The process starts when a while loop is found during the execution of the program.
  • The condition is evaluated to check if it's
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7 or
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    8.
  • If the condition is
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7, the statements that belong to the loop are executed.
  • The while loop condition is checked again.
  • If the condition evaluates to
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7 again, the sequence of statements runs again and the process is repeated.
  • When the condition evaluates to
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    8, the loop stops and the program continues beyond the loop.

One of the most important characteristics of while loops is that the variables used in the loop condition are not updated automatically. We have to update their values explicitly with our code to make sure that the loop will eventually stop when the condition evaluates to

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8.

πŸ”Ή General Syntax of While Loops

Great. Now you know how while loops work, so let's dive into the code and see how you can write a while loop in Python. This is the basic syntax:

Cara menggunakan python infinite increment
While Loop (Syntax)

These are the main elements (in order):

  • The
    nums = []
    8 keyword (followed by a space).
  • A condition to determine if the loop will continue running or not based on its truth value (
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7 or
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    8 ).
  • A colon (
    while len(nums) < 4:
    1) at the end of the first line.
  • The sequence of statements that will be repeated. This block of code is called the "body" of the loop and it has to be indented. If a statement is not indented, it will not be considered part of the loop (please see the diagram below).
Cara menggunakan python infinite increment

πŸ’‘ Tip: The (PEP 8) recommends using 4 spaces per indentation level. Tabs should only be used to remain consistent with code that is already indented with tabs.

πŸ”Έ Examples of While Loops

Now that you know how while loops work and how to write them in Python, let's see how they work behind the scenes with some examples.

How a Basic While Loop Works

Here we have a basic while loop that prints the value of

while len(nums) < 4:
2 while
while len(nums) < 4:
2 is less than 8 (
while len(nums) < 4:
4):

i = 4

while i < 8:
    print(i)
    i += 1

If we run the code, we see this output:

4
5
6
7

Let's see what happens behind the scenes when the code runs:

Cara menggunakan python infinite increment
  • Iteration 1: initially, the value of
    while len(nums) < 4:
    2 is 4, so the condition
    while len(nums) < 4:
    4 evaluates to
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7 and the loop starts to run. The value of
    while len(nums) < 4:
    2 is printed (4) and this value is incremented by 1. The loop starts again.
  • Iteration 2: now the value of
    while len(nums) < 4:
    2 is 5, so the condition
    while len(nums) < 4:
    4 evaluates to
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7. The body of the loop runs, the value of
    while len(nums) < 4:
    2 is printed (5) and this value
    while len(nums) < 4:
    2 is incremented by 1. The loop starts again.
  • Iterations 3 and 4: The same process is repeated for the third and fourth iterations, so the integers 6 and 7 are printed.
  • Before starting the fifth iteration, the value of
    while len(nums) < 4:
    2 is
    user_input = int(input("Enter an integer: "))
    5. Now the while loop condition
    while len(nums) < 4:
    4 evaluates to
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    8 and the loop stops immediately.

πŸ’‘ Tip: If the while loop condition is

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8 before starting the first iteration, the while loop will not even start running.

User Input Using a While Loop

Now let's see an example of a while loop in a program that takes user input. We will the

user_input = int(input("Enter an integer: "))
9 function to ask the user to enter an integer and that integer will only be appended to list if it's even.

This is the code:

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)

The loop condition is

if user_input % 2 == 0:
0, so the loop will run while the length of the list
if user_input % 2 == 0:
1 is strictly less than 4.

Let's analyze this program line by line:

  • We start by defining an empty list and assigning it to a variable called
    if user_input % 2 == 0:
    1.
nums = []
  • Then, we define a while loop that will run while
    if user_input % 2 == 0:
    0.
while len(nums) < 4:
  • We ask for user input with the
    user_input = int(input("Enter an integer: "))
    9 function and store it in the
    if user_input % 2 == 0:
    5 variable.
user_input = int(input("Enter an integer: "))

πŸ’‘ Tip: We need to convert (cast) the value entered by the user to an integer using the

if user_input % 2 == 0:
6 function before assigning it to the variable because the
user_input = int(input("Enter an integer: "))
9 function returns a string ().

  • We check if this value is even or odd.
if user_input % 2 == 0:
  • If it's even, we append it to the
    if user_input % 2 == 0:
    1 list.
nums.append(user_input)
  • Else, if it's odd, the loop starts again and the condition is checked to determine if the loop should continue or not.

If we run this code with custom user input, we get the following output:

Enter an integer: 3
Enter an integer: 4    
Enter an integer: 2    
Enter an integer: 1
Enter an integer: 7
Enter an integer: 6    
Enter an integer: 3
Enter an integer: 4    

This table summarizes what happens behind the scenes when the code runs:

Cara menggunakan python infinite increment

πŸ’‘ Tip: The initial value of

if user_input % 2 == 0:
9 is
nums.append(user_input)
0 because the list is initially empty. The last column of the table shows the length of the list at the end of the current iteration. This value is used to check the condition before the next iteration starts.

As you can see in the table, the user enters even integers in the second, third, sixth, and eight iterations and these values are appended to the

if user_input % 2 == 0:
1 list.

Before a "ninth" iteration starts, the condition is checked again but now it evaluates to

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8 because the
if user_input % 2 == 0:
1 list has four elements (length 4), so the loop stops.

If we check the value of the

if user_input % 2 == 0:
1 list when the process has been completed, we see this:

>>> nums
[4, 2, 6, 4]

Exactly what we expected, the while loop stopped when the condition

if user_input % 2 == 0:
0 evaluated to
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8.

Now you know how while loops work behind the scenes and you've seen some practical examples, so let's dive into a key element of while loops: the condition.

πŸ”Ή Tips for the Condition in While Loops

Before you start working with while loops, you should know that the loop condition plays a central role in the functionality and output of a while loop.

Cara menggunakan python infinite increment

You must be very careful with the comparison operator that you choose because this is a very common source of bugs.

For example, common errors include:

  • Using
    nums.append(user_input)
    7 (less than) instead of
    nums.append(user_input)
    8 (less than or equal to) (or vice versa).
  • Using
    nums.append(user_input)
    9 (greater than) instead of
    Enter an integer: 3
    Enter an integer: 4    
    Enter an integer: 2    
    Enter an integer: 1
    Enter an integer: 7
    Enter an integer: 6    
    Enter an integer: 3
    Enter an integer: 4    
    0 (greater than or equal to) (or vice versa).  

This can affect the number of iterations of the loop and even its output.

Let's see an example:

If we write this while loop with the condition

Enter an integer: 3
Enter an integer: 4    
Enter an integer: 2    
Enter an integer: 1
Enter an integer: 7
Enter an integer: 6    
Enter an integer: 3
Enter an integer: 4    
1:

4
5
6
7
0

We see this output when the code runs:

4
5
6
7
1

The loop completes three iterations and it stops when

while len(nums) < 4:
2 is equal to
Enter an integer: 3
Enter an integer: 4    
Enter an integer: 2    
Enter an integer: 1
Enter an integer: 7
Enter an integer: 6    
Enter an integer: 3
Enter an integer: 4    
3.

This table illustrates what happens behind the scenes when the code runs:

Cara menggunakan python infinite increment
  • Before the first iteration of the loop, the value of
    while len(nums) < 4:
    2 is 6, so the condition
    Enter an integer: 3
    Enter an integer: 4    
    Enter an integer: 2    
    Enter an integer: 1
    Enter an integer: 7
    Enter an integer: 6    
    Enter an integer: 3
    Enter an integer: 4    
    1 is
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7 and the loop starts running. The value of
    while len(nums) < 4:
    2 is printed and then it is incremented by 1.
  • In the second iteration of the loop, the value of
    while len(nums) < 4:
    2 is 7, so the condition
    Enter an integer: 3
    Enter an integer: 4    
    Enter an integer: 2    
    Enter an integer: 1
    Enter an integer: 7
    Enter an integer: 6    
    Enter an integer: 3
    Enter an integer: 4    
    1 is
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7. The body of the loop runs, the value of
    while len(nums) < 4:
    2 is printed, and then it is incremented by 1.
  • In the third iteration of the loop, the value of
    while len(nums) < 4:
    2 is 8, so the condition
    Enter an integer: 3
    Enter an integer: 4    
    Enter an integer: 2    
    Enter an integer: 1
    Enter an integer: 7
    Enter an integer: 6    
    Enter an integer: 3
    Enter an integer: 4    
    1 is
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7. The body of the loop runs, the value of
    while len(nums) < 4:
    2 is printed, and then it is incremented by 1.
  • The condition is checked again before a fourth iteration starts, but now the value of
    while len(nums) < 4:
    2 is 9, so
    Enter an integer: 3
    Enter an integer: 4    
    Enter an integer: 2    
    Enter an integer: 1
    Enter an integer: 7
    Enter an integer: 6    
    Enter an integer: 3
    Enter an integer: 4    
    1 is
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    8 and the loop stops.

In this case, we used

nums.append(user_input)
7 as the comparison operator in the condition, but what do you think will happen if we use
nums.append(user_input)
8 instead?

4
5
6
7
2

We see this output:

4
5
6
7
3

The loop completes one more iteration because now we are using the "less than or equal to" operator

nums.append(user_input)
8 , so the condition is still
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
7 when
while len(nums) < 4:
2 is equal to
Enter an integer: 3
Enter an integer: 4    
Enter an integer: 2    
Enter an integer: 1
Enter an integer: 7
Enter an integer: 6    
Enter an integer: 3
Enter an integer: 4    
3.

This table illustrates what happens behind the scenes:

Cara menggunakan python infinite increment

Four iterations are completed. The condition is checked again before starting a "fifth" iteration. At this point, the value of

while len(nums) < 4:
2 is
4
5
6
7
06, so the condition
4
5
6
7
07 is
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8 and the loop stops.

πŸ”Έ Infinite While Loops

Now you know how while loops work, but what do you think will happen if the while loop condition never evaluates to

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8?

Cara menggunakan python infinite increment

What are Infinite While Loops?

Remember that while loops don't update variables automatically (we are in charge of doing that explicitly with our code). So there is no guarantee that the loop will stop unless we write the necessary code to make the condition

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8 at some point during the execution of the loop.

If we don't do this and the condition always evaluates to

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
7, then we will have an infinite loop, which is a while loop that runs indefinitely (in theory).

Infinite loops are typically the result of a bug, but they can also be caused intentionally when we want to repeat a sequence of statements indefinitely until a

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 statement is found.

Let's see these two types of infinite loops in the examples below.

πŸ’‘ Tip: A bug is an error in the program that causes incorrect or unexpected results.

Example of Infinite Loop

This is an example of an unintentional infinite loop caused by a bug in the program:

4
5
6
7
4

Analyze this code for a moment.

Don't you notice something missing in the body of the loop?

That's right!

The value of the variable

while len(nums) < 4:
2 is never updated (it's always
4
5
6
7
14). Therefore, the condition
4
5
6
7
15 is always
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
7 and the loop never stops.

If we run this code, the output will be an "infinite" sequence of

4
5
6
7
17 messages because the body of the loop
4
5
6
7
18 will run indefinitely.

4
5
6
7
5

To stop the program, we will need to interrupt the loop manually by pressing

4
5
6
7
19.

When we do, we will see a

4
5
6
7
20 error similar to this one:

Cara menggunakan python infinite increment

To fix this loop, we will need to update the value of

while len(nums) < 4:
2 in the body of the loop to make sure that the condition
4
5
6
7
15 will eventually evaluate to
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8.

This is one possible solution, incrementing the value of

while len(nums) < 4:
2 by 2 on every iteration:

4
5
6
7
6

Great. Now you know how to fix infinite loops caused by a bug. You just need to write code to guarantee that the condition will eventually evaluate to

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
8.

Let's start diving into intentional infinite loops and how they work.

πŸ”Ή How to Make an Infinite Loop with While True

We can generate an infinite loop intentionally using

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
5. In this case, the loop will run indefinitely until the process is stopped by external intervention (
4
5
6
7
19) or when a
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 statement is found (you will learn more about
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 in just a moment).

This is the basic syntax:

Cara menggunakan python infinite increment

Instead of writing a condition after the

nums = []
8 keyword, we just write the truth value directly to indicate that the condition will always be
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
7.

Here we have an example:

4
5
6
7
7

The loop runs until

4
5
6
7
19 is pressed, but Python also has a
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 statement that we can use directly in our code to stop this type of loop.

The # Define the list nums = [] # The loop will run while the length of the # list nums is less than 4 while len(nums) < 4: # Ask for user input and store it in a variable as an integer. user_input = int(input("Enter an integer: ")) # If the input is an even number, add it to the list if user_input % 2 == 0: nums.append(user_input)6 statement

This statement is used to stop a loop immediately. You should think of it as a red "stop sign" that you can use in your code to have more control over the behavior of the loop.

Cara menggunakan python infinite increment

According to the :

The statement, like in C, breaks out of the innermost enclosing or loop.

This diagram illustrates the basic logic of the

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 statement:

Cara menggunakan python infinite increment
The
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 statement

This is the basic logic of the

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 statement:

  • The while loop starts only if the condition evaluates to
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7.
  • If a
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    6 statement is found at any point during the execution of the loop, the loop stops immediately.
  • Else, if
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    6 is not found, the loop continues its normal execution and it stops when the condition evaluates to
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    8.

We can use

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 to stop a while loop when a condition is met at a particular point of its execution, so you will typically find it within a conditional statement, like this:

4
5
6
7
8

This stops the loop immediately if the condition is

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
7.

πŸ’‘ Tip: You can (in theory) write a

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 statement anywhere in the body of the loop. It doesn't necessarily have to be part of a conditional, but we commonly use it to stop the loop when a given condition is
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
7.

Here we have an example of

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 in a
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
5 loop:

Cara menggunakan python infinite increment

Let's see it in more detail:

The first line defines a

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
5 loop that will run indefinitely until a
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 statement is found (or until it is interrupted with
4
5
6
7
19).

4
5
6
7
9

The second line asks for user input. This input is converted to an integer and assigned to the variable

if user_input % 2 == 0:
5.

user_input = int(input("Enter an integer: "))

The third line checks if the input is odd.

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
1

If it is, the message

4
5
6
7
55 is printed and the
# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 statement stops the loop immediately.

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
2

Else, if the input is even , the message

4
5
6
7
57 is printed and the loop starts again.

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
3

The loop will run indefinitely until an odd integer is entered because that is the only way in which the

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
6 statement will be found.

Here we have an example with custom user input:

# Define the list
nums = []

# The loop will run while the length of the
# list nums is less than 4
while len(nums) < 4:
    # Ask for user input and store it in a variable as an integer.
    user_input = int(input("Enter an integer: "))
    # If the input is an even number, add it to the list
    if user_input % 2 == 0:
        nums.append(user_input)
4

πŸ”Έ In Summary

  • While loops are programming structures used to repeat a sequence of statements while a condition is
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    7. They stop when the condition evaluates to
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    8.
  • When you write a while loop, you need to make the necessary updates in your code to make sure that the loop will eventually stop.
  • An infinite loop is a loop that runs indefinitely and it only stops with external intervention or when a
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    6 statement is found.
  • You can stop an infinite loop with
    4
    5
    6
    7
    19.
  • You can generate an infinite loop intentionally with
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    5.
  • The
    # Define the list
    nums = []
    
    # The loop will run while the length of the
    # list nums is less than 4
    while len(nums) < 4:
        # Ask for user input and store it in a variable as an integer.
        user_input = int(input("Enter an integer: "))
        # If the input is an even number, add it to the list
        if user_input % 2 == 0:
            nums.append(user_input)
    6 statement can be used to stop a while loop immediately.

I really hope you liked my article and found it helpful. Now you know how to work with While Loops in Python.

Follow me on Twitter @EstefaniaCassN and if you want to learn more about this topic, check out my online course Python Loops and Looping Techniques: Beginner to Advanced.

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT


Cara menggunakan python infinite increment
Estefania Cassingena Navone

Developer, technical writer, and content creator @freeCodeCamp. I run the freeCodeCamp.org EspaΓ±ol YouTube channel.


If you read this far, tweet to the author to show them you care. Tweet a thanks

Learn to code for free. freeCodeCamp's open source curriculum has helped more than 40,000 people get jobs as developers. Get started