Download presentation
Presentation is loading. Please wait.
Published byCody Rogers Modified over 9 years ago
1
Looping Construct or Statements
2
Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination of the loop are satisfied. A program loop consists of two segments, (i) Body of the loop (ii) Control statement A looping construct includes the following four terms: (i) Initialization (ii) Condition (iii) Updation (iv) Body
3
Looping construct C supports three types of looping constructs: –while loop –do….while loop –for loop
4
While loop The while loop is an entry-controlled loop construct. The general syntax of while loop is: initialization; while (test-condition) { body of the loop; updation; }
5
An example of while loop /*To print numbers from 1-10*/ #include void main() { int i=1; /*initialization*/ while( i<=10) /*condition- check */ { printf( “%d \n”, i ); /*body of while loop*/ i++; /*updation*/ }
6
Do….While loop The do…while loop is an exit controlled loop construct. The general syntax of while loop is: initialization; do { body of the loop; } while (test-condition) ;
7
For loop The for loop is an entry-controlled loop. The general syntax of the for statement is: The initialization is an assignment statement that sets the loop control variable, before entering the loop. The conditional test is a relational expression, which determines, when the loop will exit. for( initialization; condition; updation) { body of the loop; }
8
Nested loop Nesting of loop means one loop defined within another loop. The nesting may continue upto 15 levels or more i.e. compiler dependent. For exam.: for( i=1; i<10; ++i) { -------------- for( j=1;j!=5;++j) { ------------ }
9
An example of nested loop /*To print “*” in triangle form */ #include void main() { int i, j; for( i=0; i<5; i++) { for( j=0; j<i; j++) { printf( “*” ); } printf( “*” ); printf( “\n”); }
10
Thanks…..
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.