Download Repetition Structures in C Programming: While, Do-While, and For Loops and more Slides Computer Engineering and Programming in PDF only on Docsity!
Repetition Structures
Lecture 10
Repetition Structures
- Repetition structures are used to repeat
statements or blocks of code.
- The decision whether to repeat the code is based
on the evaluation of a logical expression. If the
expression is true, the code is executed. If false,
the code is not executed.
- Repetition structures may repeat the code a
definite number of times (usually for loops) or an
indefinite number of times (usually while or do
while loops).
While Loops (Syntax)
The syntax of a while loop is:
while ( logical expression is true ) statement ;
or
while ( logical expression is true )
statement ;
statement ;
While Loops
/* This program associates letter grades with
numeric test scores. It continues to request
input until the user types in a 0 and then it
terminates with a message. */
#include <stdio.h>
int main ( )
int score ;
While Loops
else if (score >= 60)
printf ("Your score of %d is a D\n", score) ;
else if (score != 0)
printf ("Your score of %d is an E\n", score) ;
else
printf ("Terminating at your request.\n\n") ;
} /* End of While loop */
} /* End of "main" function */
Do-While Loops (Syntax)
do statement ; while ( logical expr is true ) ;
or, more commonly do { statement ; statement ; } while ( logical expression is true ) ;
Do-While Loops
do { printf ("enter a test score (0 to quit) >") ; scanf ("%d", &score) ; if (score >= 90) printf ("Your score of %d is a A\n", score) ; else if (score >= 80 && score <90) printf ("Your score of %d is a B\n", score) ; else if (score >= 70) printf ("Your score of %d is a C\n", score) ;
Do-While Loops
else if (score >= 60)
printf ("Your score of %d is a D\n", score) ;
else if (score != 0)
printf ("Your score of %d is an E\n", score) ;
else
printf ("Terminating at your request.\n\n") ;
} while (score != 0) ;
For Loops
for ( expr1 ; expr2 ; expr3 ) { statement(s) ; }
Expression2 is the logical expression to be
evaluated as true or false. It does NOT need to
contain any of the variables that are in
expression1. It gets evaluated after expression
and again after expression3.
For Loops
for ( expr1 ; expr2 ; expr3 ) { statement(s) ; }
Expression3 defines changes to conditions. It
can be multiple expressions separated by
commas. It gets executed after the statements
in the loop are executed. The expressions do
NOT have to be related to either those in
expression1 or in expression2 (but they often
are). Docsity.com
Simple For Loop Example
int counter;
for (counter = 1; counter <= 10; counter++)
printf ("The counter value is: %2d\n",
counter);
Equivalency of While Loop vs. For
Loop
The Loop: expression1 ; while ( expression2 ) { statement ; expression3 ; }
Is Equivalent to:
for ( expr1 ; expr2 ; expr3 ) statement ;
Simple While Loop Example
int counter ;
counter = 1 ; while ( counter <= 10 ) { printf ("The counter value is: %2d\n", counter) ; counter++ ; }
Example of For Loop
#include <stdio.h>
int main ( )
int k, n ;
for (k = 1, n = 12 ; k < 9 && n > 6 ; k++, n--)
printf ("k=%d, n=%d\n" , k , n ) ;