Loops statement are used to repeat the execution of Statement or blocks. Types of loops While loop Do-While loop For loop Introduction
Basic execution of loops A loop statement allows us to execute a statement or group of statements multiple times Onces the condition becomes false the loop terminates.
Syntax: while(condition) { //Statements } While loop Here, statement(s) may be a single statement or a block of statements. First Condition is evaluated. If the condition is true then statement part is executed then again it checks the condition. If the condition is false then out of the while loop.
Print 1 to 5. # include < stdio.h > void main () { int a = 1; while ( a < 6 ) { printf ("%d\n ", a ); a ++; } } While loop program a<6 output a++ 1<6 1 2 2<6 2 3 3<6 3 4 4<6 4 5 5<6 5 6 6<6==false - -
Syntax : do { // Statements } while(condition ); Do-while A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at least one time . First statement part is executed and then the condition part is evaluated. If the condition is true then again statement part executed and then again to the condition part. If the condition is false then out of do-while loop.
Print 0 to 6 # include < stdio.h > v oid main () { int a = 0 ; do { printf ("% d\n", a); a + +; } while ( a < 6 ); } Do-while program output a++ A<6 1 1<6 1 2 2<6 2 3 3<6 3 4 4<6 4 5 5<6 5 6 6<6==false
Syntax: for ( initialization; condition; increment /decrement) { //statement(s ); } First go for initialization. Second Condition - If the condition is true then statement part is executed and then iteration portion. - If the condition is false then out of the for-loop. Third Iteration -After iteration portion control goes back to condition. For loop
Print 1 to 5 # include < stdio.h > void main () { For( int i=1; i<6; i++) { printf ("%d\n", i ); } } For loop program i<6 output i++ i<6 1 2 2<6 2 3 3<6 3 4 4<6 4 5 5<6 5 6 6<6==false - -