Questions And Answers

More Tutorials

Raspberry while-loop

Let's first take a look at a program written with while-loop statement:

print("loop start")
while a < 5 :
a = a + 1
print("loop end")

The program is first executed in the sequence structure. At the beginning of the program, we first declare a variable “a" and print a line of characters “loop start" with print().

print("loop start")

What is a variable?

Variables are values that have no fixed values and can be changed at any time. We use variables to store data so that they can be called in later portions of our code. In a program, a variable generally has two aspects: variable name and variable value. The variable name is the identity of the variable, whereas the variable value is the value stored in the variable. For example, in the above code, “a” is the variable name whereas 0 is the variable value. When multiple variables are used in a programme, unique names are used to distinguish them.

Variables in Python do not need to be declared. Each variable must be assigned before use, and will be created at the same time as when it is first assigned.

Next, we use the while statement to create a definite loop structure. This specifies that when the variable “a" is less than 5, the program should repeatedly execute the instructions contained in the while statement. That is, it should print out the value of “a", and add one to “a", until “a" is greater than or equal to 5.

while a < 5 :
a = a + 1

The output of the program is as follows:

loop start
loop end

Looking at the execution results of the sample program, we can find that the program does not start to execute the last print() statement until it has executed several loops within the while statement.

But, how does the computer know which statements need to be repeated in the while loop statement and which statements are outside the while loop statement? MicroPython uses indentation and colon “:" to distinguish the levels between code blocks, unlike other programming languages (such as Java and C) which use braces “{}" to separate code blocks. This change makes code written in MicroPython clear and easy to understand.

Python is very strict on code indentation. The same level of code block indentation must be consistent. In general, we can use BACKSPACE or TAB to complete the indentation. However, whether we manually type multiple spaces or use TAB to indent, we usually define the length on an indentation as four spaces (by default, one tab is equal to four spaces).


In this page (written and validated by ) you learned about Raspberry while-loop . What's Next? If you are interested in completing Raspberry tutorial, your next topic will be learning about: Raspberry for loop.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.