Introduction to C Programming Looping ConstructsComputers are very good at performing repetitive tasks very quickly. In this section we will learn how to make computer repeat actions either a specified number of times or until some stopping condition is met. Show
while Loops ( Condition-Controlled Loops )
while( condition ) body;
int i = 0; while( i < 5 ) printf( "i = %d\n", i++ ); printf( "After loop, i = %d\n", i ); do-while Loops
do { body; } while( condition );
int month; do { printf( "Please enter the month of your birth > " ); scanf( "%d", &month ); } while ( month < 1 || month > 12 );
for Loops
for( int i = 0; i < 5; i++ ) printf( "i = %d\n", i ); printf( "After loop, i is no longer valid\n" );
The comma operator
int i, j = 10, sum; for( i = 0, sum = 0; i < 5; i++, j-- ) sum += i * j; break and continue
Infinite Loops
int i, j; for( i = 0; i < 5; j++ ) printf( "i = %d\n", i ); printf( "This line will never execute\n" );
while( true ) { printf( "Please enter a month from 1 to 12 > " ); scanf( "%d", &month ); if( month > 0 && month < 13 ) break; printf( "I'm sorry, but %d is not a valid month.\nPlease try again.\n", month ); } Empty Loops
int i; for( i = 0; i < 5; i++ ); // Error on this line causes empty loop printf( "i = %d\n", i ); // This line is AFTER the loop, not inside it.
int i = 0; while( i < 5 ); // Error - empty loop on this line printf( "i = %d\n", i++ ); // Again, this line is AFTER the loop.
while( ( error = someFunction( ) ) != 0 ) ; // Empty loop - Does nothing forever, until someFunction returns a zero printf( "error = %d\n", error ); // After the loop. error must be zero to get here. Nested Loops
const int NROWS = 10; const int NCOLS = 10; for( int r = 0; r < NROWS; r++ ) { // Loop through rows for( int c = 0; c < NCOLS; c++ ) { // Loop through columns printf( "%5d", r * c ); // No newline in the middle of a row } // End of loop through columns printf( "\n" ); // One newline at the end of each row } // End of loop through rows
Floating Point Increments Within Loops
for( time = tMin; time <= tMax; time += deltaT ) { // Use the time variable in the loop
int nTimes = ( tMax - tMin ) / deltaT + 1; for( int i = 0; i < nTimes; i++ ) { time = tMin + i * deltaT; // NOW use a more accurate time variable
double deltaT = ( tMax - tMin ) / ( nTimes - 1 ); for( int i = 0; i < nTimes; i++ ) { time = tMin + i * deltaT; // NOW use a more accurate time variable
When to Use Which Loop ?
Exercises
Which type of loop automatically increments the counter?The Count-Controlled while loop
A counter variable is created and initialized to a starting value before the loop is started. The condition that is tested before each iteration of the loop is whether the counter has reached a predetermined value. The counter itself is incremented inside the while loop.
Which loop is specifically designed to initialize test and increment a counter variable?It is known as the for loop. The for loop is specifically designed to initialize, test, and update a counter variable.
Which type of loop uses a Boolean expression to control the number of times a loop repeats?Condition controlled loop:
This is a loop which exhibits true or false condition to control the number of times that the loop repeats. For example: “while” loop is a condition controlled loop. The “while” loop first checks its expression and then, the statements inside the loop gets executed.
Which of the following loops is a posttest loop?The do-while loop is called a posttest the loop because the condition is checked after the loop body is executed.
|