← All lectures

Lecture 12 · 25 min

do…while Loop

A posttest loop that always executes at least once — ideal for input validation and menu-driven programs.

do…while syntaxPosttest loopInput validationMenu-driven programsChoosing the right loop
Slide 1 / 14

The do…while loop — runs at least once

The do…while loop is different from while and for in one key way: the condition is checked AFTER the body runs.

This means the body is guaranteed to execute at least once, no matter what.

This is perfect for:

  • Asking the user for input (you must ask at least once)
  • Menu-driven programs (the menu must appear at least once)
  • Input validation (you must attempt to read before checking if it's valid)
← / → navigateF fullscreenEsc exit⌘K search