Before we use for loop statement we must Understand the loop:
When we want to execute a statement or set of statement again and again then we have two way
write the statement or set of statement again and again
for example if we want to display the message 5 time on the screen then we use pritnf statement 5 time.
printf("hello learninghints\n") ;
printf("hello learninghints\n") ;
printf("hello learninghints\n") ;
printf("hello learninghints\n") ;
printf("hello learninghints\n") ;
other simple and easy way is loop , loop execute a statement or set of statement again and again until the given condition is true. Control comes out of the loop statements once condition becomes false. with the help of loop its easy to perform those task which occur many time in same way.
For Example
for(i=0 ; i<5 ; i++)
{
printf("Hello Learninghints \n")
}
Syntax of for loop
for (exp1; exp2; expr3)
{
statements;
}
Where exp1 is the initial value of loop its like simple variable initialization for Example: i=0, j=2, k=3 etc
exp2 is an condition (relational expression) which check that when loop will terminate. for example i>5, j<3, k=3
exp3 is increment or decrement loop counter. ++i , j? , ++k
Note:- (Relational expression compare two variable or constant and give the result in shape of true or false for example 10 >12 return False)
after initialization of loop for example
for(i= 1; 1<=5 , i++)
every thing in braces or "curly brackets " { " and " } " is called body of loop will be executed the number of time of the loop.
Example :1
#include int main() { int i; for(i=1 ; i<5 ; i++) { printf("Hello learninghints"); } }
in above example loop initialize for 5 time (This loop execute 5 time) and in the body of loop printf statement display the message " Hello Learninghints"
so in the result "Hello Learninghints" display 5 time on the screen.
Example 2 : Using the loop counter variable for example i
#include int main() { int i; for(i=1 ; i<5 ; i++) { printf("%d \t ", i); } } Result
1 2 3 4 5
Explanation of above example
int i;
this is simple declaration of variable which we use in loop as counter variable.
for(i=1 ; i<5 ; i++)
this is initialization of loop which tell that loop starting value is 1 and final value is 5 so that loop will be execute each statement in the body of the loop 5 time.
printf("%d \t ", i);
this printf statment display the value of i (which is 1 to 5) on the screen
Note: - (Remember that \t is used as escape sequences which give Horizontal Tab after displaying this message).
If you like this tutorial please share with your friends.
Declaring and Initializing Local Variable in C
Declaring and Initializing Global Variable in C Language
Printf Function In C Language
Sacnf Function In C Language
for Loop in C Language
While Loop in C Language
Do While Loop in C Language
Switch Case Statement IN C
IF -Else Statement IN C
Break Statement IN C /C++
User Define Function IN C /C++
Cout/Cin Function IN C /C++
Array IN C /C++
Two Dimensional Array IN C /C++
String In IN C /C++
Pointer In IN C /C++
Const and volatile keyword IN C /C++
Class and Object in C++
Function Over Loading in C++
Character Array In C++
Constructor in C++
Operator overloading in C++
Function Templates in C++
ATM Sample Project In Simple C Language
Find Area of Shape Using Function In C++