Introduction To C
Introduction To C
4
C Program
Control
OBJECTIVES
In this chapter you will learn:
▪ The essentials of counter-controlled repetition.
▪ To use the for and do...while repetition statements to
execute statements in a program repeatedly.
▪ To understand multiple selection using the switch
selection statement.
▪ To use the break and continue program control statements
to alter the flow of control.
▪ To use the logical operators to form complex conditional
expressions in control statements.
▪ To avoid the consequences of confusing the equality and
assignment operators.
4.1 Introduction
▪ This chapter introduces
– Additional repetition control structures
- for
- do…while
– switch multiple selection statement
– break statement
- Used for exiting immediately and rapidly from certain control
structures
– continue statement
- Used for skipping the remainder of the body of a repetition
structure and proceeding with the next iteration of the loop
– The statement
int counter = 1;
- Names variable as counter
- Defines it to be an integer
- Reserves space for it in memory
- Sets it to an initial value of 1
1
2
3
4
5
6
7
8
9
10
Counter-Controlled Repetition
▪ Condensed code
– C Programmers would make the program more concise
– Initialize counter to 0
- int counter = 0;
- while ( ++counter <= 10 )
printf("%d\n", counter );
Fig. 4.4 |
Flowcharting a
typical for
repetition statement.
▪ Arithmetic expressions
– Initialization, loop-continuation, and increment can contain
arithmetic expressions.
Error-Prevention Tip
Sum is 2550
25.4545 is printed
main() {
int i, j;
for (i=0; i<10; i++)
for (j=0; j<=3; j++)
printf ("*"); }
a) 13 b) 30 c) 33 d) 40 e) 44
main() {
int i=3, j=-1;
for (i=5; i < 10; i++) {
printf ("%d", i); i++;
for (j=0; j < 2; j++) printf ("*"); } }
a) 579
b) 5**7**9
c) 5**7**9**
d) **7**9**
e) ******
main() {
int number=13986, i;
int s=0, d=0;
for (i=1 ; i ; i=number) {
s+=number%10;
number/=10;
d++; }
printf ("%d%d", s, d); }
switch ( value ) {
case '1':
actions
if ‘value’ is equal to ‘2’, the code is swithed to
case '2':
the ‘case 2’. Then, statements under ‘case 2’
actions
are executed.
default:
actions
} if ‘value’ is not equal to ‘1’ and ‘2’, the code is
swithed to the ‘default’. Then, statements
under ‘default’ is executed.
case 1
action
case 2
action
case 3
action
default
action
▪ Syntax: break;
getchar() function
The C library function
getchar()
reads only one character (an unsigned char) from
standart input (keyboard).
#include <stdio.h>
int main () {
char k;
k = getchar();
return(0); }
Portability Tip
main() {
int x;
switch (x=3.2) {
case 0: printf ("zero\n"); break;
case 1: printf ("one\n"); break;
case 2: printf ("two\n"); break;
case 3: printf ("three\n"); break;
default: printf ("default\n"); break; } }
main() {
int x=0,i;
for (i=0;i<5;i++) {
switch (i) {
case 0: x++;
case 1: x*=x; break;
case 2: x+=x;
default: x--; }
}
printf ("%d", x); }
a) -1 b) 0 c) 1 d) 2 e) 3
do {
printf( "%d ", counter );
} while (++counter <= 10);
do {
printf( "%d ", counter );
} while (++counter <= 10);
1 2 3 4
Broke out of loop at x == 5
1 2 3 4 6 7 8 9 10
Used continue to skip printing the value 5
a) 6 6 4 b) 6 8 5
c) 7 8 5 d) 7 8 4 e) 8 8 5
Logical Operators
▪ && ( logical AND )
– Returns true if both conditions are true
▪ || ( logical OR )
– Returns true if either of its conditions are true
▪ ! ( logical NOT, logical negation )
– Reverses the truth/falsity of its condition
– Unary operator, has one operand
0 0 0
0 nonzero 0
nonzero 0 0
nonzero nonzero 1
Fig. 4.13 | Truth table for the && (logical AND) operator.
▪ || ( logical OR )
– Returns true if either of its conditions are true
0 0 0
0 nonzero 1
nonzero 0 1
nonzero nonzero 1
expression !expression
0 1
nonzero 0
Performance Tip
In expressions using operator &&, make the
condition that is most likely to be false the leftmost
condition. (0 && x)
In expressions using operator ||, make the
condition that is most likely to be true the leftmost
condition. (1 ||x)
a) 1 2 0 b) 21 2 0 c) 21 2 1
d) 1 3 1 e) 31 3 1
main() {
int i, j, a, b, c, d;
a=1; b=-4; c=2; d=-3;
if ((a>b+c&&d) == (a>(b+c)&&d)) printf ("THIS");
else if ((a>b+c&&d) == ((a>b)+c&&d)) printf ("THAT");
else if ((a>b+c&&d) == (a>b+(c&&d))) printf ("NOT");
else printf ("DOT"); }