- 2.1. Introduction
- 2.2. A Simple C Program: Printing a Line of Text
- 2.3. Another Simple C Program: Adding Two Integers
- 2.4. Arithmetic in C
- 2.5. Decision Making: Equality and Relational Operators
- 2.6. Secure C Programming
2.5. Decision Making: Equality and Relational Operators
Executable statements either perform actions (such as calculations or input or output of data) or make decisions (we’ll soon see several examples of these). We might make a decision in a program, for example, to determine whether a person’s grade on an exam is greater than or equal to 60 and whether the program should print the message “Congratulations! You passed.” This section introduces a simple version of C’s if statement that allows a program to make a decision based on the truth or falsity of a statement of fact called a condition. If the condition is true (i.e., the condition is met), the statement in the body of the if statement is executed. If the condition is false (i.e., the condition isn’t met), the body statement isn’t executed. Whether the body statement is executed or not, after the if statement completes, execution proceeds with the next statement after the if statement.
Conditions in if statements are formed by using the equality operators and relational operators summarized in Fig. 2.9. The relational operators all have the same level of precedence and they associate left to right. The equality operators have a lower level of precedence than the relational operators and they also associate left to right. [Note: In C, a condition may actually be any expression that generates a zero (false) or nonzero (true) value.]
Fig. 2.9. Equality and relational operators.
Algebraic equality or relational operator |
C equality or relational operator |
Example of C condition |
Meaning of C condition |
Equality operators |
|
|
|
= |
== |
x == y |
x is equal to y |
≠ |
!= |
x != y |
x is not equal to y |
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 |
Figure 2.10 uses six if statements to compare two numbers entered by the user. If the condition in any of these if statements is true, the printf statement associated with that if executes. The program and three sample execution outputs are shown in the figure.
Fig. 2.10. Using if statements, relational operators, and equality operators.
1
// Fig. 2.10: fig02_10.c
2
//
Using if statements, relational
3
//
operators, and equality operators.
4
#include <stdio.h>5
6
// function main begins program execution
7
int main( void )8
{9
int num1;// first number to be read from user
10
int num2;// second number to be read from user
11
12
printf("Enter two integers, and I will tell you\n"
);13
printf("the relationships they satisfy: "
);14
15
scanf("%d%d"
, &num1, &num2 );// read two integers
16
17
if ( num1 == num2 ) {
18
printf(
"%d is equal to %d\n", num1, num2 );
19
}
// end if
20
21
if (num1 != num2
) {22
printf("%d is not equal to %d\n", num1, num2 );
23
}// end if
24
25
if (num1 < num2
) {26
printf("%d is less than %d\n", num1, num2 );
27
}// end if
28
29
if (num1 > num2
) {30
printf("%d is greater than %d\n", num1, num2
);31
}// end if
32
33
if (num1 <= num2
) {34
printf("%d is less than or equal to %d\n", num1, num2 );
35
}// end if
36
37
if (num1 >= num2
) {38
printf("%d is greater than or equal to %d\n", num1, num2 );
39
}// end if
40
}// end function mai
The program uses scanf (line 15) to input two numbers. Each conversion specifier has a corresponding argument in which a value will be stored. The first %d converts a value to be stored in the variable num1, and the second %d converts a value to be stored in the variable num2.
Comparing Numbers
The if statement in lines 17–19
if ( num1 == num2 ) {
printf(
"%
d is equal to%
d\n", num1, num2 ); }
compares the values of variables num1 and num2 to test for equality. If the values are equal, the statement in line 18 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 21, 25, 29, 33 and 37, the corresponding body statement displays an appropriate line of text. Indenting the body of each if statement and placing blank lines above and below each if statement enhances program readability.
A left brace, {, begins the body of each if statement (e.g., line 17). A corresponding right brace, }, ends each if statement’s body (e.g., line 19). Any number of statements can be placed in the body of an if statement.2
Figure 2.11 lists from highest to lowest the precedence of the operators introduced in this chapter. Operators are shown top to bottom in decreasing order of precedence. The equals sign is also an operator. All these operators, with the exception of the assignment operator =, associate from left to right. The assignment operator (=) associates from right to left.
Fig. 2.11. Precedence and associativity of the operators discussed so far.
Operators |
Associativity |
|||
() |
|
|
|
left to right |
* |
/ |
% |
|
left to right |
+ |
- |
|
|
left to right |
< |
<= |
> |
>= |
left to right |
== |
!= |
|
|
left to right |
= |
|
|
|
right to left |
Some of the words we’ve used in the C programs in this chapter—in particular int and if—are keywords or reserved words of the language. Figure 2.12 contains the C keywords. These words have special meaning to the C compiler, so you must be careful not to use these as identifiers such as variable names.
Fig. 2.12. C’s keywords.
Keywords |
|||
auto |
double |
int |
struct |
break |
else |
long |
switch |
case |
enum |
register |
typedef |
char |
extern |
return |
union |
const |
float |
short |
unsigned |
continue |
for |
signed |
void |
default |
goto |
sizeof |
volatile |
do |
if |
static |
while |
Keywords added in C99 standard |
|||
_Bool _Complex _Imaginary inline restrict |
|||
Keywords added in C11 standard |
|||
_Alignas _Alignof _Atomic _Generic _Noreturn _Static_assert _Thread_local |