September 2001
Beginner
970 pages
23h 20m
English
You've seen how if and while statements often use relational expressions as tests. Sometimes you will find it useful to combine two or more relational expressions. For example, suppose you want a program that counts how many times the characters other than single or double quotes appear in an input sentence. You can use logical operators to meet this need, and you can use the period character (.) to identify the end of a sentence. Listing 7.6 presents a short program illustrating the method.
// chcount.c -- use the logical AND operator #include <stdio.h> #define PERIOD '.' int main(void) { int ch; int charcount = 0; while ((ch = getchar()) != PERIOD) { if (ch != '"' && ch != '\'') charcount++; ... |
Read now
Unlock full access