for Loop
- It works as while loop, But Syntax is very good as compare to other two loops.
- Condition is checked before entering in the body of loop.
- You can also call this as ,entry control loop.
- Many programmer Prefer to go with is loop , because In this loop
initialization, condition, a nd increment of the variable is done in an single line, Because of that You can Understand Concept of loop in better way. - Reading ability is very good as compare to other two loop.
Syntax
for(initializing ; condition ; increment)
{
//body of for loop.
}
for(initializing ; condition ; increment) { //body of for loop. }
Lets see the same program of printing number from 1 to 5 ,with for loop.
#include<stdio.h> //header file
void main()
{
int i;
for(i = 1; i <= 5 ; i++)
{
// body of for loop.
printf("%d\n",i); //Printing the value of i , for every iteration.
}
}
#include<stdio.h> //header file void main() { int i; for(i = 1; i <= 5 ; i++) { // body of for loop. printf("%d\n",i); //Printing the value of i , for every iteration. } }
Output:
1
2
3
4
5
2
3
4
5
- Step by Step Explanation.
- Valid values of i = 1,2,3,4,5 , for this program.
- For i = 1,First Condition got checked and it was evaluated to true, so it print the value of i. which is 1 and incremented it by 1.
- Now value of has become 2,condition got check it evaluated to true and it printed the value of i which is 2 and incremented it by 1.
- Now i=3,Again with i =3,condition got check it evaluated to true and it printed the value of i which is 3 and incremented it by 1.
- Above step , will repeat until the condition gets evaluated to false.
- In this example, for value of i = 6 ,loop will get terminate(stop) and control will go to the next piece of code.
- Control will not stay within the for loop, it will go to the next statements of the code.
Further Concepts
- break keyword
- continue keyword
- difference between break and Continue keywords
- Nested if-else statement
- Practice Programs
Practice Programs
- Programs Regarding If-else Statements
- Program Regarding Nested If - statements
- Programs Regarding loops
Further Topics
- What are Array?
- Why Array?
- Types of Array
- How to declare array ?
- how to Initialize an array?
- What is One Dimensional Array
- Concept of One Dimensional Array
- Concept of Initializing One Dimensional Array
- How to take array elements as input from the User
- What is Two Dimensional Array ?
- Concept of Two Dimensional Array
- How to initialize Two Dimensional Array?
- How to take Elements of Two dimensional array from the User?
- How to display 2D Array As an Table?
- Practice Program Regarding Arrays
People Also Searched:
People Also Searched:
0 Comments
Post a Comment