INFORMATION FROM DR. EBRAHIMI'S C++ PROGRAMING EASY WAYS

Home                  ABOUT US                    CONTACT US                  TUTORIAL

CHAPTER 4

 

Decision Making: making programs intelligent

 

 

Have you heard of computers that beat chess champions or mimic experts? The decision making statement responsible for making a program an intelligent entity is known as the "if" statement. However, not using the proper decision-making can cause a program to fail or possibly become dumb.  This is known as a computer error.  An example of a program error is to print or display a paycheck for an employee who worked regular hours with a negative amount of money.    

 

 

MAKING THE DECISION

 

If given an employee's salary, how would you determine the tax rate? If given the number of hours an employee worked, how would you determine the overtime hours? If you were asked whether you want to continue, or to stop what you were doing, what would you answer?  If you were asked to enter your password and you typed either the right or the wrong password, what would you expect to happen?  If given a menu with a list of items, how would you make a selection? These are a few examples of decision-making. They are used in a similar fashion in programming.

 

 

HOW DO YOU MAKE A DECISION IN C/C++?

 

In C/C++, decision making is done by using an if or a switch statement. The if statement uses the form shown in Figure 4.1.

 

 

Text Box: if( expression ){
                //Body of if ( Runs only when the if expression is true )
}//IF
else {
            // Body of else ( Runs only when the if expression is false )
}//ELSE
 
Text Box: Figure 4.1 – The if / else structure of C/C++.
 

 

 

 

 

 


 

The expression after the if must evaluate to either true or false. If the expression evaluates to true, the “body of the if”, which is the statement next to parentheses, will be executed. If the expression evaluates to false, the “body of the else” will be executed. If the body of if or the body of else contain more than one statement, then braces {    } should be used. It is good practice to always use braces.

 

 

 

LOOK AT THE FOLLOWING IF STATEMENTS IN C/C++

 

Text Box: if( grosspay > 1000 ) 
taxrate = 0.20;
else 
taxrate = 0.10;

  

 

 

 

 

 

Text Box: Figure4.2a –Assign taxrate the value of 0.20 if grosspay is greater than 1000 otherwise assign taxrate the value of 0.10.

  

 

 

Text Box: if( hoursworked > 40)
overtimehours = hoursworked-40;
else
overtimehours=0.0;

  

 

 

 

 

 

Text Box: Figure 4.2b – Assign overtimehours the value of hoursworked – 40 if hoursworked is greater than 40 otherwise assign the value of 0.0 to overtime hours.

  

 

 

 

 

 

 

 

Text Box: if( candidateInitial == ‘H’ )
hc=hc+1;
else
oc=oc+1;
Text Box: Figure 4.2c – If value of candidateInitial is equal to H then increment the counter named hc by the value of one (1) and assign the value back to hc. If the value of candidateInitial is anything other than H, then add one to the value of the counter named oc and assign the value back to oc.

  

 

 

 

 

 

 

 

 

 

 

 


 

WHAT DOES THE EXPRESSION “IF” CONSIST OF ?

 

The expression if, compares two values using the comparison operators of Table 4.1 listed below.


 

 

 

Common Value Comparisons of IF Expression

 

 

equality        = =

 

not equal                     !=

 

less than       <

 

less than or equal        <=

Text Box: Table 4.1 – Common comparison operators of the if expression.

 

greater than  >

 

 

greater than or equal   >=

 

 

 

 

The result of the expression can be either true or false. In C/C++, anything besides zero is considered to be true.  Therefore, the number zero is false and any other number is true. The expression if can be made more complex when making several evaluations or even an assignment statement.  

 

 

GENERAL SYNTAX OF AN IF STATEMENT

 

The if statement syntax is illustrated by Figure 4.3a and 4.3b.

 

Text Box: if( expression)
statement;
else 
statement;
 
 
 
 
Text Box: if ( expression ) 
statement;
 

  

 

 

 

 

 

 

Text Box: Figure 4.3a – if statement syntax along with the else statement syntax.
Text Box: Figure 4.3b – if statement syntax without else statement syntax.

  

 

 

 

 


 

The statement after if or else can be followed by any other statement, such as an assignment, a loop, or even another if statement.

 

COMPARISON OF TWO NUMBERS: FIND THE MINIMUM

 

In the example below, two numbers are compared to each other to determine whether the first number is less than the second number. If the answer is true the first statement will be executed; otherwise, the statement after the else will be executed.

Text Box: if( firstnumber < secondnumber )
cout<< “ THE FIRST NUMBER IS THE MINIMUM.”;
else 
cout<< “ THE SECOND NUMBER IS THE MINIMUM.”;

  

 


 

 

Text Box: Figure 4.4 – if statement conditional checking for the minimum.

  


 

What happens if the two numbers are equal? In the above statement the else part will be executed because the RESULT OF THE STATEMENT firstnumber < secondnumber is not true.  Therefore, the statement THE SECOND NUMBER IS THE MINIMUM will be displayed. To avoid displaying the second number is the minimum, it is possible to use an assignment to set the minimum value and then display the minimum value at the end.  In this case, both are evaluated as the minimum.

Text Box: if ( firstnumber < secondnumber ) 
minimum = firstnumber;
else 
minimum = secondnumber;

  

 

 

 

 

Text Box: 1.      #include <iostream>
2.      using namespace std;
3.      main(){
4.            int firstnumber, secondnumber, minimum;
5.            cout <<  “ENTER TWO NUMBERS: ”;
6.            cin >> firstnumber >> secondnumber;
7.            if( firstnumber < secondnumber ) 
8.                 minimum = firstnumber;                      
9.            else  
10.             minimum = secondnumber;
11.        cout << “THE MINIMUM IS ” << minimum << endl; 
12.        return 0;
13.  }//MAIN
Text Box: Figure 4.5 – Making the first and the second number the minimum.

  

 

 


 

   

 

 

 

 

 

 

 

 

 

 

Text Box: Figure 4.6a – Program that discovers the minimum value of two integer variables.
Text Box: Figure 4.6b –Sample output of Figure 4.6a for example variables.
Text Box: ENTER TWO NUMBERS: 12   20
THE MINIMUM IS 12
 
ENTER TWO NUMBERS: 106  211
THE MINIMUM IS 106
 
ENTER TWO NUMBERS: 501  312
THE MINIMUM IS 312
 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

FIND THE MINIMUM OF TWO NUMBERS: CASE WHERE BOTH ARE EQUAL

 

When finding the minimum of two numbers and the numbers are equal; we do not want to display either as the minimum. Therefore, we need to include an additional if statement to the program of figure 4.6a to evaluate it if the numbers are equal, as illustrated in figure 4.7a.

 

Text Box: 1.      #include <iostream>
2.      using namespace std;
3.      main(){
4.            int firstnumber, secondnumber, minimum;
5.            cout << “ENTER TWO NUMBERS: ”;
6.            cin >> firstnumber >> secondnumber;
7.            if( firstnumber < secondnumber ) 
8.                 minimum = firstnumber;
9.            else 
10.             minimum = secondnumber;
11.        if ( firstnumber = = secondnumber )
12.              cout << “BOTH NUMBERS ARE EQUAL.” << endl;
13.        else
14.              cout << “ THE MINIMUM IS ”<< minimum << endl; 
15.        return 0;
16.  }//MAIN
 
Text Box: Figure 4.7a – Program that finds the minimum of two variables and evaluates the possibility that the first and the second number are equal.

  

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  

 


 


IF WITH A COMPOUND STATEMENT

 


 

Text Box:  if( expression ){ 
      statement 1;  
      statement 2;
      //etc.
 }//IF
 else {
      statement3;
      statement4;
      //etc. 
 }//ELSE

An if statement may contain more than one statement whether its condition is true or false. Braces that make up the compound statement surround each group of statements; however, braces aren’t necessary when only one statement is placed after the condition of if or else

 

 

 

 

 

 

 

 

 

 

 

 

Text Box: Figure 4.8 – if statement syntax when more than one statement is used for an if / else block.

  

 

 

 

 


 

FIND THE MINIMUM AND MAXIMUM OF TWO NUMBERS: USE OF A COMPOUND STATEMENT

 

In the example shown in figure 4.9a, the body of if and else contain more than one statement, this represents a compound statement.


 

 

Text Box: 1.      #include <iostream>
2.      using namespace std;
3.      main(){
4.            int firstnumber, secondnumber, min, max;
5.            cout << “ENTER TWO NUMBERS: ”; 
6.            cin  >> firstnumber >> secondnumber;
7.            if( firstnumber < secondnumber ){
8.                 min = firstnumber;
9.                 max = secondnumber;    }//IF
10.        else {
11.             min  = secondnumber;
12.             max =  firstnumber;        }//ELSE
13.        if ( firstnumber == secondnumber )
14.             cout << “BOTH ARE EQUAL.” << endl;
15.        else {
16.             cout << “MIN IS  ” << min << endl;
17.             cout << “MAX IS ” << max << endl;       }//ELSE
18.      return 0;
19.  }//MAIN
Text Box: Figure 4.9a – Program containing compound statements within an if / else construction.

  

 

 

 

 

 

 

 

 

 

 

  

 

 

 

Hosted by www.Geocities.ws

1