Loading, please wait...

While Repetition Statement

A repetition statement allows you to specify that an action is to be repeated while some condition remains true.

Example

/* Class average program with counter-controlled repetition*/
#include <stdio.h>
.
/*function main begins program execution * /
int main( void )
{
int counter;
int grade;
int total;
int average;
.
/*initialization phase*/
Total = 0;
Counter = 1;
.
/*processing phase*/
while ( counter<= 10){
printf(“enter grade: ”);
scanf(“%d”, &grade);
total = total + grade;
counter = counter + 1;
}/*end while*/

/*termination phase*/
average = total / 10; /*integer divison*/

printf(“class average is %d\n”, average );/*display result */
return 0; /*indicate program ended successfully*/
} /*end function main */

Output:

 Enter grade: 98

 Enter grade: 76

 Enter grade: 71

 Enter grade: 87

 Enter grade: 83

 Enter grade: 90

 Enter grade: 57

 Enter grade: 79

 Enter grade: 82

 Enter grade: 94

 Class average is 81