FOR LOOP IN C LANGUAGE

Dev Frank
2 min readJan 14, 2024

--

In C programming, loop statements are used to repeatedly execute a block of code until a certain condition is met. The three main types of loops in C are:

1. for loop

2. while loop

3. do-while loop

Loops help in efficient and concise coding by eliminating the need to duplicate code for repeated tasks.

Today we’ll be talking about the first loop

For Loop

Utilize the for loop when you have a predetermined number of iterations for executing a block of code. It is used when the number of iterations is known beforehand.

A "for loop" is a programming construct that allows you to repeat a block of code a specific number of times or iterate over elements in a collection.

SYNTAX OF for Loop

for (expression 1; expression 2; expression 3) {
  // code block to be executed
}

expression 1: is executed once before the code block,

expression 2: defines the condition for code block execution,

expression 3: is executed every time after the code block.

Imagine you want to perform a certain task multiple times in a program. That’s where a for loop comes in handy. Let’s break it down:

  • Initialization: You start by setting up a variable that will act as a counter. This is done in the first part of the for loop.

For example, int i = 0; initializes a variable i to 0.

  • Condition: The loop will continue executing as long as a specified condition is true. This condition is checked before each iteration.

For instance, i < 5; means the loop will run as long as i is less than 5.

Iteration: After each iteration (execution of the loop’s code), you typically want to change something. This is done in the third part of the for loop.

For example, i++ increments the value of i by 1 after each iteration.

Putting it all together, here’s a simple example in C:

#include <stdio.h>

int main() {
// Initialization; Condition; Iteration
for (int i = 0; i < 5; i++) {
// Code to be executed repeatedly
printf("%d\n", i);
}

return 0;
}

HOW THE for Loop WORKS USING FLOWCHART

TRY THIS !!!

Write a program that prints the multiplication table of a given number. The user should input the number, and the program should display the multiplication table up to 10.

Cheers 💪

--

--

Dev Frank

Passionate tech enthusiast, diving deep into the world of software engineering. Thrilled to share insights with the world. A Software engineering student.