Skip to main content
What is looping in C language - loops in C language - for loop in C language - while loop in C language - do while loop in C language - loops kya hote hai
What is looping in C language
Looping in C language allows you to repeat a set of
instructions multiple times, based on a specified condition or a predetermined
number of iterations. It is a fundamental concept used to perform repetitive
tasks efficiently. C provides three main types of loops:
for loop:
The "for" loop is commonly used when you know the
exact number of iterations needed. It consists of three parts inside
parentheses: initialization, condition, and increment/decrement.
for (initialization; condition; increment/decrement) {
// Code block to
execute repeatedly
}
Example:
#include <stdio.h>
int main() {
int i;
for (i = 1; i
<= 5; i++) {
printf("Iteration %d\n", i);
}
return 0;
}
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
while loop:
The "while" loop is used when the number of
iterations is not known beforehand, and the loop will continue executing as
long as the specified condition is true.
while (condition) {
// Code block to
execute repeatedly
}
#include <stdio.h>
int main() {
int count = 1;
while (count <=
5) {
printf("Iteration %d\n", count);
count++;
}
return 0;
}
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
do-while loop:
The "do-while" loop is similar to the
"while" loop, but it ensures that the code block is executed at least
once before checking the condition.
do {
// Code block to
execute repeatedly
} while (condition);
Example:
#include <stdio.h>
int main() {
int count = 1;
do {
printf("Iteration %d\n", count);
count++;
} while (count
<= 5);
return 0;
}
Output:
Iteration 1
Iteration 2
Iteration 3
Iteration 4
Iteration 5
Looping is a powerful construct that allows programs to
perform repetitive tasks, process data collections, and iterate through arrays
and other data structures. Care should be taken to ensure that the loop
condition eventually becomes false to avoid infinite loops.
Comments
Post a Comment