C language provides three types of loops: for, while, and do-while. These loops are used to execute a block of code repeatedly until a certain condition is met.
1. for loop: The for loop is used when you know the exact number of times you want to execute a block of code. It has the following syntax:
for (initialization; condition; increment/decrement) {
/* code to be executed repeatedly */
}
The initialization statement is executed once before the loop starts. The condition is evaluated at the beginning of each iteration, and the loop will continue as long as the condition is true. The increment or decrement statement is executed at the end of each iteration.
Example:
for (int i = 0; i < 10; i++)
{
printf("%d ", i);
}
This will print the numbers 0 to 9.
2. while loop: The while loop is used when you don’t know the exact number of times you want to execute a block of code. It has the following syntax:
while (condition)
{
/* code to be executed repeatedly */
}
The loop will continue executing as long as the condition is true.
Example:
int i = 0;
while (i < 10) {
printf("%d ", i);
i++;
}
This will print the numbers 0 to 9.
3.do-while loop: The do-while loop is similar to the while loop, but the condition is evaluated at the end of each iteration instead of the beginning. This means that the loop will execute at least once, even if the condition is false. It has the following syntax:
do {
/* code to be executed repeatedly */
}
while (condition);
Example:
int i = 0;
do {
printf("%d ", i);
i++;
}
while (i < 10);
This will print the numbers 0 to 9.
In all three types of loops, you can use break and continue statements to control the flow of the loop. The break statement is used to exit the loop, while the continue statement is used to skip the rest of the code in the current iteration and move on to the next iteration.
3. Nested loop:Top of Form In C programming, nested loops are loops that are placed inside other while, for, or do..while loop. The outer loop is executed first, and then the inner loop is executed for each iteration of the outer loop. This process is repeated until all iterations of the outer loop have been completed. Here’s an example of nested loops in C that prints out a multiplication table:
#include
int main() {
int i, j;
for (i = 1; i <= 10; i++) {
for (j = 1; j <= 10; j++) {
printf("%d ", i*j); }
printf("\n");
}
return 0;
}
In this example, there are two for loops: the outer loop iterates from 1 to 10, and the inner loop also iterates from 1 to 10. Inside the inner loop, the program prints out the product of the current values of i and j, separated by a space. After the inner loop has finished iterating, the program prints out a newline character (\n) to move to the next line.
When you run this program, it will output a multiplication table from 1 to 10.
Nested loops are useful when you need to iterate through a set of data that has multiple dimensions, such as a two-dimensional array. By using nested loops, you can access each element of the array and perform operations on it.
Learners TV is a website that is designed to educate users and provide instructional material on particular subjects and topics.