Institute of Computer Science
  1. Courses
  2. 2025/26 fall
  3. Introduction to Programming (MTAT.03.236)
ET
Log in

Introduction to Programming 2025/26 fall

  • Home Page
  • Introduction
  • Expressions
  • Conditional Execution
  • Functions
  • Iterations
  • Strings
  • Files
  • Lists
  • Graphics?
< previousTable of Contentsnext >

5.2 WHILE STATEMENT

While statements, or while loops, allow a block of code to be executed repeatedly as long as the condition is True.

A while loop starts with the keyword while, followed by a condition that is evaluated each time before the body of the loop is executed. The condition is a boolean expression that determines whether the loop continues to execute. If the condition is True, the code block under the while loop is executed. This process repeats until the condition becomes False. After the condition evaluates to False, the program exits the while loop and continues executing the subsequent lines of code.

Note that the condition is checked before each iteration, so the loop will not execute if the condition is False from the beginning.

In this example, we sum all the numbers from 1 through 9 as the condition states that the loop should continue working until i is less than 10. We increment the i each iteration and add the value of i on each iteration to the variables sum_numbers which calculates the total sum of all the numbers. We then print out both the values of sum_numbers and i.

Here's a flowchart to demonstrate how a while-loop operates.

Flowchart. While statement flowchart. geeksforgeeks.org

As you can see from the flowchart, the while condition is evaluated on each iteration, if it’s true, then the body of the while loop (the indented block after the while condition) is executed. In case the condition is not true, the loop will be exited, and the program will continue execution from the statements following the while loop.

As a rule of thumb, you should use while loops when the number of iterations is unknown and the infinite loops are intentional.

INFINTE LOOPS

Infinite loops occur when a loop's condition never equates to False and thus it keeps on executing the loop’s body indefinitely.

In while loops, if the condition always evaluates to True, the loop will continue endlessly. For for loops, infinite loops can occur if the loop iterates over a data structure that continuously grows, for example, iterating over a list to which you add more values within the loop.

Take the previous example of summing numbers and say we forgot to increment the value of i during each iteration. Now, the loop would keep working indefinitely as the value of i remains constant and is always less than 10. Hence, we'd never reach the print statement.

However, infinite loops are sometimes intentionally used to keep a program running until an action triggers a break statement to exit the loop. More on how to use break statements will be discussed in the next part of this chapter.

KEY THINGS TO REMEMBER

  • while loop allows for the execution of a block of code repeatedly as long as a given condition remains true.
  • The condition is a boolean expression checked before each iteration. The loop executes if the condition is True.
  • Infinite loops run indefinitely because the termination condition is never met. However, on some occasions, we intentionally want to create an infinite loop, which we then exit upon a certain condition being met.

SELF-CONTROL EXERCISES

< previousTable of Contentsnext >

  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment