Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) switch

 

switch is a keyword to perform a kind of multiple if statements on a certain value. case determines which values can be switch on. If there is no named value to switch on, default can be used optionally.

 

#include <cassert>
#include <iostream>

int main()
{
  const int coin = std::rand() % 2;
  switch (coin)
  {
    case 0: std::cout << "Heads\n"; break;
    case 1: std::cout << "Tail\n" ; break;
    default: assert(!"Should not get here");
  }
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict