Computer Science Grade 10 20 min

While Loops

While Loops

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Implement a while loop that terminates based on a user-provided sentinel value. Construct a while loop to perform robust input validation. Design and implement nested while loops to solve problems with two-dimensional logic. Analyze loop conditions to identify and prevent potential infinite loops. Use loop control statements like 'break' to create complex exit conditions. Differentiate between state-controlled and counter-controlled loops and choose the appropriate one for a given task. Ever wonder how a login screen keeps asking for your password until you get it right? 🤔 That's the power of advanced while loops in action! In this lesson, we move beyond simple counter-based loops. You will learn how to create powerful, flexible while loo...
2

Key Concepts & Vocabulary

TermDefinitionExample Sentinel ValueA special value used in the condition of a loop to signal that it should terminate. It is not processed as regular data.In a program that averages grades, the user might enter `-1` as a sentinel value to indicate they are done entering grades. Input Validation LoopA loop that repeatedly prompts a user for input until they enter data that meets a specific set of criteria (e.g., a number within a certain range).while (age < 18) { print("You must be 18 or older."); age = get_user_input(); } Infinite LoopA loop where the terminating condition is never met, causing it to run endlessly and freeze the program.int x = 0; while (x < 10) { print("Hello"); } // This is infinite because x is never changed. Nested While LoopsA while loop th...
3

Core Syntax & Patterns

Sentinel-Controlled Loop Pattern 1. Get first input. 2. while (input != sentinel_value) { 3. Process the input. 4. Get the next input. 5. } Use this pattern when you need to process an unknown number of data items. The key is to get the next input at the very end of the loop body to avoid an infinite loop and to avoid processing the sentinel value itself. Input Validation Loop Pattern 1. Get initial input. 2. while (input is NOT valid) { 3. Print error message. 4. Get input again. 5. } This is the standard way to ensure data quality. The loop only runs if the initial data is bad, and it continues to trap the user until they provide valid data.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
Analyze this code: `outer = 1; count = 0; while (outer <= 4) { inner = outer; while (inner <= 4) { count += 1; inner += 1; } outer += 1; }`. How many times is `count += 1` executed?
A.16
B.10
C.4
D.7
Challenging
You are creating a password validation loop. The loop must continue until the password is at least 8 characters long AND contains at least one digit. Which `while` condition correctly enforces this?
A.while (length(password) < 8 && !contains_digit(password))
B.while (length(password) > 8 || contains_digit(password))
C.while (length(password) >= 8 && contains_digit(password))
D.while (length(password) < 8 || !contains_digit(password))
Challenging
Compare the standard sentinel pattern (Pattern A) with a `while(true)` with `break` pattern (Pattern B). Pattern A: `input = get(); while(input != S) { process(input); input = get(); }`. Pattern B: `while(true) { input = get(); if(input == S) { break; } process(input); }`. Which statement is the most accurate evaluation?
A.Pattern A is generally preferred as its termination condition is explicit in the loop header, making it more readable and less prone to infinite loops if the `break` is forgotten.
B.Pattern B is superior because it avoids the duplicated `get()` call, making the code more concise and efficient.
C.Both patterns are functionally identical and the choice is purely a matter of personal style with no impact on readability or correctness.
D.Pattern A is more likely to process the sentinel value by mistake, while Pattern B inherently prevents this error.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.