Repetition Statements in C programming



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

The While Repetition Statement

The statement(s) contained in the while repetition statement constitute the body of the while. The while statement body may be a single statement or a compound statement.

Eventually, the condition will become false (when the last item on the shopping list has been purchased and crossed off the list). At this point, the repetition terminates, and the first pseudo code statement after the repetition structure is executed.

    // Analysis of examination results 
    #include <stdio.h>
    
    // function main begins program execution 
    int main( void )
    {
    
    // initialize variables in definitions 
    unsigned int passes = 0;   // number of passes   
    unsigned int failures = 0;   // number of failures 
    unsigned int student = 1;   // student counter    
    int result;   // one exam result 
    
    // process 10 students using counter-controlled loop 
    while ( student <= 10 ) {
    
    // prompt user for input and obtain value from user 
      printf( "%s", "Enter result ( 1=pass,2=fail ): " );
      scanf( "%d", &result );
    
    // if result 1, increment passes 
      if ( result == 1 ) {     
         passes = passes + 1;
      } // end if 
      else { // otherwise, increment failures 
         failures = failures + 1;
      } // end else 
      student = student + 1; // increment student counter  
    } // end while 
        // termination phase; display number of passes and failures 
    printf( "Passed %u\n", passes );
    printf( "Failed %u\n", failures );
    
    // if more than eight students passed, print "Bonus to instructor!" 
    if ( passes > 8 ) {
      puts( "Bonus to profesor!" );
        } // end if 
    } // end function main

    Output:
    Enter result ( 1=pass,2=fail ): 1
    Enter result ( 1=pass,2=fail ): 1
    Enter result ( 1=pass,2=fail ): 1
    Enter result ( 1=pass,2=fail ): 1
    Enter result ( 1=pass,2=fail ): 1
    Enter result ( 1=pass,2=fail ): 2
    Enter result ( 1=pass,2=fail ): 2
    Enter result ( 1=pass,2=fail ): 1
    Enter result ( 1=pass,2=fail ): 2
    Enter result ( 1=pass,2=fail ): 1
    Passed 7
    Failed 3

The do while Repetition Statement in C programming

In C, do...while loop is very similar to while loop. Only difference between these two loops is that, in while loops, test expression is checked at first but, in do...while loop code is executed at first then the condition is checked..


    #include <stdio.h>
    int main(){
    
    //initialize variables in definitions 
    int sum=0;
    int number;   
    do     // code is executed at first then the condition is checked
       {                                    
        printf("Enter a number\n");
        scanf("%d",&number);
        sum+=number;      
       }
     while(number!=0);  // while user insert 0 
     printf("sum=%d",sum);
     return 0;
      }

    Output:
    Enter a number : 5
    Enter a number : 9
    Enter a number : 7
    Enter a number : 0
    sum=21

Ads Right