Learn C with example
Print your own name n times using for
loop
In this example we are going to teach you
how you can use for loop and what is use of for loop.
Say We have print Sudhir Yadav
10 times then we need to write printf("Sudhir Yadav"); ten times.
To avoid this have a for loop.
Syntax of for loop:
for(initialization ;conditions, increment
or decrements )
{
//body of for loop
}
Here in this we pass first initialize
value from where for loop is started .The second is conditions. If this
condition is true then loop body will execute else the next statement after loop
will execute. Thread is increment or decrements of initialized values value.
The flow of for loop is firs initialize
then execute loop body then increment or decrements the value then check
conditions then if condition is true then again body will execute and increment
or decrements the values again. Do this until condition is
/* print your own name n times */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n;
clrscr();
printf("enter a number");
scanf("%d",&n);
for(i=1;i<=10;i++)
printf("\nSudhir Yadav");
getch();
}
|
Output Of Example
Enter a number 10
Sudhir Yadav
Sudhir Yadav
Sudhir Yadav
Sudhir Yadav
Sudhir Yadav
Sudhir Yadav
Sudhir Yadav
Sudhir Yadav
Sudhir Yadav
Sudhir Yadav |