Stat Mania
  • C Programming
  • All Courses
  1. Loops
  2. Why Use break in a while Loop If the Condition Already Stops It?
  • Course Home
  • Getting Started
    • C's "Hello, World!" Explained Line by Line (for R & Python Users)
    • How to Test a Small C Snippet Quickly
  • Variables and Data Types
    • Declaring Multiple Variables in One Line in C
    • float vs double in C: Precision, Memory, and When to Use Which
  • Operators and Input
    • Pre-Increment vs Post-Increment in C: ++i vs i++
    • Checking User Input in C: Is It Really a Number?
  • Making Decisions
    • Nested if in C, Explained with Real-Life Examples
    • Shortcut Ways to Write if in C: the Ternary Operator and More
    • The switch Statement in C: Syntax, break, Fall-Through and Examples
  • Loops
    • The do-while Loop in C: Run First, Check After
    • Why Use break in a while Loop If the Condition Already Stops It?
    • Printing a Comma-Separated List in C (No Trailing Comma, Clean Line Breaks)
    • 5 while Loop Practice Problems in C (with Hints and Solutions)
    • 5 for Loop Practice Problems in C (with Hints and Solutions)
    • 5 Nested for Loop Practice Problems in C (with Hints and Solutions)
  • Arrays and Structs
    • How to Save Loop Results in an Array in C
    • Structs in C: Grouping Related Data (with Python and R Comparisons)
  • Random Numbers
    • srand(time(NULL)) in C: Why Random Numbers Repeat and How to Seed Them
    • Tossing a Die in C Until We Get a 6

On this page

  • The Condition Is the Normal Exit, break Is the Special Exit
  • Four Short Examples
    • 1. Search: Stop When We Find It
    • 2. Read Numbers Until 0
    • 3. Retry with a Limit
    • 4. An Endless Loop with an Exit
  • Which Stop Goes Where?
  • Summary
  1. Loops
  2. Why Use break in a while Loop If the Condition Already Stops It?

Why Use break in a while Loop If the Condition Already Stops It?

If a loop runs while i < 10 but breaks at 4, why write i < 10 at all? The condition is the planned exit and break is the exit for a special case. Four short examples: search, sentinel input, retries and menus.
Reading 10 min

By the end of this lesson you will

  • Separate a loop’s planned exit from break
  • Apply break to search, sentinel, retry and menu loops

Resources

  • break statement
  • while loop

A while loop box with the condition i < 10. A teal arrow on the right is the normal exit when the condition is false; a red arrow below is the break exit taken when i equals 4.

A loop has a planned exit (the condition) and an emergency exit (break).

A common question when we first meet break:

int i = 0;
while (i < 10) {
    if (i == 4) break;
    printf("%d\n", i);
    i++;
}
0
1
2
3

The loop stops at 4, so why did we write i < 10?

The Condition Is the Normal Exit, break Is the Special Exit

The two answer different questions:

  • i < 10 says “keep going while i is under 10”: the planned range.
  • if (i == 4) break; says “but if something special happens, stop now.”

Think of driving. “Drive until we reach the destination” (i < 10) is the plan. “Stop at a red light” (break) is the exception. We don’t delete the destination from the plan because a red light might appear.

In this toy example we could simply write i < 4. Real programs rarely know the stopping point in advance, as the next examples show.

Note

Rule. break leaves the innermost loop at once and skips the rest of the body. The program continues after the loop.

Four Short Examples

(The snippets go inside main, with #include <stdio.h>. Each was compiled with gcc -Wall -Wextra and run.)

1. Search: Stop When We Find It

We don’t know where 42 is, or whether it is there at all. We stop at the first match:

int arr[] = {7, 19, 42, 5};
int found = -1;
for (int i = 0; i < 4; i++) {
    if (arr[i] == 42) {
        found = i;
        break;
    }
}
printf("%d\n", found);
2

Without break the loop would keep scanning for no reason. If 42 is missing, found stays -1.

2. Read Numbers Until 0

The condition means “keep reading while the input is valid”. break means “the user typed 0”:

int x, sum = 0;
while (scanf("%d", &x) == 1) {
    if (x == 0) break;
    sum += x;
}
printf("%d\n", sum);

For the input 5 10 0 99:

15

The 0 stops the loop, so 99 is never added.

3. Retry with a Limit

“At most 3 tries” is the plan; “stop as soon as it works” is the discovery. Here the second try pretends to succeed:

int tries = 0;
while (tries < 3) {
    tries++;
    if (tries == 2) break;   // pretend it worked
}
printf("%d\n", tries);
2

Change the test to tries == 5 and the loop ends by its condition, printing 3.

4. An Endless Loop with an Exit

When there is no natural condition (a menu, a game loop), while (1) runs until break:

char cmd;
while (1) {
    scanf(" %c", &cmd);
    if (cmd == 'q') break;
    printf("%c\n", cmd);
}

For the input a b q c:

a
b

Here break is the exit condition. (A real program should also stop when scanf fails, such as at the end of the input.)

Which Stop Goes Where?

Kind of stop Where it goes Example
Planned stop (known up front) loop condition i < 10, scanf(...) == 1
Discovered stop (found mid-loop) break found the item, user typed 0
No natural condition while (1) + break menu loop, game loop

Summary

A while condition says when to keep going in the normal case. A break says stop immediately when a special case shows up. The special case often can’t be checked in the condition, because it is only known once we are inside the loop.

For more practice, see while loop practice problems.

  • The condition is the planned exit; break is the special-case exit.
  • while (1) with a break suits menus and retries.
  • break leaves only the innermost loop.

Innermost loop. The loop closest around the break; outer loops keep running.

Back to top
Stat Mania

Free, hands-on statistics and programming courses that run right in your browser.

Courses

All courses R Programming Statistics Probability

Resources

Practice quizzes Tools & games Quarto docs Course kit on GitHub

Company

Home About Contact Privacy policy Terms of use
© Stat Mania. All rights reserved.
The do-while Loop in C: Run First, Check After
Printing a Comma-Separated List in C (No Trailing Comma, Clean Line Breaks)