The if Structure

Type in the following code. Do not cut and paste it. Typing it in yourself helps you understand and remember syntax and structures better. Setup your project as normal, and name everything as "ifonly.*"


// App to demonstrate if structures

#include <iostream>  // STL iostream header

int main ()  // main function

{ // Begin main
   int weight = 0;
   // Define an integer labeled weight, initialize it to 0

   cout << endl << "Enter your weight in pounds (use an integer): ";
   // Ask the user for input

   cin >> weight;
   // Take input and put its value into weight
   
   // Explanation of this part follows in regular document
   if (weight <= 0 || weight > 500)
      cout << "You are a liar!" << endl;
   if (weight > 0 && weight < 50)
      cout << "Eat more!" << endl;
   if (weight >= 50 && weight < 100)
      cout << "I hope you are short! " << endl;
   if (weight >= 100 && weight < 200)
      cout << "Quite Average." << endl;
   if (weight >= 200 && weight < 300)
      cout << "I hope you are tall." << endl;
   if (weight >= 300 && weight <= 500)
      cout << "Into sumo?"  << endl;
   
   return 0; // Properly exit program

} // End main

#include <iostream>  // STL iostream header
int main ()  // main function
{ // Begin main
   int weight = 0;
   cout << endl << "Enter your weight in pounds (use an integer): ";
   cin >> weight;

Okay, nothing new in this code. We have defined and initialized the variable weight. It is always a good practice to initialize variables upon definition. The endl at the beginning of the cout statement is for formatting purposes only, we just want to make the output look nice and a little spaced out.

   if (weight <= 0 || weight > 500)
      cout << "You are a liar!" << endl;

In this segment we see a basic if structure. In pseudocode, we would say: if weight is less than or equal to zero or weight is greater than 500, display "You are a liar!" What we want to do is make sure the input given is within a reasonable range of normal weights. Notice the OR ( || ) operand, and the fact that you must explicitly state both variables of each conditional expression, not ( 0 < weight < 500 ). This WILL NOT evaluate at all, and will generate a compiler error.

The basic syntax of an if structure is like this:

if (conditional expression)
   statement;

If the conditional expression evaluates to true, then do the single statement that immediately follows. If the conditional expression evaluates to false, skip the next single statement.

Just a single statement? How is that so powerful? Well, by the syntax, only a single statement can be done in an if structure. But, you can enclose multiple statements within braces ( { , } ) and the compiler will treat it as the one statement for the if structure, as you'll see in later examples.

This would also be a good time to point out the difference between the assignment operator (=) and the equality operator (==). This is one common error that even experienced programmers often make. An example of the problem this would cause is this:

if ( pay == 35000 )
   bonus = true;

This works well. If the employee's pay is equal to 35,000, that employee will recieve a bonus. But...

if ( pay = 35000 )
   bonus = true;

This, on the other hand, does not. It will still compile with no warnings or errors, but (potentially serious) logical errors will occur, which may or may not cause the program to crash. Here is why. The assignment operator (=) is assigning the value of 35,000 to the variable pay. No matter what the pay was before, it was just changed to 35,000. Good for that new employee, bad for the CEO.

Then, worse, because any non-zero number evaluates to true, the if conditional expression is satisfied as true, and the bonus code is executed, every time. Everybody gets a Christmas bonus, regardless of what their pay was before this operation! You can easily see where this might cause serious problems. What if the variable was used later as a divisor, and you accidently assigned a zero to it, causing a divide-by-zero error, and a program crash. Watch very carefully for this common error.

The rest of the program code is just figuring out what range the input given is and displaying a different reply.

   if (weight > 0 && weight < 50)
      cout << "Eat more!" << endl;

If the weight is between 1 and 49. Note, because we are using greater than and less than, without the "or equal to" part, the number given is not included in the range. This is to prevent having duplicate truth values in two different if structures (without this, a given input of 50 would display two line items because two separate if structures determined their conditional expression to be true).

   if (weight >= 50 && weight < 100)
      cout << "I hope you are short! " << endl;
   if (weight >= 100 && weight < 200)
      cout << "Quite Average." << endl;
   if (weight >= 200 && weight < 300)
      cout << "I hope you are tall." << endl;
   if (weight >= 300 && weight <= 500)
      cout << "Into sumo?"  << endl;

You get the idea for the rest. Now, this is not the best way to tackle this problem, and is only presented like this to demonstrate how an if structure functions at the basic level. An if/else structure would be better suited for a program such as this, and I will allow you to do it for yourself at the end of the next section. So on to using if and else together.