Relational Operators

Sometimes you won’t want every statement in your C++ program to execute every time the program runs. So far, every program in this book has executed from the top and has continued, line-by-line, until the last statement completes. Depending on your application, you might not always want this to happen.

Programs that don’t always execute by rote are known as data- driven programs. In data-driven programs, the data dictates what the program does. You would not want the computer to print every employee’s paychecks for every pay period, for example, because some employees might be on vacation, or they might be paid on commission and not have made a sale during that period. Printing paychecks with zero dollars is ridiculous. You want the computer to print checks only for employees who have worked.

This chapter shows you how to create data-driven programs. These programs do not execute the same way every time. This is possible through the use of relational operators that conditionally control other statements. Relational operators first “look” at the literals and variables in the program, then operate according to what they “find.” This might sound like difficult programming, but it is actually straightforward and intuitive.

This chapter introduces you to

  • Relational operators

  • The i f statement

  • The el se statement

Not only does this chapter introduce these comparison com- mands, but it prepares you for much more powerful programs, possible once you learn the relational operators.

Defining Relational Operator s

Relational operators compare data.

In addition to the math operators you learned in Chapter 8, “Using C++ Math Operators and Precedence,” there are also opera- tors that you use for data comparisons. They are called relational operators, and their task is to compare data. They enable you to determine whether two variables are equal, not equal, and which one is less than the other. Table 9.1 lists each relational operator and its meaning.

Table 9.1. The relational operators.

Operator Description

== Equal to

> Greater than

< Less than

>= Greater than or equal to

<= Less than or equal to

! = Not equal to

The six relational operators form the foundation of data com- parison in C++ programming. They always appear with two literals, variables, expressions (or some combination of these), one on each side of the operator. These relational operators are useful and you should know them as well as you know the +, - , * , / , and % mathemati- cal operators.

Relational Operators - 图1

Examples

  1. Relational Operators - 图2Assume

    that a program initializes four variables as follows:

i nt a=5;

i nt b=10; i nt c=15; i nt d=5;

The following statements are then True:

a is equal to d, so a == d b is less than c, so b < c

c is greater than a, so c > a

b is greater than or equal to a, so b >= a d is less than or equal to b, so d <= b

b is not equal to c, so b ! = c

Relational Operators - 图3These are not C++ statements; they are statements of com- parison (relational logic) between values in the variables.

Relational logic is easy.

Relational logic always produces a True or False result. In C++, unlike some other programming languages, you can directly use the True or False result of relational operators inside other expressions. You will soon learn how to do this; but for now, you have to understand only that the following True and False evaluations are correct:

  • A True relational result evaluates to 1.

  • A False relational result evaluates to 0.

Each of the statements presented earlier in this example evaluates to a 1, or True, result.

  1. If you assume the same values as stated for the previous example’s

    four variables, each of the value’s statements is False (0):

a == b b > c

d < a

d > a a ! = d b >= c

c <= b

Study these statements to see why each is False and evalu- ates to 0. The variables a and d, for example, are exactly equal to the same value (5), so neither is greater or less than the other.

You use relational logic in everyday life. Think of the follow- ing statements:

“The generic butter costs less than the name brand.” “My child is younger than Johnny.”

“Our salaries are equal.”

“The dogs are not the same age.”

Each of these statements can be either True or False. There is no other possible answer.

The i f Statemen t

Relational Operators - 图4Relational Operators - 图5You incorporate relational operators in C++ programs with the if statement. Such an expression is called a decision statement because it tests a relationship—using the relational operators—and, based on the test’s result, makes a decision about which statement to execute next.

The i f statement appears as follows:

i f ( condi t i on )

{ bl ock of one or mor e C++ st at ement s }

The condi t i on includes any relational comparison, and it must be enclosed in parentheses. You saw several relational comparisons earlier, such as a==d, c<d, and so on. The bl ock of one or mor e C++ st at ement s is any C++ statement, such as an assignment or pr i ntf () , enclosed in braces. The block of the i f , sometimes called the body of the i f statement, is usually indented a few spaces for readability. This enables you to see, at a glance, exactly what executes if condi t i on is True.

Thei f statement makes a decision.

If only one statement follows the i f , the braces are not required (but it is always good to include them). The block executes only if condi t i on is True. If condi t i on is False, C++ ignores the block and simply executes the next appropriate statement in the program that follows the i f statement.

Basically, you can read an i f statement in the following way: “If the condition is True, perform the block of statements inside the braces. Otherwise, the condition must be False; so do not execute that block, but continue executing the remainder of the program as though this i f statement did not exist.”

The i f statement is used to make a decision. The block of statements following the i f executes if the decision (the result of the relation) is True, but the block does not execute otherwise. As with relational logic, you also use if logic in everyday life. Consider the statements that follow.

“If the day is warm, I will go swimming.”

“If I make enough money, we will build a new house.” “If the light is green, go.”

“If the light is red, stop.”

Each of these statements is conditional. That is, if and only if the condition is true do you perform the activity.

Relational Operators - 图6

mai n( )

{

i nt age=21; / / Decl ar es and assi gns age as 21. i f ( age=85)

{ cout << “ You have li ved t hr ough a l ot! ” ; }

/ / Remai ni ng pr ogr am code goes her e.

At first, it might seem as though the pr i ntf () does not execute, but it does! Because the code line used a regular assignment operator (=) (not a relational operator, ==), C++ performs the assignment of 85 to age. This, as with all assignments you saw in Chapter 8, “Using C++ Math Operators and Precedence,” produces a value for the expression of 85. Because 85 is nonzero, C++ interprets the i f condition as True and then performs the body of the i f statement.

Confusing the relational equality test (==) with the regular assignment operator (=) is a common error in C++ programs, and the nonzero True test makes this bug even more difficult to find.

Relational Operators - 图7The designers of C++ didn’t intend for this to confuse you. They want you to take advantage of this feature whenever you can. Instead of putting an assignment before an i f and testing the result of that assignment, you can combine the assignment and i f into a single statement.

Test your understanding of this by considering this: Would C++ interpret the following condition as True or False?

i f ( 10 == 10 == 10) ...

Be careful! At first glance, it seems True; but C++ interprets it as False! Because the == operator associates from the left, the program compares the first 10 to the second. Because they are equal, the result is 1 (for True) and the 1 is then compared to the third 10—which results in a 0 (for False)!

Examples

  1. Relational Operators - 图8The

    following are examples of valid C++ i f statements.

If (the variable sal es is greater than 5000 ), then the variable bonus

becomes equal to 500 .

i f ( sal es > 5000)

{ bonus = 500; }

If this is part of a C++ program, the value inside the variable sal es determines what happens next. If sal es contains more than 5000 , the next statement that executes is the one inside the block that initializes bonus . If, however, sal es contains 5000 or less, the block does not execute, and the line follow- ing the i f ’s block executes.

Relational Operators - 图9If (the variable age is less than or equal to 21*) then print* You ar e a mi nor . *to the screen and go to a new line, print* What i s your

gr ade? to the screen, and accept an integer from the keyboard.

i f ( age <= 21)

{ cout << “ You ar e a mi nor .\ n” ; cout << “ What i s your gr ade? “ ; ci n >> gr ade; }

If the value in age is less than or equal to 21, the lines of code within the block execute next. Otherwise, C++ skips the entire block and continues with the remaining program.

Relational Operators - 图10If (the variable bal ance is greater than the variable l ow_bal ance ), then print Past due! to the screen and move the cursor to a new line.

i f ( bal ance > l ow_bal ance)

{ cout << “ Past due!\ n” ; }

If the value in bal ance is more than that in l ow_bal ance , execu- tion of the program continues at the block and the message “Past due! ” prints on-screen. You can compare two variables to each other (as in this example), or a variable to a literal (as in the previous examples), or a literal to a literal (although this is rarely done), or a literal to any expression in place of any variable or literal. The following i f statement shows an expression included in the i f .

Relational Operators - 图11If (the variable pay multiplied by the variable t ax_r at e equals the variable mi ni mum*), then the variable* l ow_sal ar y *is assigned* 1400. 60 *.*

I f ( pay * t ax_r at e == mi ni mum)

{ l ow_sal ar y = 1400. 60; }

The precedence table of operators in Appendix D, “C++ Precedence Table,” includes the relational operators. They are at levels 11 and 12, lower than the other primary math operators. When you use expressions such as the one shown in this example, you can make these expressions much more readable by enclosing them in parentheses (even though C++ does not require it). Here is a rewrite of the previous i f statement with ample parentheses:

Relational Operators - 图12If (the variable pay (multiplied by the variable t ax_r at e ) equals the variable mi ni mum*), then the variable* l ow_sal ar y *is assigned* 1400. 60 *.*

I f (( pay * t ax_r at e) == mi ni mum)

{ l ow_sal ar y = 1400. 60; }

  1. The following is a simple program that computes a salesperson’s pay.

    The salesperson receives a flat rate of

Relational Operators - 图13$4.10 per hour. In addition, if sales are more than $8,500, the salesperson also receives an additional $500 as a bonus. This is an introductory example of conditional logic, which depends on a relation between two values, sal es and $8500 .

/ / Fil ename: C9PAY1. CPP

/ / Cal cul at es a sal esper son’ s pay based on hi s or her sal es. #i ncl ude <i ost r eam. h>

#i ncl ude <st di o. h> mai n( )

{

char sal _name[ 20]; i nt hour s;

f l oat t ot al _sal es, bonus, pay;

cout << “ \ n\ n” ; / / Pr i nt t wo bl ank li nes. cout << “ Payr oll Cal cul at i on\ n” ;

cout << “ \ n” ;

/ / Ask t he user f or needed val ues.

cout << “ What i s sal esper son’ s l ast name? “ ; ci n >> sal _name;

cout << “ How many hour s di d t he sal esper son wor k? “ ; ci n >> hour s;

cout << “ What wer e t he t ot al sal es? “ ; ci n >> t ot al _sal es;

bonus = 0; / / I ni t i all y, t her e i s no bonus.

/ / Comput e t he base pay.

pay = 4. 10 * ( f l oat ) hour s; / / Type cast s t he hour s.

/ / Add bonus onl y i f sal es wer e hi gh. i f ( t ot al _sal es > 8500. 00)

{ bonus = 500. 00; }

pr i ntf (“ %s made $%. 2f \ n” , sal _name, pay) ; pr i ntf (“ and got a bonus of $%. 2f ” , bonus) ;

r et ur n 0;

}

This program uses cout , ci n , and pr i ntf () for its input and output. You can mix them. Include the appropriate header files if you do (stdio.h and iostream.h).

The following output shows the result of running this program twice, each time with different input values. Notice that the program does two different things: It computes a bonus for one employee, but doesn’t for the other. The $500 bonus is a direct result of the i f statement. The assignment of $500 to bonus executes only if the value in t ot al _sal es is more than $8500 .

Payr oll Cal cul at i on

- - - - - - - - - - - - - - - - - - -

What i s sal esper son’ s l ast name? Harr i son How many hour s di d t he sal esper son wor k? 40 What wer e t he t ot al sal es? 6050. 64

Harr i son made $164. 00

and got a bonus of $0. 00

Payr oll Cal cul at i on

- - - - - - - - - - - - - - - - - - -

What i s sal esper son’ s l ast name? Rober t son How many hour s di d t he sal esper son wor k? 40 What wer e t he t ot al sal es? 9800

Rober t son made $164. 00

Relational Operators - 图14and got a bonus of $500. 00

  1. When programming the way users input data, it is wise to program

    data validation on the values they type. If they enter a bad value (for instance, a negative number when the input cannot be negative), you can inform them of the problem and ask them to reenter the data.

Not all data can be validated, of course, but most of it can be checked for reasonableness. For example, if you write a student record-keeping program, to track each student’s name, address, age, and other pertinent data, you can check whether the age falls in a reasonable range. If the user enters 213 for the age, you know the value is incorrect. If the user enters - 4 for the age, you know this value is also incorrect.

Not all erroneous input for age can be checked, however. If the user is 21, for instance, and types 22, your program has no way of knowing whether this is correct, because 22 falls in a reasonable age range for students.

Relational Operators - 图15Relational Operators - 图16The following program is a routine that requests an age, and makes sure it is more than 10. This is certainly not a fool- proof test (because the user can still enter incorrect ages), but it takes care of extremely low values. If the user enters a bad age, the program asks for it again inside the i f statement.

/ / Fil ename: C9AGE. CPP

/ / Pr ogr am t hat ensur es age val ues ar e r easonabl e. #i ncl ude <st di o. h>

mai n( )

{

i nt age;

pr i ntf (“ \ nWhat i s t he st udent ’ s age? “) ;

scanf (“ %d” , &age) ; / / Wi t h scanf () , r emember t he &

i f ( age < 10)

{ pr i ntf (“ %c” , ‘ \ x07’ ) ; / / BEEP

pr i ntf (“ *** The age cannot be l ess t han 10 *** \ n”) ; pr i ntf (“ Tr y agai n...\ n\ n”) ;

pr i ntf (“ What i s t he st udent ’ s age? “) ; scanf (“ %d” , &age) ;

}

pr i ntf (“ Thank you. You ent er ed a vali d age. ”) ; r et ur n 0;

}

This routine can also be a section of a longer program. You learn later how to prompt repeatedly for a value until a valid input is given. This program takes advantage of the bell (ASCII 7) to warn the user that a bad age was entered.

Because the \ a character is an escape sequence for the alarm (see Chapter 4, “Variables and Literals” for more informa- tion on escape sequences), \ a can replace the \ x07 in this program.

If the entered age is less than 10, the user receives an error message. The program beeps and warns the user about the bad age before asking for it again.

The following shows the result of running this program. Notice that the program “knows,” due to the i f statement, whether age is more than 10.

What i s t he st udent ’ s age? 3

*** The age cannot be l ess t han 10 *** Tr y agai n...

What i s t he st udent ’ s age? 21

Relational Operators - 图17Thank you. You ent er ed a vali d age.

  1. Unlike many languages, C++ does not include a square math operator.

    Remember that you “square” a number by multi- plying it times itself (3* 3 , for example). Because many com- puters do not allow for integers to hold more than the square of 180, the following program uses i f statements to make sure the number fits as an integer.

The program takes a value from the user and prints its square—unless it is more than 180. The message * Squar e i s not all owed f or number s over 180 * appears on-screen if the user types a huge number.

/ / Fil ename: C9SQR1. CPP

/ / Pr i nt t he squar e of t he i nput val ue

/ / i f t he i nput val ue i s l ess t han 180. #i ncl ude <i ost r eam. h>

mai n( )

{

i nt num, squar e;

cout << “ \ n\ n” ; / / Pr i nt t wo bl ank li nes.

cout << “ What number do you want t o see t he squar e of ? “ ; ci n >> num;

i f ( num <= 180)

{ squar e = num * num;

cout << “ The squar e of “ << num << “ i s “ << squar e << “ \ n” ;

}

i f ( num > 180)

{ cout << ‘ \ x07’ ; / / BEEP

cout << “ \ n* Squar e i s not all owed f or number s over 180 * ” ; cout << “ \ nRun t hi s pr ogr am agai n t r yi ng a small er val ue. ” ;

Relational Operators - 图18}

cout << “ \ nThank you f or r equest i ng squar e r oot s.\ n” ; r et ur n 0;

}

The following output shows a couple of sample runs with this program. Notice that both conditions work: If the user enters a number less than 180, the calculated square appears, but if the user enters a larger number, an error message appears.

What number do you want t o see t he squar e of ? 45

The squar e of 45 i s 2025

Thank you f or r equest i ng squar e r oot s.

What number do you want t o see t he squar e of ? 212

* Squar e i s not all owed f or number s over 180 * Run t hi s pr ogr am agai n t r yi ng a small er val ue. Thank you f or r equest i ng squar e r oot s.

You can improve this program with the el se statement, which you learn later in this chapter. This code includes a redundant check of the user’s input. The variable num must be checked once to print the square if the input number is less than or equal to 180, and checked again for the error message if it is greater than 180.

  1. The value of 1 and 0 for True and False, respectively, can help save

    you an extra programming step, which you are not necessarily able to save in other languages. To understand this, examine the following section of code:

commi ssi on = 0; / / I ni t i ali ze commi ssi on

i f ( sal es > 10000)

{ commi ssi on = 500. 00; }

pay = net _pay + commi ssi on; / / Commi ssi on i s 0 unl ess

/ / hi gh sal es.

You can make this program more efficient by combining the

i f ’s relational test because you know that i f returns 1 or 0:

pay = net _pay + ( commi ssi on = ( sal es > 10000) * 500. 00) ;

This single line does what it took the previous four lines to do. Because the assignment on the extreme right has prece- dence, it is computed first. The program compares the variable sal es to 10000 . If it is more than 10000 , a True result of 1 returns. The program then multiplies 1 by 500. 00 and stores the result in commi ssi on . If, however, the sal es were not

more than 10000 , a 0 results and the program receives 0 from multiplying 0 by 500. 00 .

Whichever value (500. 00 or 0) the program assigns to commi s- si on is then added to net _pay and stored in pay .

The el se Statemen t

The el se statement never appears in a program without an i f statement. This section introduces the el se statement by showing you the popular i f - el se combination statement. Its format is

i f ( condi t i on )

{ A bl ock of 1 or mor e C++ st at ement s } el se

{ A bl ock of 1 or mor e C++ st at ement s }

The first part of the i f - el se is identical to the i f statement. If

condi t i on is True, the block of C++ statements following the i f

executes. However, if condi t i on is False, the block of C++ statements following the el se executes instead. Whereas the simple i f statement determines what happens only when the condi t i on is True, the i f - el se also determines what happens if the condi t i on is False. No matter what the outcome is, the statement following the i f - el se executes next.

Relational Operators - 图19The following describes the nature of the i f - el se :

  • If the condi t i on test is True, the entire block of statements

    following the i f executes.

  • If the condi t i on test is False, the entire block of statements

    following the el se executes.

Relational Operators - 图20

Examples

  1. Relational Operators - 图21The

    following program asks the user for a number. It then prints whether or not the number is greater than zero, using the i f - el se statement.

/ / Fil ename: C9I FEL1. CPP

/ / Demonst r at es i f - el se by pr i nt i ng whet her an

/ / i nput val ue i s gr eat er t han zer o or not. #i ncl ude <i ost r eam. h>

mai n( )

{

i nt num;

cout << “ What i s your number ? “ ;

ci n >> num; / / Get t he user ’ s number .

i f ( num > 0)

{ cout << “ Mor e t han 0\ n” ; } el se

{ cout << “ Less or equal t o 0\ n” ; }

/ / No matt er what t he number was, t he f oll owi ng execut es. cout << “ \ n\ nThanks f or your t i me!\ n” ;

r et ur n 0;

}

There is no need to test for both possibilities when you use an el se . The i f tests whether the number is greater than zero, and the el se automatically handles all other possibilities.

  1. Relational Operators - 图22The

    following program asks the user for his or her first name, then stores it in a character array. The program checks the first character of the array to see whether it falls in the first half of the alphabet. If it does, an appropriate message is displayed.

/ / Fil ename: C9I FEL2. CPP

/ / Test s t he user ’ s f i r st i ni t i al and pr i nt s a message. #i ncl ude <i ost r eam. h>

mai n( )

{

char l ast[ 20] ; / / Hol ds t he l ast name. cout << “ What i s your l ast name? “ ;

ci n >> l ast;

/ / Test t he i ni t i al i f ( l ast[ 0] <= ‘ P’ )

{ cout << “ Your name i s ear l y i n t he al phabet.\ n” ; } el se

{ cout << “ You have t o wai t a whil e f or “

<< “ YOUR name t o be call ed!\ n” ; }

r et ur n 0;

}

Notice that because the program is comparing a character array element to a character literal, you must enclose the character literal inside single quotation marks. The data type on each side of each relational operator must match.

  1. Relational Operators - 图23The

    following program is a more complete payroll routine than the other one. It uses the i f statement to illustrate how to compute overtime pay. The logic goes something like this:

Relational Operators - 图24If employees work 40 hours or fewer, they are paid regular pay (their hourly rate times the number of hours worked). If employees work between 40 and 50 hours, they receive one- and-a-half times their hourly rate for those hours over 40, in addition to their regular pay for the first 40. All hours over 50 are paid at double the regular rate.

/ / Fil ename: C9PAY2. CPP

/ / Comput e t he f ull over t i me pay possi bili t i es. #i ncl ude <i ost r eam. h>

#i ncl ude <st di o. h> mai n( )

{

i nt hour s;

f l oat dt , ht , r p, r at e, pay;

cout << “ \ n\ nHow many hour s wer e wor ked? “ ; ci n >> hour s;

cout << “ \ nWhat i s t he r egul ar hour l y pay? “ ; ci n >> r at e;

/ / Comput e pay her e

/ / Doubl e- t i me possi bili t y i f ( hour s > 50)

{ dt = 2. 0 * r at e * ( f l oat )( hour s - 50) ;

ht = 1. 5 * r at e * 10. 0; } / / Ti me + 1/ 2 f or 10 hour s.

el se

{ dt = 0. 0; } / / Ei t her none or doubl e f or hour s over 50.

/ / Ti me and a hal f. i f ( hour s > 40)

{ ht = 1. 5 * r at e * ( f l oat )( hour s - 40) ; }

/ / Regul ar Pay

i f ( hour s >= 40)

{ r p = 40 * r at e; } el se

{ r p = ( f l oat ) hour s * r at e; }

pay = dt + ht + r p; / / Add t hr ee component s of payr oll .

pr i ntf (“ \ nThe pay i s %. 2f ” , pay) ; r et ur n 0;

}

  1. The block of statements following the if can contain any valid C++

    statement—even another i f statement! This sometimes is handy, as the following example shows.

You can even use this program to award employees for their years of service to your company. In this example, you are giving a gold watch to those with more than 20 years of service, a paperweight to those with more than 10 years, and a pat on the back to everyone else!

/ / Fil ename: C9SERV. CPP

/ / Pr i nt s a message dependi ng on year s of ser vi ce. #i ncl ude <i ost r eam. h>

mai n( )

{

i nt yr s;

cout << “ How many year s of ser vi ce? “ ;

ci n >> yr s; / / Det ermi ne t he year s t hey have wor ked.

i f ( yr s > 20)

{ cout << “ Gi ve a gol d wat ch\ n” ; } el se

{ i f ( yr s > 10)

{ cout << “ Gi ve a paper wei ght\ n” ; } el se

{ cout << “ Gi ve a pat on t he back\ n” ; }

}

r et ur n 0;

}

Don’t rely on the i f within an i f to handle too many condi- tions, because more than three or four conditions can add confusion. You might mess up your logic, such as: “If this is True, and if this is also True, then do something; but if not that, but something else is True, then...” (and so on). The

swi t ch statement that you learn about in a later chapter handles these types of multiple i f selections much better than a long i f within an i f statement does.

Review Question s

The answers to the review questions are in Appendix B.

  1. Relational Operators - 图25Relational Operators - 图26Which

    operator tests for equality?

  2. State whether each of these relational tests is True or False: a.

    4 >= 5

b. 4 == 4

c. 165 >= 165

d. 0 ! = 25

  1. Relational Operators - 图27True

    or false: C++ i s f un prints on-screen when the following statement executes.

i f ( 54 <= 54)

Relational Operators - 图28{ pr i ntf (“ C++ i s f un”) ; }

  1. What is the difference between an i f and an i f - el se state-

    ment?

  2. Does the following pr i ntf () execute?

i f ( 3 ! = 4 ! = 1)

Relational Operators - 图29{ pr i ntf (“ Thi s will pr i nt ”) ; }

  1. Using the ASCII table (see Appendix C, “ASCII Table”), state whether

    these character relational tests are True or False:

a. ‘ C’ < ‘ c’

b. ‘ 0’ > ‘ 0’

c. ‘ ?’ > ‘ ) ’

Review Exercise s

  1. Relational Operators - 图30Write

    a weather-calculator program that asks for a list of the previous five days’ temperatures, then prints Brrrr ! every time a temperature falls below freezing.

  2. Relational Operators - 图31Write

    a program that asks for a number and then prints the square and cube (the number multiplied by itself three times) of the number you input, if that number is more than

    1. Otherwise, the program does not print anything.
  3. Relational Operators - 图32In

    a program, ask the user for two numbers. Print a message telling how the first one relates to the second. In other words, if the user enters 5 and 7, your program prints “5 i s

l ess t han 7 .”

  1. Write a program that prompts the user for an employee’s pre-tax

    salary and prints the appropriate taxes. The taxes are 10 percent if the employee makes less than $10,000; 15 percent if the employee earns $10,000 up to, but not includ- ing, $20,000; and 20 percent if the employee earns $20,000 or more.

Summary

You now have the tools to write powerful data-checking pro- grams. This chapter showed you how to compare literals, variables, and combinations of both by using the relational operators. The i f and the i f - el se statements rely on such data comparisons to deter- mine which code to execute next. You can now conditionally execute statements in your programs.

The next chapter takes this one step further by combining relational operators to create logical operators (sometimes called compound conditions). These logical operators further improve your program’s capability to make selections based on data comparisons.

Relational Operators - 图33

Relational Operators - 图34Relational Operators - 图35