Selection structures: if and if-else statements

Previous Index Next

Single-way selection Two-way selection Nesting selectors

The selection structures provided in C++ are similar to those in Pascal.


if structureSingle-way selection Top

C++ control structure Equivalent Pascal control structure
if (<condition>)
  <statement_1>;
if <condition> then
  <statement_1>

<statement_1> will be executed if <condition> is non-zero (i.e. true). Note that in C++, then is not required but the parentheses around the condition is necessary.

<statement_1> can be a compound statement if more than one statement are to be executed when the condition is true. This applies to all kinds of control structures.

A compound statement is a group of statements enclosed by a pair of braces. There is no semicolon after the right brace.

Sample program:

Program 11: Using the if selection structure
C++ version Pascal version
#include <iostream.h>

main()
{
  int OT_hours, wages = 8000;

  cout << "Number of overtime hours? ";
  cin >> OT_hours;

  if (OT_hours > 30) {
    cout << "Maximum OT hours is 30" << endl;
    OT_hours = 30;
  }

  wages += OT_hours * 50;
  cout << "Wages: $" << wages << endl;

  return 0;
}
program prog_11(input, output);
var
  OT_hours, wages: integer;

begin
  wages := 8000;
  write('Number of overtime hours? ');
  readln(OT_hours);

  if OT_hours > 30 then begin
    writeln('Maximum OT hours is 30');
    OT_hours := 30
  end;


  wages := wages + OT_hours * 50;
  writeln('Wages: $', wages);
end.
Sample output 1 (text in red is entered by user):
Number of overtime hours? 50
Maximum OT hours is 30
Wages: $9500
Sample output 2 (text in red is entered by user):
Number of overtime hours? 12
Wages: $8600

if-else structureTwo-way selection Top

C++ control structure Equivalent Pascal control structure
if (<condition>)
  <statement_1>;
else
  <statement_2>;
if <condition> then
  <statement_1>
else
  <statement_2>

If <condition> is non-zero (i.e. true), <statement_1> is executed. Otherwise, <statement_2>  is executed.

A statement using the conditional operator can be replaced by this selection structure. For example, Program 9 can be rewritten as follows:

Program 12: Using the if-else selection structure
C++ version Pascal version
#include <iostream.h>
main()
{
  int x, y, min;

  cout << "Enter x: ";
  cin >> x;
  cout << "Enter y: ";
  cin >> y;

  if (x < y)
    min = x;
  else
    min = y;

  cout << x;
  if (x > y)
    cout << " > ";
  else
    cout << " <= ";
  cout << y << endl;
  cout << min << " is the smaller number." << endl;

  return 0;
}
program prog_12(input, output);
var
  x, y, min: integer;

begin
  write('Enter x: ');
  readln(x);
  write('Enter y: ');
  readln(y);

  if x < y then
    min := x
  else
    min := y;

  write(x);
  if x > y then
    write(' > ')
  else
    write(' <= ');
  writeln(y);
  writeln(min, ' is the smaller number.')
end.
Sample output 1 (text in red is entered by user):
Enter x: 10
Enter y: 5
10 > 5
5 is the smaller number.
Sample output 2 (text in red is entered by user):
Enter x: 4
Enter y: 7
4 <= 7
4 is the smaller number.

Nesting selectors Top

Like Pascal, C++ also supports nested control structures.

We can use nested if-else statements for multiple cases. For example, the following program outputs a grade according to the input score. The indentation shows how the if-else statements are nested.

Program 13: Using nested if-else statements
C++ version Pascal version
#include <iostream.h>
main()
{
  int score;
  char grade;

  cout << "Score? ";
  cin >> score;

  if (score >= 80)
    grade = 'A';
  else                   // score < 80
    if (score >= 70)
      grade = 'B';
    else                 // score < 70
      if (score >= 60)
        grade = 'C';
      else               // score < 60
        if (score >= 50)
          grade = 'D';
        else             // score < 50
          grade = 'F';
  cout << "Grade: " << grade << endl;

  return 0;
}
program prog_13;
var
  score: integer;
  grade: char;

begin
  write('Score? ');
  readln(score);

  if score >= 80 then
    grade := 'A'
  else                   (* score < 80 *)
    if score >= 70 then
      grade := 'B'
    else                 (* score < 70 *)
      if score >= 60 then
        grade := 'C'
      else               (* score < 60 *)
        if score >= 50 then
          grade := 'D'
        else             (* score < 50 *)
          grade := 'F';
  writeln('Grade: ', grade)
end.
Sample output 1 (text in red is entered by user):
Score? 74
Grade: B
Sample output 2 (text in red is entered by user):
Score? 32
Grade: F

But to enhance readability, the nested if-else statements are usually written as follows:
if (score >=80)
  grade = 'A';
else if (score >= 70)
  grade = 'B';
else if (score >= 60)
  grade = 'C';
else if (score >= 50)
  grade = 'D';
else
  grade = 'F';

Previous Index Next