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

Stryg for at vise menuen

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.

Opgave

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 desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

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.

Opgave

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 desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 1. Kapitel 1
Switch to desktopSkift til skrivebord for at øve i den virkelige verdenFortsæt der, hvor du er, med en af nedenstående muligheder
Vi beklager, at noget gik galt. Hvad skete der?
some-alt