Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) else

 

else is a Keyword to allow conditional program flow.else will always follow an if statement and is optional to it.

 

if ( /* condition */ )
{
  //Do this when condition is true
}
else
{
  //Do this when condition is false
}

 

 

 

 

 

Example

 

#include <iostream>

int main()
{
  //Draw a random number
  const int x = std::rand();

  if (x % 2 == 0)
  {
    std::cout << " The value of " << x << " is even." << std::endl;
  }
  else
  {
    std::cout << " The value of " << x << " is odd." << std::endl;
  }
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict