if Statements

In general, programs are linear. In other words, the execution starts at the first line of the main() function and continues line by line up to the last line in that function. We just looked at while, do-while and for loops which altered this strict linear sequence. The if statement is another way of altering the natural linear flow of a program.

Look at these examples of programs with if statements :

// file : if1.C
#include <iostream.h>

int main()
{
  int x;

  cout << "Enter a number : ";
  cin >> x;
  if (x == 0)
    cout << "You entered a 'zero'." << endl;

  return(0);
}

So, the general syntax in the above program is :

if (expr)
  statement;

Semantically, it means : if ``expr'' is true, then ``statement'' gets executed. The ''expr'' in an if statement is a boolean expression as opposed to an arithmetic expression. These boolean expressions are the same types of expressions we used with the loops last week. They evaluate to either true of false. Note that ``expr'' can be a compound expression (I'll explain later) and ``statement'' can be a compound statement. A compound statement is a sequence of statements enclosed between a { and }. And since an if statement is in fact a statement, it can be nested inside other if statements.

Here's a slightly different if statement :

// file : if2.C
#include <iostream.h>

int main()
{
  int x;

  cout << "Enter a number : ";
  cin >> x;
  if (x == 0)
    cout << "You entered a 'zero'." << endl;
  else
    cout << "You did not enter a 'zero'." << endl;

  return(0);
}


Syntactically, we have :

if (expr)
  statement1;
else
  statement2;

Semantically, this means : if ``expr'' is true, then ``statement1'' gets executed, else ``statement2'' gets executed. Note that ``statement1'' and ``statement2'' are mutually exclusive. This means that one and only one of the two statements will be executed.

And finally, we have something like this :

// file : if3.C
#include <iostream.h>
int main()
{
  int x;

  cout << "Enter a number : ";
  cin >> x;
  if (x == 0)
    cout << "You entered a 'zero'." << endl;
  else if (x == 1)
    cout << "You entered a 'one'." << endl;
  else if (x == 2)
    cout << "You entered a 'two'." << endl;
  else if (x == 3)
    cout << "You entered a 'three'." << endl;
  else
    cout << "You did not enter a 0,1,2, or 3." << endl;

  return(0);
}

General syntax :

if (expr1)
  statement1;
else if (expr2)
  statement2;
else if (expr3)
  statement3;
else if (expr4)
  statement4;
else
  statement5;

Semantically, the above structure is intuitively as you would think it is. These statements are also mutually exclusive. All the three above examples each contain exactly one if statement. The first one is a simple if statement. The last two are compound if statements. Note that in the last example, you may have as many else if (expr) statement clauses as you wish.

To make if statements that are not mutually exclusive, you need to remove the else clauses and replace them with many simple if statements.

Ambiguity with if statements.