For loop in C


For loop is most wildely used loop in C program.

For Loops:

  • For Loop is used when a C program or specific part of C program has to be repeatedly executed.
  • E.g when we want to calculate simple interest of five different persons.
  • This loop can be used as a replacement of while loop.
  • Inface For loop is widely used for it better efficieny.

Why For loop is more efficient than While loop?

Syntax of for loop makes it better, easy to use and efficient. In for loop we set a loop counter, test condition and increment/decrement in just one single line. You can compare it with the while loop syntax to understand it easily.


Syntax:

for (initialise counter;test counter;increment operator)

{
statements;
do this;
do this;
/* This portion usually includes logic of program or 
anything that has to be executed repeatedly*/ 
}

More about for loops:

  • Test condition should be such that it should eventually become false, otherwise the loop would be executed FOREVER...indefinitely.
  • Condition being tested may use relational or logical operator.
while(i<=10)
while(i>=10 && j<=15)
while(j>10 &&(b<20 || C>20))
  • Note that for statement does not have ";" at the end.

See the following video:



No comments:

Post a Comment