v for loop:
v
It is use to
execute the block of statement repeatedly till the condition is True.
Syntax:
for(initialization; test condition; increment)
{
Statement 1;
Statement 2;
.
. Block of statements
.
.
Statement n;
}
Statement x;
Statement y;
Flowchart
§ It is an entry control loop.
§ It is only loop in which initialization,
condition and loop counter statement are given in single line separated by semicolon
(;).
§ If the condition is True the
body of loop is executing.
§ After execution of the loop body, the
control is transfer back to the for statement. Now the value of control
variable is modified using loop counter statement.
§ Then it go to the condition and check
the condition is true, it executes the block of statements agains.
§ When condition is False the control
will transfer out of loop and then execute statement x, statement y.
Program: Write a program to print following
series
1
2 3
4
5 6
/* Program to print following series
1
2 3
4 5 6 */
#include <stdio.h>
#include <stdio.h>
void main()
{
int i,j,rows,k=1;
printf("Input number of rows : ");
scanf("%d",&rows);
for(i=1;i<=rows;i++)
{
for(j=1;j<=i;j++)
{
printf("%d ",k++);
}
printf("\n");
}
}
Output:
0 Comments