The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
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.
Let us see how neat a syntax of for loop is
for (initializeCounter; testCondition; ++ or -- ) { . . }
The following example program will clearly explain the concept of for loop
#include <stdio.h> int main() { int a; for(a = 1;a <= 5;a++) { printf("%d ",a); } return 0; }
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.
When the for statement contains no test condition between the ;(semicolon) then the loop is said to be an infinite for loop.
The following C program will clearly demonstrate infinite for loop
#include <stdio.h> int main() { int a = 1; for(;;) { printf("%d ", a++); } return 0; }
for(;;) statement allows execution of a loop continued for infinite times.
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019