Download as pdf
Download as pdf
You are on page 1of 9
J Sy wT “Loops in C Programming Language ° Y > In, and all other modern programming languages, iteration statements (also called loops) allow a set of instructions to be repeatedly executed until a certain condition is reached. > The process of repeatedly executing a collection of statements is called looping. The statements get executed many numbers of times based on the condition. But if the condition is given in such logic that the repetition continues any number of times with no fixed condition to stop looping those statements, then this type of looping is called infinite looping. > This condition may be predetermined (as in the for loop) or open-ended (as in the while and do-while loops). 7 © Zao) Y( )) LJ Y% 4 How is it Works Q Types of Loop C A x Iteration statement or Loop do-while Sy while loop in C Programming Language > The while loop in c is to be used in the scenario where we don't know the number of iterations Min advance. The block of statement executed in the while loop until the condition specified in the while loop is satisfied. It is also called a pre-tested loop. > The condition of the loop is tested before the body of the loop is executed, hence it is also called an entry-controlled loop. Synta: variable initialization; while(condition) f statements; //code to be executed i.e Statements to be executed repeatedly variable increment or decrement; // Increment (++) or Decrement (--) Operation } VY Y © a Z +#includecstdio.to int mein() 4 int count = 1; |) initialization part vhile(comt & 5 ) { printf("Si count); // expresson part or conditional part count++; | increnent part } return 0; } ho while Loop in C Programming Language > The do-while loop continues until a given condition satisfies, It is also called post testedtoop. Ut is used when it is necessary to execute the loop at least once (mostly menu driven programs). > The do-while loop is mostly used in menu-driven programs where the termination condition depends upon the end user. > do-while Statement is Exit Controlled Loop because condition is check at the last moment. Syntax: do 1 Loop body; Statement(s); a ee while(condition); “YO ma 4. Q " #include int main() { int n = 03 do { printf("Number %d \n",n)5 net; t while(n < 1@)5 return 03 wy yy C for loop in C Programming Language for loop is used to execute a set of statements repeatedly until a particular condition is satisfied. Syntax: ‘for(initialization; condition; increment/decrement) ti statement-block; } #include int main() at int i; for (is1; i<=3; i++) printf("%d\n", i); return 8;

You might also like