Professional Documents
Culture Documents
ch05 Loops
ch05 Loops
Chapter Contents
This chapter introduces:
while... Loop
for... Loop
Summary
Exercises
Loops
Loops break the serial execution of the program.
while
for
do … while
While – Loop
Syntax:
Example: while1.c
1 /* while1.c
2 This program illustrates the while loop.
3 The characters from 'a' to 'z' are printed */
4
5 #include <stdio.h>
6 #define LOW_BORDER 'a'
7 #define HIGH_BORDER 'z'
8
9 void main(void)
10 {
11 char ch = LOW_BORDER; /* declare a character and
12 initialize it to hold the ASCII of 'a'*/
13 printf ("Here are the English letters :\n");
14 /* loop as long as the value of ch is lower or
15 equal to the ASCII of 'z' */
16 while(ch <= HIGH_BORDER)
17 {
18 putchar(ch); /*print the character whose ASCII is in ch
*/
19 ch++; /* increase the value of ch by one */
20 }
21 }
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 7
Example: while2.c
1 /* while2.c
2 This program illustrates the while-loop.
3 The user is requested to enter a digit, the digit is read from
4 The keyboard using getchar() and printed to the screen */
5 #include <stdio.h>
6
7 void main(void)
8 {
9 char dig ='a'; /* declare a character and initialize
10 it to hold the ASCII of 'a', which is not a digit*/
11 /* loop until a legal input is entered (a digit) */
expr1
F
test
expr2
T
loop body
expr3
continue program
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 9
for - Loop
Syntax:
or
for (expr1 ; expr2 ; expr3)
{
statements;
}
Is equivalent to:
expr1;
while (expr2)
{
{statements;}
expr3;
}
Example: for.c
1 /* for.c
2 This program illustrates the for loop.
3 The characters from 'a' to 'z' are printed. */
4
5 #include <stdio.h>
6
7 #define LOW_BORDER 'a'
8 #define HIGH_BORDER 'z'
9
10 void main(void)
11 {
12 char ch;
13 printf ("Here are the English letters :\n");
14
15 for (ch = LOW_BORDER ; ch <= HIGH_BORDER ; ch++)
16 {
17 printf("%c ", ch); /* the loop body - print ch.
*/
18 }
19 }
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 12
2. Loop body: print the character whose ASCII is the value of ch.
Comma Operator
The comma operator ( the operator with the lowest precedence)
is most often used in the for statement. It expands the flexibility
of the for loop, by allowing more than one initialization (expr1)
or more than one update (expr3).
Example:
int i,j;
for(i=0, j=0; i<5; i++, j--)
printf("i=%d j=%d\n", i,j);
Example:
Try to write a program to print following
11
2112
321123
do while Loop
Syntax:
do
{
Statements;
}while (expression);
loop
body statements
False
The value of the expression tested should change within the loop
body, otherwise the loop is infinite.
The loop body is executed at least once, unlike the other looping
methods, where the condition is evaluated before entering the loop
body.
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 18
Example: do-while.c
1 /* do_while.c
2 This program illustrates the do while loop.
3 The characters from 'a' to 'z' are printed */
4 #include <stdio.h>
5 #define LOW_BORDER 'a'
6 #define HIGH_BORDER 'z'
7
8 void main(void)
9 {
10 char ch = LOW_BORDER; /* declare a character and
11 initialize it to hold the ASCII of 'a' */
12 printf ("Here are the English letters :\n");
13
14 /* in a do while loop, the condition is checked AFTER each
15 iteration. This means that there is at least one iteration
*/
16 do
17 {
18 printf("%c ", ch); /* print the character
19 whose ASCII is in ch */
20 ch++; /* increase ch by 1 */
21 }while(ch <= HIGH_BORDER);
22 }
© Copyright: Spymek Software Pvt. Ltd.
C1 Ch03 - Operators and Expressions – 19
break Statement
We have seen how to use the break statement within the switch
statement.
Example: break.c
1 /* break.c
2 This program chooses a random number(0-10),and the user
3 will try to guess it */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <time.h>
8
9 void main(void)
10 {
11 int num; /* the random number */
12 int guess = -1; /* the user’s guess,
13 initialized to -1 (Why ?) */
14
15 srand(time(0)); /* initialize the mechanism of
16 random numbers*/
17 num = rand() % 11; /* choose a random number
18 and limit it’s range */
19 printf ("A random number(in the range 0-10) "
20 "was chosen. Try to guess it.\n");
continue Statement
In some situations, you might want to skip to the next iteration of
a loop without finishing the current iteration.
Example:
Suppose we want to print the values of the expression
y = 1/(x-3) for all the values of x (integer) from 1 to 10.
for (x = 1 ; x <= 10 ; ++x)
{
if (x==3)
continue; /* avoid division by zero */
printf("%d %f\n", x, 1/(x-3) );
}
Summary
Loops break the serial execution of commands.
Summary – cont’d
while for do
do
while(exp) for(exp1; exp2; {
{ exp3) statements;
statements; { } while(exp);
} statements;
}
loop body is
loop body might not loop body might not
executed at least
be executed at all. be executed at all.
once.