The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

C for loop

what is for loop?

Conceptually, for loop is a bit more complex than while loop and do while loop, though syntax is neat and compact way to write certain types of loops. Typically, a programmer need to use a for loop when you exactly know how many times you want a block of statement to be executed repeatedly.

Syntax for loop

Let us see how neat a syntax of for loop is

Syntax
for (initializeCounter; testCondition; ++ or -- )
{
.
.
}

for loop FlowChart

C for Loop

for loop Realtime

RESETICSP2LTXRXONArduinoTMDigital PWM = ~POWERANALOG IN-+UNOIOREFRESETEVE5VGNDGNDVINA0A1A2A3A4A5ICSPAREFGND1211109487563210

Program for loop

The following example program will clearly explain the concept of for loop

for.c
#include <stdio.h>
int main()
{
int a;
for(a = 1;a <= 5;a++)
{
printf("%d ",a);
}
return 0;
}
  • 1 2 3 4 5

Note:

The variable a is initialized to 1 for the first time when the program execution starts in. The condition a <= 5 is a test condition, which is tested for every iteration. The statements under a loop will be executed repeatedly until the test condition is true.

Infinite for loop

When the for statement contains no test condition between the ;(semicolon) then the loop is said to be an infinite for loop.

Program Infinite for loop

The following C program will clearly demonstrate infinite for loop

goto.c
#include <stdio.h>
int main()
{
int a = 1;
for(;;)
{
printf("%d ", a++);
}
return 0;
}
  • 1 2 3 4 5 ...........

Note:

for(;;) statement allows execution of a loop continued for infinite times.

Report Us

We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

Report