For loop in c programming

21 Aug 2022 Balmiki Mandal 0 C Programming

For Loop loop in c

The "for" loop in C programming is a control flow statement that allows you to repeatedly execute a block of code for a specific number of times. Its syntax is as follows:

for loops is an iterative control statement 

for (initialization; condition; increment/decrement) {
    // Code to be executed repeatedly
}

The "for" loop consists of three parts:

  1. Initialization: Here, you initialize a loop control variable and set an initial value.
  2. Condition: It specifies the condition that needs to be evaluated for each iteration. If the condition evaluates to true, the loop continues; otherwise, it terminates.
  3. Increment/Decrement: It specifies how the loop control variable should be modified after each iteration. It can be incremented or decremented.

The "for" loop works in the following way:

  1. The initialization part is executed first.
  2. The condition is checked. If it is true, the code inside the loop is executed. If it is false, the loop is terminated.
  3. After executing the code inside the loop, the increment/decrement part is executed.
  4. The condition is checked again, and the process repeats until the condition becomes false.

Here's an example that prints the numbers from 1 to 5 using a "for" loop:

Program 01:

#include <stdio.h>
int main() {
    int i;
for (i = 1; i <= 5; i++) {
        printf("%d\n", i);
    }
    return 0;
}

Output: 1 2 3 4 5

In this example, the loop control variable i is initialized to 1. The loop continues as long as i is less than or equal to 5. After each iteration, i is incremented by 1 (i++).

You can modify the initialization, condition, and increment/decrement parts of the "for" loop to suit your specific requirements.



Related Programs

Multiplication table using for loop

print binary number using for loop

Write a program to how many bits are set in the given number using for loop

Write a program to add digits from the given number using for loop

Write the program reverse the number using for loop

Write a program to reverse the bits of a given number using for loop

Write a program for reversing nibbles of a given number using for loop

Converting the little eledian data to big eledians data using for loop

Write a program to check given number is prime or not using for loop 

 Write a C program to print a reliable prime number between 50 to 100

BY: Balmiki Mandal

Related Blogs

Post Comments.

Login to Post a Comment

No comments yet, Be the first to comment.