| INFORMATION FROM DR. EBRAHIMI'S C++ PROGRAMING EASY WAYS |
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.
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.
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.



|
Common Value Comparisons of IF Expression
|
|
|
equality = = |
not equal != |
|
less than < |
less than or equal <= |
|
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.
The if statement syntax is illustrated by Figure 4.3a and 4.3b.




The statement after if or else can be followed by any other statement, such as an assignment, a loop, or even another if statement.
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.

![]()
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.


![]()
![]()

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.




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.

![]()