Cpp - Statement break statement

Introduction

The break statement causes a loop to end immediately.

This statement appears inside a loop.

Demo

#include <iostream> 
 
int main() /* www  .  j a  v a2 s. c om*/
{ 
    int counter = 0; 
    int multiples = 0; 
 
    while (true) 
    { 
           counter++; 
           if (counter % 14 == 0) 
           { 
                std::cout << counter << " "; 
                multiples++; 
           } 
           if (multiples > 19) 
                break; 
    } 
 
    std::cout << "\n"; 
    return 0; 
}

Result

A counter variable is incremented from 0 upward, and every time the variable is evenly divisible by 14, its value is displayed.

The break statement causes the loop to end.

Related Topics