Programming fundamentalsLesson 1 of 1 in the course Loops

While Loops: Conditions, Counters, and Input-Controlled Repetition

5 flashcards · 5 quiz questions · 5 match pairs · 5 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

A while loop repeats a block of code as long as a condition remains true. It is useful when a task must repeat until a condition changes, such as generating a sequence, counting, or waiting for a particular user input.

A while loop has a loop expression (its condition) and a loop body (the statements that repeat). Python evaluates the condition before each iteration.

All flashcards

What is a while loop?
A control structure that repeats its body while its condition evaluates to true.
What is a loop body?
The indented statements that execute during each loop iteration.
What is a step size?
The amount by which a counter changes in each iteration.
What is a counter variable?
A variable used to track iterations or the values processed by a loop.
What makes a while loop terminate?
Its condition eventually evaluates to false.

Quiz questions

  1. When does Python stop executing a while loop?Answer: When its loop condition evaluates to false.
  2. What happens if a while loop's condition is false before the first check of its body?Answer: The loop body executes zero times.
  3. A loop starts with n = 4, prints n, and then subtracts 1 while n > 0. What values does it print?Answer: 4, 3, 2, and 1.
  4. A while loop keeps asking for input until the user enters "begin". Why should the loop body read another input?Answer: So the condition can be checked against a new value.
  5. In a loop that processes integers from n1 through n2, why use the condition number <= n2?Answer: It allows the loop to process the endpoint n2.

Based on Introduction to Python Programming by Udayan Das, Aubrey Lawson, Chris Mayfield, Narges Norouzi (OpenStax), licensed CC BY 4.0. Adapted into a summary, flashcards and quizzes.

All Programming fundamentals lessons