Section 2 - The if/else Statement

3B.2.1 The if/else Statement

The if statement is a one-way choice: if the condition is true we do something, if not we don't. We would now like an either/or, or two-way choice: if the condition is true, do this, if not, do that. Once we can accomplish that, we can respond one way if the user is under age, and another way if the user is 21 or greater.

For this we add an else statement:

#include <iostream>
#include <string>
using namespace std;

int main()
{
int age;

cout << "How old are you? ";
cin >> age;

if (age >= 21)
cout << "Would you like a beer?\n";
else
cout << "Sorry, I can't serve you.\n";
cout <<"Have a nice day.\n\n";
}

Try the program after making this change and see how it behaves.

Here's another example.  Remember how to generate a remainder? Use the modulo operator, %. Here is a console application that gets a number from the user and tells the user whether or not the number is even or odd.  Make sure you can follow the logic - why does this program give the correct answer?  You have to understand the % operator to answer that question.

#include <iostream>
#include <string>
using namespace std;

int main()
{
int userValue;

cout << "Enter an int: ";

// get the user's number
cin >> userValue;

if ( userValue % 2 == 0 )
// if we divide by 2 with no remainder, it's even
cout << userValue << " is even.\n\n";
else
// if we divide by 2 with a 1 remainder, it's odd
cout << userValue << " is odd.\n\n";
}

3B.2.2 Indentation of if/else

If there are multiple statements inside the if (or else) we need braces.  Notice the placement of the braces and indentation:

if ( myAge > 65 )
{
cout << "Welcome to Retirement.";
cout << "Would you like to join AARP?";
}
else
{
// whatever goes here ...
}

There are a few other acceptable if/else styles (see your text or the style booklet for this course). 

Point Penalty Disclosure

You must be consistent and use an acceptable style or you will lose one point for every instance of an incorrect style.

Here are a few brace styles for if and if/else statements  that I won't, i.e., WILL NOT, accept:

// double indentation. bad:
if ( myAge > 65 )
{
cout << " Welcome to Retirement.";
cout << "Would you like to join AARP?";
}

nor this

// after opening brace, nothing should be on rest of line. bad:
if ( myAge > 65 )
{   cout << " Welcome to Retirement.";
cout << "Would you like to join AARP?";
}

nor this

// body of if on same line as if:  bad:
if ( myAge > 65 ) cout << " Welcome to Retirement."

That is, don't place anything on the same line as an opening brace after you type it. The reason is it either causes a jagged and confused inner block or a hard-to-read outer framework.

3B.2.3 Application: Testing for Non-Numeric Input

If you are asked to get a number from the user in this course, you can usually assume that the user gives you a real number, and not something illegal, like "dork" or "five".  You are not expected to inspect the string for valid characters.  If you come up with a complicated algorithm for doing this that makes your code hard to read, I'll probably deduct points, so don't try to protect against non-numeric input.

Typically, it is not that easy to check a string and determine if the entire string is a valid number. 

However, there is one thing you can do that is not too hard:  using the above techniques, you can see if the beginning of the string can be interpreted as a number.  For example, the following strings begin with valid numbers:

"123.455" →  123.455
"-87"  → -87
"   234   " →   234
"5Hello There" →  5
"54.32Hello There"  → 54.32
"123.123.123.123"  → 123.123

If you want to test this, you can do it by checking the expression istringstream(s) >> c to see if it is true (the string starts with a number) or false (it does not):

if ( istringstream(userString) >> num )
cout << "Yes, I was able to convert the first part to the number: "
<< num << endl;
else
cout << "No, "
<< userString << " begins with non-numeric content. " << endl;

In short, the value of the expression A >> B when you are doing a string conversion will be true if the conversion was successful.  Here is a full program that demonstrates it:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main()
{
string userString;
double num;

// these should be the same if conversion worked
cout << "Type in something and I'll tell you if "
"\nthe first part can be converted to a number: ";

// compute using the number
getline(cin, userString);

if ( istringstream(userString) >> num )
cout << "Yes, I was able to convert the first part to the number: "
<< num << endl;
else
cout << "No, " << userString
<< " begins with non-numeric content. " << endl;

return 0;
}

I'll let you run this with different input to see how it works.