C++ - While Loop

Overview

Estimated time: 20–30 minutes

Repeat work while a condition holds using while and do/while loops.

Learning Objectives

  • Write guarded loops with while.
  • Use do/while when the body must run at least once.

Prerequisites

Examples

int i=0; while (i<3) { ++i; }

do { /*...*/ } while(false);

Common Pitfalls

  • Forgetting to update the loop variable can cause infinite loops.