Learn C Programming
for Loop
Loop
In programming, a loop enables the execution of a block of commands over and over again until a specific condition is met. You will get to know detail about loop with examples.
Advantages of Loop
- No need to write code again and again
- This reduces errors
- It provides code reusability
- Improve readability
- Traverse over element of data structures
C programming has three types of loops:
- for loop
- while loop
- doโฆwhile loop
In this tutorial, you will learn about for loop and Nested for Loop with examples.
for Loop
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
Syntax
The basic syntax for a for loop in C programming is as follows:
for (initialization; condition; update) {
// code to be executed repeatedly
}
Hereโs how it the for loop works with examples:
- initialization: This expression executes only once at the beginning of the loop. It is typically used to declare and/or initialize a counter variable, but it can also be used for other purposes. For example, int i = 0 would be a common initialization statement.
- condition: Before each iteration of the loop, the condition is checked. If the condition is true, the loop body is executed. If the condition is false, the loop stops. For example, i < 10 would be a common condition.
- update: This expression runs after each loop iteration, usually to change the counter variable, but it can also serve other purposes. For example, i++ would be a common update statement.
For instance:
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 5; i++) {
printf("%d\n", i);
}
return 0;
}
Output
0
1
2
3
4
Hereโs how it the for loop works:
- Declaration and initialization: We declare and set the variable โiโ to 0, and we will use it as a counter for the loop.
- Loop condition: The loop will continue executing as long as the condition โi < 5โ is true. This means the loop will iterate five times since โiโ starts at 0 and increments by 1 in each iteration.
- Loop body: The block of code inside the curly braces following the โforโ statement is the loop body. In this case, we use the printf() function to display the value of โiโ and add a new line character afterwards.
- Iteration: In each loop iteration, we use printf() to print the value of โiโ. At the beginning, โiโ is 0, so it gets printed in the first iteration. Then, we increment โiโ by 1 using the expression โi++โ.
- Loop termination: When the value of โiโ becomes 5, the condition โi < 5โ becomes false, and the loop terminates. The program continues with the next line of code after the loop.
Nested Loopsย
A nested loop is a loop inside another loop. This means that the inner loop runs several times for each iteration of the outer loop.
Syntax
The basic syntax of a nested for loop in C programming is as follows:
for (int i = 0; i < n; i++) { // outer loop
for (int j = 0; j < m; j++) { // inner loop
// code to be executed repeatedly
}
}
In this, the outer loop executes n times, and for each iteration of the outer loop, the inner loop executes m times. This means the inner loop will run n times multiplied by m times, resulting in a total of n*m executions of the code inside.
For instance:
#include <stdio.h>
int main() {
int i, j;
// Outer loop
for (i = 1; i <= 2; ++i) {
printf("Outer: %d\n", i); // Executes 2 times
// Inner loop
for (j = 1; j <= 3; ++j) {
printf(" Inner: %d\n", j); // Executes 6 times (2 * 3)
}
}
return 0;
}
Output
Outer: 1
Inner: 1
Inner: 2
Inner: 3
Outer: 2
Inner: 1
Inner: 2
Inner: 3
Hereโs how it works:
- Header inclusion: The code starts by including the <stdio.h> header file, which provides the necessary function prototypes for input and output operations, including printf().
- ย The main() function: The main() function is the entry point of the program and where the execution begins.
- Variable declaration: We declare two integer variables, i and j, to use as loop counters for the outer and inner loops, respectively.
- Outer loop: During every time the outer loop runs, a second loop runs inside it. It initializes i to 1 and continues as long as i is less than or equal to 2. In each iteration, the loop prints the value of i using printf() with the โOuter: %dโ format specifier.
- Inner loop: Within each iteration of the outer loop, an inner loop is executed. The inner loop also uses a for loop construct. It initializes j to 1 and continues as long as j is less than or equal to 3. In each iteration of the inner loop, we print the value of j using printf() and the format specifier โInner: %dโ.
- Execution flow: The outer loop executes two times (when i is 1 and 2). Within each iteration of the outer loop, the inner loop executes six times (when j is 1, 2, and 3). As a result, the program prints the message โOuter: xโ twice and the message โInner: yโ six times for each iteration of the outer loop.
- Program termination: When the loops finish running, the program encounters the โreturn 0โ statement, which shows that the program has successfully ended.
ย
ย
ย
ย