Search for notes by fellow students, in your own course and all over the country.

Browse our notes for titles which look like what you need, you can preview any of the notes via a sample of the contents. After you're happy these are the notes you're after simply pop them into your shopping cart.

My Basket

You have nothing in your shopping cart yet.

Title: Looping in the C Programming Language
Description: These notes will help you understand the 3 different looping statements in C: for, while and do while. These notes are suitable for 1st year beginners who are struggling in understanding the concept of looping. Many illustrative examples are given and even complete C programs are included.

Document Preview

Extracts from the notes are below, to see the PDF you'll receive please use the links above


Chapter 5
Repetition and Loop Statements
-

Instructions in C are classified into three kinds of control structures to control
execution flow:
1
...


...

statementn;
}
2
...



...

else
statementn;
3
...
To execute a statement many times, use one of C loop
statements: while, for and do-while
...


THE while LOOP
- The syntax for the while loop is as follows:
while (loop repetition condition)
one or more actions;
- The while loop is used to repeat actions as long as the while repetition condition is
true
...

- The while repetition condition must be enclosed between ( and )
...

- Like the if statement, multiple actions must be enclosed between { and }
...

- Each loop consists of three components:
- Initialization : to initialize the loop control variable
...

- Update: to update the loop control variable during each iteration
...

- Initially, star_count is set to 0 and incremented each iteration
...

- The above while loop can become infinite if the loop control variable is not updated
as follows:
int star_count = 0; /* Initialization */
while (star_count < 5) { /* Test */
printf(“*”);
}
- What is the output of the following while loop:

int counter = 1;
while (counter <= 5) {
printf(“%d\n”,counter);
counter = counter + 1;
}
- What is the output of the following while loop:
int x = 20;
while (x%5 == 0) {
printf(“Hello\n”);
x = x – 5;
}

2

This is an infinite loop because x%5 will be 0 when x is negative
...

- Like the while loop, the for loop has three components: initialisation, test & update
...

- Here is how it works:
1
...

2
...
After the last action, the update statement is performed followed by step 2
...
h>
int
main(void)
{
int time, start;
printf(“Enter starting time (an integer) in seconds> “);
scanf(“%d”, &start);
printf(“\nBegin countdown\n”);
for (time = start;
time > 0;
time -= 1) {
printf(“T - %d\n”, time);
}
printf(“Blast-off!\n”);
return 0;
}
- Another C program:
#include ...
8 * celsius + 32
...
2f\n”, celsius, fahrenheit);
}
return 0;
}

5

- The following is a sample execution of the program:
Celsius
10
5
0
-5
-

Fahrenheit
50
...
00
32
...
00

CLASSIFICATIONS OF while AND for LOOPS
- while and for loops can be classified as follows:

1
...

- Counter controlled loops are used when the number of iterations in the
loop is known before the loop starts
...
CONDITIONAL LOOPS
- Conditional loops are controlled by a condition that will determine when
the loop terminates
...


6

int item,product = 1;
while (product < 1000) {
printf(“%d\n”,product);
printf(“Enter next item> “);
scanf(“%d”,&item);
product = product * item;
}
- In the above loop, note that the condition product < 1000 determines when
the loop terminates (which is when product >= 1000)
...

3
...

- The sentinel value should be carefully selected so that it cannot be part of
the input
...
In this
case, a negative integer is a better selection as a sentinel value
...
h>
#define SENTINEL -99
int main() {
int sum = 0, score;
printf("Enter first score (or %d to quit)> ",SENTINEL);
for (scanf("%d", &score);score != SENTINEL; scanf("%d",&score)) {
sum += score;
printf("Enter next score (or %d to quit)> " ,SENTINEL);
}
printf("\nSum of exam scores is %d\n", sum);
return (0);
}
- The following is a sample run of the above program:
Enter first score (or -99 to quit)> 70
Enter next score (or -99 to quit)> 80
Enter next score (or -99 to quit)> 83
Enter next score (or -99 to quit)> 85
Enter next score (or -99 to quit)> 77
Enter next score (or -99 to quit)> 90
Enter next score (or -99 to quit)> -99
Sum of exam scores is 485

7

4
...
A flag-controlled loop is then a loop that uses a flag to
control the loop
...

- Example:
int x, y, done = 0,sum = 0;
while (!done)
{
printf(“Enter two numbers X and Y where Y is greater than X ”);
scanf(“%d%d”,&x,&y);
if (y <= x)
done = 1;
else
sum += x + y ;
}
printf("\nSum is %d\n", sum);
-

THE do-while LOOP
- Similar to the while loop except that the test at end of the loop rather than the
beginning of the loop
...
Then, the loop repetition condition is tested, and if it
is true, the statement is re-executed and the condition retested
...
When the
condition is tested and found to be false, the loop is exited and the next statement
after the do-while loop is executed (if any)
...

- Let us re-write the countdown program on page 5 using a do-while loop:

8

#include ...
For example, it is possible to have an if statement
nested inside a while or for loop statement, a for loop statement nested inside a
while loop statement, a while loop statement nested inside a for loop statement, etc
...

- Example: if statement nested inside a while loop statement:
int item,product = 1;
while (product < 1000) {
printf(“%d\n”,&product);
printf(“Enter next item> ”);
scanf(“%d”,&item);
if (item != 0)
product = product * item;
}

9

-

THE break and continue STATEMENTS
- Inside while, do-while and for loops, the break statement can be used to terminate
the loop prematurely even if the loop repetition condition is still true
...

- Example using the break statement:

int item,product = 1;
while (product < 1000) {
printf(“%d\n”,&product);
printf(“Enter next item> “);
scanf(“%d”,&item);
if (item == 0)
break;
else
product = product * item;
}
- In the above example, as soon as the user enters the value 0 for item, the loop
terminates
Title: Looping in the C Programming Language
Description: These notes will help you understand the 3 different looping statements in C: for, while and do while. These notes are suitable for 1st year beginners who are struggling in understanding the concept of looping. Many illustrative examples are given and even complete C programs are included.