11. Can I Compare Two Values?

With Relational Operators

image

C provides an extremely useful statement called if. if lets your programs make decisions and execute certain statements based on the results of those decisions. By testing contents of variables, your programs can produce different output given different input.

Relational operators are also described in this chapter. Relational operators, combined with if, make C a powerful data-processing language. Computers would really be boring if they couldn’t test data. Computers would be little more than calculators if they had no capability to decide courses of action based on data.

Testing Data

C’s if statement works just like it does in spoken language: If something is true, do one thing; otherwise, do something else. Consider these statements:

If I make enough money, we’ll go to Italy.

If the shoes don’t fit, take them back.

If it’s hot outside, water the lawn.

Table 11.1 lists C’s relational operators, which permit testing of data. Notice that some of the relational operators are made up of two symbols.

Table 11.1. C’s relational operators.

image

Note

image

Relational operators compare two values. You always put a variable, literal, or expression—or a combination of any two of them—on either side of a relational operator.

Before delving into if, let’s look at a few relational operators and see what they really mean. A regular operator produces a mathematical result. A relational operator produces a true or false result. When you compare two data values, the data values either produce a true comparison or they don’t. For example, given the following values:

int i = 5;
int j = 10;
int k = 15;
int l = 5;

the following statements are true:

i == l
j < k
k > i
j != k

The following statements are not true, so they are false:

i > j
k < j
j == l

Warning

image

Only like values should go on either side of the relational operator. In other words, don’t compare a character to a float. If you have to compare two unlike data values, use a typecast to keep the values the same data type.

Warning

image

Every time C evaluates a relational operator, a value of 1 or 0 is produced. True always results in 1, and false always results in 0. The following statements would assign a 1 to the variable a and a 0 to the variable b:

a = (4 < 10);  /* (4 < 10) is true, so a 1 is put in a */
b = (8 == 9);  /* (8 == 9) is false, so a 0 is put in b */

You will often use relational operators in your programs because you’ll often want to know if sales (stored in a variable) is more than a set goal, if payroll calculations are in line, if a product is in inventory or needs to be ordered. You have now seen only the beginning of relational operators. The next section explains how to use them.

Using if

The if statement uses relational operators to perform data testing. Here’s the format of the if statement:

if (condition)
{ block of one or more C statements; }

The parentheses around the condition are required. The condition is a relational test like those described in the preceding section. The block of one or more C statements is called the body of the if statement. The braces around the block of one or more C statements are required if the body of the if contains more than a single statement.

Clue

image

Even though braces aren’t required if an if contains just one statement, always use the braces. If you later add statements to the body of the if, the braces will be there. If the braces enclose more than one statement, the braces enclose what is known as a compound statement.

Here is an if statement:

image

The if reads like this to the C programmer: “If the variable named age contains a value less than 18, print the messages and calculate the value of yrs. Otherwise, don’t print and calculate. Whatever happens, the program continues at the statement that follows the body of the if” once the if completes its job.

Note

image

The main() function in the Blackjack program in Appendix B asks the player if he or she wants to hit or stand (in casino lingo, that means to draw another card or not). An if is used to determine exactly what the user wants to do.

Otherwise...: Using else

In the preceding section, you saw how to write a course of action that executes if the relational test is true. If the relational test were false, nothing happened. This section explains the else statement that you can add to if. Using else, you can specify exactly what happens when the relational test is false. Here is the format of the combined if-else:

image

Here is an example of if-else:

image

If the age is less than 18, the body of the if executes. If the age is not less than 18, the body of the else executes.

Clue

image

Put semicolons only at the end of executable statements in the body of the if or the else. Never put a semicolon after the if or the else. Semicolons go only at the end of complete statements.

The following program computes simple income tax if the user earns more than a preset salary.

image

Here are two runs of the program showing that the output differs depending on the amount of the user’s salary:

image

Rewards

image

• Use relational operators to compare data.

• Remember that a true relational result produces a 1, and a false relational result produces a 0.

• Use if to compare data and else to specify what to do if the if test fails.

• Put braces around the if body of code and around the else body of code. All the code in the braces either executes or does not execute depending on the relational comparison.

Pitfalls

image

• Don’t put values of different data types on each side of a relational operator.

• Don’t put a semicolon after if or else. Semicolons go only at the end of each statement, inside the body of the if or the else.

In Review

The goal of this chapter was to show you ways to test data and execute one set of code or another, depending on the result of that test. You don’t always want the same code to execute every time someone runs your program because the data is not always the same. Computers must be able to look at data and process that data (which is why it’s called data processing).

The relational operators and the if statement work together to produce a true or false analysis of data.

Code Example

image

Code Analysis

When the user enters a salary, the program makes a decision depending on the salary’s relation to $100,000. If the user earned more than $100,000 (we should all be so productive!), a congratulatory message is printed. If the user earned less than $100,000, a message prints telling the user how far away from $100,000 he or she is. (Most of us have quite a way to go!)

The else statement ensures that only one set of code, after if, executes.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.35.77