Presentation is loading. Please wait.

Presentation is loading. Please wait.

Program Looping EE2372 Software Design I Dr. Gerardo Rosiles.

Similar presentations

Presentation on theme: "Program Looping EE2372 Software Design I Dr. Gerardo Rosiles."— Presentation transcript:

1 Program Looping EE2372 Software Design I Dr. Gerardo Rosiles

2 Program Looping Looping programming elements are one of the most powerful feature in programming languages. Consider writing program to sum 10 numbers… easy Consider writing a program to sum 1,000,000 numbers => one million sums … Looping constructs make this easy In C-language we have three types of loops: – for loops – while loops – do loops

3 for loops Similar to the MATLAB for loops Syntax for (init_expression, loop_condition, loop_expression) program statement Syntax with block of statements for (init_expression, loop_condition, loop_expression) { program statements }

4 for loops init expression – set any initial values before the loop begins. Typically initializes a variable to control the loop condition. loop condition(s) – a condition that continues the loop as long as it evaluated to TRUE. loop expression(s) – evaluated each time after the body of the loop is executed. Typically involved a variable controlling the loop.

5 for loops - example #include int main(void) { int n, number, triangularNumber; printf( What triangular number do you want? ); scanf ( %i, &number); printf( TABLE OF TRIANGULAR NUMBERS \n\n); printf( n Sum from 1 to n \n); printf(--- ----------------\n); triangularNumber = 0; for( n = 1; n<=number; ++n) { triangularNumber += n; printf( %i %i \n, n, triangularNumber); } return(0); }

6 Some variants Nested for loops for( n = 1; n<=number; ++n) { sum = 0; for ( m = number; m >= n; m--) { sum = sum + m; printf( sum %d to %d = %d \n, n, number, sum); } Multiple expressions, for( i=0, j=100; i < 10;++i, j=j-10)

7 Some variants Omitting fields for( ; ; ){.... } // infinite loop for( ; j!= 0 ; j++ ){... } Declaring variables for( int i; i <= 10 ; i++ ){... } i is a local variable and cant be accessed outside of the loop. Disappears after the loop finished.

8 while loops Syntax while( expression ) { program statements } Equivalency with for loops init_expression; while (loop_condition) { program statements loop_expression; } while statements are useful when there is not a predetermined number of times the loop needs to be executed.

9 do-while loops Syntax: do { program statements } while( expression ); do-while loops are a transpositions of while loops. However, do-while loops ensure the program statement is executed at least once.

10 Loop interruption statements In some situations it may be desirable to stop the loop or to finish early the current iteration of the loop. break – can finish the execution of a loop before the loop condition is not satisfied. continue – finishes early the current iteration of the loop if some specific condition arises. Otherwise the loop continues executing.

Download ppt "Program Looping EE2372 Software Design I Dr. Gerardo Rosiles."

Similar presentations

Ads by Google