Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use

The "if ... else" Statements

Let's look at situations when the word "otherwise" can be used in your decision making!

if (test condition)  
{
      block of one;
      or more C++ statements;
}
else
{
     block of one;
     or more C++ statements;
}

If the test condition is TRUE, the block of statements following the "if" executes.  If the test condition is FALSE, however, the block of statements following the "else" executes instead.  

 

Example:

//example of if ... else
cout<<"Enter the name of your city."
getline (cin, city);
cout<<"Enter the population";
cin>>population;
if (population >= 534187)
{
     cout<<"According to the census, " << city 
            <<" is one of \n the 100 largest cities."
}
else
{
     cout<<"According to the census, " << city 
             <<"is not one of \n the 100 largest cities."
}

 

 

Return to Topic Menu | Computer Science Main Page | MathBits.com  | Terms of Use