Sep 26
Nested loop is one loop inside another loop.
Example
#include<stdio.h>
main()
{
int i,j;
for(i=1;i<=4;++i)
{
printf(”%d\n”,i);
for(j=1;j<=i;++j)
printf(”%d “,j);
}
}
Output
1
1 2
1 2 3
1 2 3 4
here,an inner for loop is written inside the outer for loop. For every value of i, j takes the value from 1 to i and then value of i is incremented and next iteration of outer loop starts ranging j value from 1 to i.
written by Shweta
Sep 18
The for allows us to specify three things about a loop in single line :
- Setting a loop counter to an initial value.
- Test conditions.
- Increasing the value of loop counter each time the program segment within the loop has been executed.
Syntax
for (initialize counter ; test counter; increment counter)
{
}
Concept of execution of For loop
| 1. |
Initial condition |
|
| 2. |
testing condition |
|
|
if true (step 3a) and if false (step 3b) |
|
| 3a. |
body of the loop |
3b . Stop (out of the loop) |
| 4. |
counter |
|
| 5. |
goto step 2 and run till the test condition is true. |
|
- Initialization,testing and incrementation part of for loop can be replaced by any valid expression.
Example
printf(”%d”,i);
printf(”%d”,i);
- for (i=1; i<=10;printf(”%d”,i++))
;
written by Shweta
Recent Comments