Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) break

 

break is a keyword to

  1. end a for-loop
  2. end a while-loop
  3. end a switch-statement

 

 

 

 

 

break a for-loop

 

#include <cstdlib>

int main()
{
  for(int i=0; i!=100; ++i)
  {
    if (i==69 && std::rand()%2==0) break;
  }
}

 

 

 

 

 

break a while-loop

 

#include <cstdlib>

int main()
{
  while(1)
  {
    const std::string s = AskUserForString();
    if (s == "quit") break;
  }
}

 

 

 

 

 

break a switch-statement

 

#include <cstdlib>
#include <iostream>

int main()
{
  switch(std::rand()%2)
  {
    case 0: std::cout << "Threw heads. " << std::endl; break;
    case 1: std::cout << "Threw tail. " << std::endl; break;
  }
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict