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

Veeg om het menu te tonen

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.

Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 1
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?

Vraag AI

expand
ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

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.

Taak

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.

Oplossing

Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 1. Hoofdstuk 1
Switch to desktopSchakel over naar desktop voor praktijkervaringGa verder vanaf waar je bent met een van de onderstaande opties
Onze excuses dat er iets mis is gegaan. Wat is er gebeurd?
some-alt