Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Unit 5: Input and Iteration

Overview

This unit covers input validation, sentinel loops, while loops, flags, break/continue, infinite-loop prevention. Follow the same problem progression as Python so that the main comparison is language syntax and semantics. Use the worked examples as starting points, then complete the activities independently.

Lesson 5.1: Input and validation

Reading References

  • Official C++ language guide and standard-library documentation.
  • Corresponding Python course lesson for the shared problem context and learning objective.

Learning Objectives

  • Explain this concept using C++ terminology.
  • Trace a short program and predict its behavior before execution.
  • Modify and test the example independently.

Concept Explanation

Input and validation is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: validate extraction and the allowed range; handle conversion exceptions; define a policy for empty input.

Worked Example

std::istringstream input("16"); int age = 0; if (!(input >> age) || age < 0 || age > 130) std::cout << "Enter a valid age from 0 to 130\n"; else std::cout << "Accepted age: " << age << '\n';

Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.

Expected Behavior or Output

Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):

Accepted age: 16

Common Mistakes

  • Validate extraction and the allowed range.
  • Handle conversion exceptions.
  • Define a policy for empty input.

Activities

  1. Trace the input and validation example and predict its output before compiling.
  2. Adapt the example to validated input and sentinel-driven loops and preserve the same observable requirement using idiomatic C++20.
  3. Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
  4. Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.

Accessibility Practice

Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.

Skills Checklist

  • I can explain the concept in C++ terminology.
  • I can trace and predict the example.
  • I can modify and run the code independently.
  • I can identify and correct a common error.
  • I can read source and output accessibly.

Lesson 5.2: While loops

Reading References

  • Official C++ language guide and standard-library documentation.
  • Corresponding Python course lesson for the shared problem context and learning objective.

Learning Objectives

  • Explain this concept using C++ terminology.
  • Trace a short program and predict its behavior before execution.
  • Modify and test the example independently.

Concept Explanation

While loops is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: update the loop condition and sentinel correctly; decide whether the sentinel is data; stop cleanly on failed extraction.

Worked Example

std::istringstream input("1 2 3 0"); int value = 0, total = 0; while (input >> value && value != 0) total += value; std::cout << "Sum before sentinel: " << total << '\n';

Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.

Expected Behavior or Output

Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):

Sum before sentinel: 6

Common Mistakes

  • Update the loop condition and sentinel correctly.
  • Decide whether the sentinel is data.
  • Stop cleanly on failed extraction.

Activities

  1. Trace the while loops example and predict its output before compiling.
  2. Adapt the example to validated input and sentinel-driven loops and preserve the same observable requirement using idiomatic C++20.
  3. Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
  4. Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.

Accessibility Practice

Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.

Skills Checklist

  • I can explain the concept in C++ terminology.
  • I can trace and predict the example.
  • I can modify and run the code independently.
  • I can identify and correct a common error.
  • I can read source and output accessibly.

Lesson 5.3: Flags and break

Reading References

  • Official C++ language guide and standard-library documentation.
  • Corresponding Python course lesson for the shared problem context and learning objective.

Learning Objectives

  • Explain this concept using C++ terminology.
  • Trace a short program and predict its behavior before execution.
  • Modify and test the example independently.

Concept Explanation

Flags and break is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: place break in the intended loop; handle nested-loop exit deliberately; report the not-found case.

Worked Example

const std::vector<std::string> names{"Avery", "Jordan", "Sam"}; bool found = false; for (const auto& name : names) { if (name == "Jordan") { found = true; break; } } std::cout << "Jordan found: " << std::boolalpha << found << '\n';

Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.

Expected Behavior or Output

Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):

Jordan found: true

Common Mistakes

  • Place break in the intended loop.
  • Handle nested-loop exit deliberately.
  • Report the not-found case.

Activities

  1. Trace the flags and break example and predict its output before compiling.
  2. Adapt the example to validated input and sentinel-driven loops and preserve the same observable requirement using idiomatic C++20.
  3. Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
  4. Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.

Accessibility Practice

Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.

Skills Checklist

  • I can explain the concept in C++ terminology.
  • I can trace and predict the example.
  • I can modify and run the code independently.
  • I can identify and correct a common error.
  • I can read source and output accessibly.

Lesson 5.4: Continue and filtering

Reading References

  • Official C++ language guide and standard-library documentation.
  • Corresponding Python course lesson for the shared problem context and learning objective.

Learning Objectives

  • Explain this concept using C++ terminology.
  • Trace a short program and predict its behavior before execution.
  • Modify and test the example independently.

Concept Explanation

Continue and filtering is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: filter before producing output; ensure continue does not skip required state updates; verify comparison direction.

Worked Example

for (int value : {-2, 3, 0, 5, -1}) { if (value <= 0) continue; std::cout << value << ' '; } std::cout << '\n';

Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.

Expected Behavior or Output

Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):

3 5 

Common Mistakes

  • Filter before producing output.
  • Ensure continue does not skip required state updates.
  • Verify comparison direction.

Activities

  1. Trace the continue and filtering example and predict its output before compiling.
  2. Adapt the example to validated input and sentinel-driven loops and preserve the same observable requirement using idiomatic C++20.
  3. Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
  4. Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.

Accessibility Practice

Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.

Skills Checklist

  • I can explain the concept in C++ terminology.
  • I can trace and predict the example.
  • I can modify and run the code independently.
  • I can identify and correct a common error.
  • I can read source and output accessibly.

Lesson 5.5: Infinite-loop prevention

Reading References

  • Official C++ language guide and standard-library documentation.
  • Corresponding Python course lesson for the shared problem context and learning objective.

Learning Objectives

  • Explain this concept using C++ terminology.
  • Trace a short program and predict its behavior before execution.
  • Modify and test the example independently.

Concept Explanation

Infinite-loop prevention is taught through the shared course datasets and requirements. The Python curriculum supplies a common problem context, not syntax to translate mechanically. In C++, make the type system, object lifetime, standard-library contracts, ownership, and failure behavior explicit. This lesson focuses on: bound retries; increment the attempt count on every attempt; reset validation state before retrying.

Worked Example

int attempts = 0; bool valid = false; while (!valid && attempts < 3) { ++attempts; valid = attempts == 2; } std::cout << "Attempts: " << attempts << "; valid: " << std::boolalpha << valid << '\n';

Trace: predict the output before compiling. Identify declarations, values, control-flow decisions, and state changes in order. Then compile and compare actual output with the prediction. The lesson example is intended to be independently compilable.

Expected Behavior or Output

Output observed when the worked example was run with empty standard input (g++ 13.3, C++20; the text of the output does not depend on the operating system):

Attempts: 2; valid: true

Common Mistakes

  • Bound retries.
  • Increment the attempt count on every attempt.
  • Reset validation state before retrying.

Activities

  1. Trace the infinite-loop prevention example and predict its output before compiling.
  2. Adapt the example to validated input and sentinel-driven loops and preserve the same observable requirement using idiomatic C++20.
  3. Add one boundary case and one invalid or missing-data case; state the expected result and implement the handling.
  4. Write a short test or trace table, then explain the C++ type, lifetime, ownership, iterator, or error-handling rule that matters here.

Accessibility Practice

Navigate the complete source file by line, word, and character using the learner’s access technology. Read punctuation and indentation, run from the terminal, and inspect standard output and errors sequentially.

Skills Checklist

  • I can explain infinite-loop prevention using correct C++ terminology and distinguish it from the closest Python equivalent.
  • I can trace the example and justify its output using C++ rules.
  • I can implement the same requirement for validated input and sentinel-driven loops without copying Python syntax.
  • I can test a normal case, a boundary case, and a relevant invalid or failure case.
  • I can compile the program, interpret diagnostics, and navigate source and terminal output using my access technology.