- 2.1 Introduction
- 2.2 First Program in C++: Printing a Line of Text
- 2.3 Modifying Our First C++ Program
- 2.4 Another C++ Program: Adding Integers
- 2.5 Memory Concepts
- 2.6 Arithmetic
- 2.7 Decision Making: Equality and Relational Operators
- 2.8 Wrap-Up
- Summary
- Self-Review Exercises
- Answers to Self-Review Exercises
- Exercises
- Making a Difference
2.7 Decision Making: Equality and Relational Operators
We now introduce a simple version of C++'s if statement that allows a program to take alternative action based on whether a condition is true or false. If the condition is true, the statement in the body of the if statement is executed. If the condition is false, the body statement is not executed. We'll see an example shortly.
Conditions in if statements can be formed by using the equality operators and relational operators summarized in Fig. 2.12. The relational operators all have the same level of precedence and associate left to right. The equality operators both have the same level of precedence, which is lower than that of the relational operators, and associate left to right.
Fig 2.12. Equality and relational operators.
Standard algebraic equality or relational operator |
C++ equality or relational operator |
Sample C++ condition |
Meaning of C++ condition |
Relational operators |
|||
> |
> |
x > y |
x is greater than y |
< |
< |
x < y |
x is less than y |
≤ |
>= |
x >= y |
x is greater than or equal to y |
≥ |
<= |
x <= y |
x is less than or equal to y |
Equality operators |
|||
= |
== |
x == y |
x is equal to y |
≠ |
!= |
x != y |
x is not equal to y |
Using the if Statement
The following example (Fig. 2.13) uses six if statements to compare two numbers input by the user. If the condition in any of these if statements is satisfied, the output statement associated with that if statement is executed.
Fig 2.13. Comparing integers using if statements, relational operators and equality operators. (Part 1 of 2.)
1 // Fig. 2.13: fig02_13.cpp 2 // Comparing integers using if statements, relational operators 3 // and equality operators. 4 #include <iostream> // allows program to perform input and output 5 6 |
using Directives
Lines 6–8
|
are using directives that eliminate the need to repeat the std:: prefix as we did in earlier programs. We can now write cout instead of std::cout, cin instead of std::cin and endl instead of std::endl, respectively, in the remainder of the program.
In place of lines 6–8, many programmers prefer to use the directive
|
which enables a program to use all the names in any standard C++ header (such as <iostream>) that a program might include. From this point forward in the book, we'll use the preceding directive in our programs.
Variable Declarations and Reading the Inputs from the User
Lines 13–14
|
declare the variables used in the program.
The program uses cascaded stream extraction operations (line 17) to input two integers. Remember that we're allowed to write cin (instead of std::cin) because of line 7. First a value is read into variable number1, then a value is read into variable number2.
Comparing Numbers
The if statement in lines 19–20
|
compares the values of variables number1 and number2 to test for equality. If the values are equal, the statement in line 20 displays a line of text indicating that the numbers are equal. If the conditions are true in one or more of the if statements starting in lines 22, 25, 28, 31 and 34, the corresponding body statement displays an appropriate line of text.
Each if statement in Fig. 2.13 has a single statement in its body and each body statement is indented. In Chapter 4 we show how to specify if statements with multiple-statement bodies (by enclosing the body statements in a pair of braces, { }, creating what's called a compound statement or a block).
White Space
Note the use of white space in Fig. 2.13. Recall that white-space characters, such as tabs, newlines and spaces, are normally ignored by the compiler. So, statements may be split over several lines and may be spaced according to your preferences. It's a syntax error to split identifiers, strings (such as "hello") and constants (such as the number 1000) over several lines.
Operator Precedence
Figure 2.14 shows the precedence and associativity of the operators introduced in this chapter. The operators are shown top to bottom in decreasing order of precedence. All these operators, with the exception of the assignment operator =, associate from left to right. Addition is left-associative, so an expression like x + y + z is evaluated as if it had been written (x + y) + z. The assignment operator = associates from right to left, so an expression such as x = y = 0 is evaluated as if it had been written x = (y = 0), which, as we'll soon see, first assigns 0 to y, then assigns the result of that assignment—0—to x.
Fig 2.14. Precedence and associativity of the operators discussed so far.
Operators |
Associativity |
Type |
|||
() |
[See caution in Fig. 2.10] |
grouping parentheses |
|||
* |
/ |
% |
left to right |
multiplicative |
|
+ |
- |
left to right |
additive |
||
<< |
>> |
left to right |
stream insertion/extraction |
||
< |
<= |
> |
>= |
left to right |
relational |
== |
!= |
left to right |
equality |
||
= |
right to left |
assignment |