For loop in c with example

For loop is another loop in c.

Syntax:-

for(initialization; condition; increment)
{
Body of the loop
}
Statement;

Note:-

# If body of loop is only one statement than braces ({}) are not needed.
# Body of the loop may have one or more statement.
# Initialization and increment can be skip but semicolon should be there to separate 3 section.
# Condition section can`t be skip in for loop.

Execution:-

In for loop 1st variable is initialized than condition will check if condition is true than body of the loop will executed. After body of the loop is execute, variable is incremented and the condition is again tested if condition is true than again body of loop is executed, this process is repeated until condition is true. When condition is false than statement next to while loop is executed.

For loop can be written in another way by skipping initialization and increment condition.

Syntax:-

Initialization;
for( ; condition; )
{
Body of the loop
Increment;
}
statement

Variable can be declared in for loop

Example:-

for(int i=0; i< num; i++) Note:- for loop initial declaration are only allowed in c99 mode. Example:-

Program to count N numbers


Program to display even number up to 20



Previous Code:-
Decrement operator

List Code:-
C Codes

Next Code:-
while loop

C Programs
List of c Programs

Leave reply

Add your comments here

Back to Top