Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Simple While Loop | While Loops: Get Started
Python Loops

Svep för att visa menyn

book
Simple While Loop

The While statement is one of the basic programming concepts. It repeatedly executes the piece of code.

Imagine you have a task: write the code to calculate the factorial of the given number. For example, for n = 6 the result is: res = 1*2*3*4*5*6 = 720.

The step by step solution is:

python

Each operation is a new line of code. How many lines are needed? For huge numbers, you'll get very long instructions.

Instead, we'll use the loops. Take a look at how it works:

There are two main parts: stop condition and body. Body are instructions that need to be executed several times. For the factorial task, the body is multiplication the res by the next number. The condition helps to know when to stop.

If a condition is True, the body is executed one more time. When the condition becomes False, the loop is over and we leave it.

In the factorial task, we multiply all numbers from 1 to n so the loop is over when the multiplier becomes greater than n. This way, the stop condition must be False when it happens and True otherwise.

Here is what the syntax looks like in Python:

12
while condition: instructions
copy

Let's code the factorial task in Python. The body instructions are two lines of code:

  • basic operations like multiplying the result by the new number.

  • counter change.

1234567
n = 6 i = 1 # counter res = 1 # result variable while i<=n: res *= i i += 1 print(i, res)
copy

i is a multiplier which takes all values from 1 to n. It increments by 1 on each iteration. The stop condition is i<=n which is True at the beginning and becomes False when i==n+1. After that, the instructions are over, and the values of i and res are displayed in the console. Now i == n+1.

Uppgift

Swipe to start coding

The task is to find all positive numbers that are multiples of 3 less than 20. We'll do that using loops: starting with num = 0 (0 is a multiple of 3), insert num into the list numbers and then increment the num by 3. Insert the next value and so on until num <= 20.

Output the values in the numbers list.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 1
Vi beklagar att något gick fel. Vad hände?

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

book
Simple While Loop

The While statement is one of the basic programming concepts. It repeatedly executes the piece of code.

Imagine you have a task: write the code to calculate the factorial of the given number. For example, for n = 6 the result is: res = 1*2*3*4*5*6 = 720.

The step by step solution is:

python

Each operation is a new line of code. How many lines are needed? For huge numbers, you'll get very long instructions.

Instead, we'll use the loops. Take a look at how it works:

There are two main parts: stop condition and body. Body are instructions that need to be executed several times. For the factorial task, the body is multiplication the res by the next number. The condition helps to know when to stop.

If a condition is True, the body is executed one more time. When the condition becomes False, the loop is over and we leave it.

In the factorial task, we multiply all numbers from 1 to n so the loop is over when the multiplier becomes greater than n. This way, the stop condition must be False when it happens and True otherwise.

Here is what the syntax looks like in Python:

12
while condition: instructions
copy

Let's code the factorial task in Python. The body instructions are two lines of code:

  • basic operations like multiplying the result by the new number.

  • counter change.

1234567
n = 6 i = 1 # counter res = 1 # result variable while i<=n: res *= i i += 1 print(i, res)
copy

i is a multiplier which takes all values from 1 to n. It increments by 1 on each iteration. The stop condition is i<=n which is True at the beginning and becomes False when i==n+1. After that, the instructions are over, and the values of i and res are displayed in the console. Now i == n+1.

Uppgift

Swipe to start coding

The task is to find all positive numbers that are multiples of 3 less than 20. We'll do that using loops: starting with num = 0 (0 is a multiple of 3), insert num into the list numbers and then increment the num by 3. Insert the next value and so on until num <= 20.

Output the values in the numbers list.

Lösning

Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 1
Switch to desktopByt till skrivbordet för praktisk övningFortsätt där du är med ett av alternativen nedan
Vi beklagar att något gick fel. Vad hände?
some-alt