Loops in c programming… By : Rahul Branch : CSE Roll no. : 9526/22
CONTENTS Introduction What are loops Types of loops in C For loop While loop Do-while loop Conclusion
INTRODUCTION For Example, You want to print your name i.e. Rahul, 5 times then here is a simple program to do the same . #include<stdio.h> #include<conio.h> void main() { printf (“Rahul”); printf (“Rahul”); printf (“Rahul”); printf (“Rahul”); printf (“ Rahul”); }
What is a loop A loop statement allows us to execute a statement or a group of statements multiple times based on a condition. Loop statements are used to repeat the execution of statement or blocks.
Types of Loops in C For loop While loop Do while loop
For Loop For loop is used to iterate the statements or a part of the program several times . Syntax:- f or(variable initialize; condition; increment/decrement) { statement } Example:- i nt main() { OUTPUT:- value is = i nt i; 1 for(i=1; i<=3; i++) 2 { 3 p rintf(“value is =%d\n”, i); } }
While Loop It is an entry control loop, so condition is tested before each iteration to decide whether to continue or terminate the loop. Syntax:- Variable initialize; While(condition) { Statement; Increment/decrement; } Example:- OUTPUT:- int main() { 1 int i; 2 i=1; 3 while(i<=3) { printf (“%d\n ”, i); i++; } }
Do while Loop It is an exit control loop, condition is checked after one time execution of the body of the loop . Syntax:- Variable initialize; do { Statement; Increment/Decrement; } while(condition); Example:- OUTPUT :- value is = i nt main() { 1 int i; 2 i=1; 3 do { printf (“ value is = %d\n ”, i); i++; } w hile(i<=3); }
CONCLUSIONS loops are very important in any programming language as they allow us to reduce the number of lines in a code, making our code more readable and efficient.