Explanation
of loops or looping in c programming language by examples and questions
Looping is the
process of repeating of same code until a specific condition doesn’t satisfy.
In c there are three types of loop:
(a)loop
(b)while loop
(c)do while
for loop:
This loop is
used when we have to execute a part of code in finite times. It is per tested
loop. Syntax of for loop:
for (Expression 1; Expression 2;
Expression 3){
Loop body
}
Order of movement of control in for loop:
First time:
Expression
1-> Expression 2->Loop body -> Expression 3
Second time and
onward:
Expression 2->Loop body ->
Expression 3
That is
expression 1 only executes in the first iteration. From second iteration and
onward control doesn’t go to at the expression 1.For example:
#include<stdio.h>
int main(){
int i;
for(i=0;i<=4;i++){
printf("%d ",i);
}
return 0;
}
Output: 0 1 2 3 4
Explanation of each term of syntax:
Expression 1:
It is called
initialization expression. Task of this expression is to initialize the looping
variables.
Properties
of expression 1:
1. Expression1
can initialize the more than one variable. For example:
#include<stdio.h>
int main(){
int i,j,k;
for(i=0,j=2,k=1;i<=4;i++){
printf("%d ",i+j+k);
}
return 0;
}
Output: 3 4 5 6 7
2. Expression1 is optional. For example:
#include<stdio.h>
void main(){
int i=1;
for(;i<=4;i++){
printf("%d ",i);
}
return 0;
}
Output: 1 2 3 4
3. Unlike to the
java in c we cannot declare the variable at the expression1. For example:
#include<stdio.h>
int main(){
for(int i=0;i<=10;i++){
printf("%d ",i);
}
return 0;
}
Output: ce
Expression 2: It
is called as conditional expression. Task of expression is to check the
condition and if it is false then it terminates the loop. For example:
#include<stdio.h>
int main(){
int i;
for(i=1;i<=3;i++){
printf("hi ");
}
printf("%d",i);
return 0;
}
Output: hi hi hi 4
Properties
of expression2:
1.
Expression2 can have more than one checking condition and if any
condition is false loop will terminate. For example:
(a)
#include<stdio.h>
void main(){
int i,j=2;
for(i=0;i<=5,j>=0;i++){
printf("%d ",i+j);
j--;
}
return 0;
}
Output: 2 2 2
(b)
#include<stdio.h>
int main(){
int i,j=2;
for(i=0;j>=0,i<=5;i++){
printf("%d ",i+j);
j--;
}
return 0;
}
Output: 2 2 2 2 2 2
2. Expression2 is also optional. For
example:
#include<stdio.h>
int main(){
int j;
for(j=0; ;j++){
printf("%d ",j);
if(j>=2)
break;
}
return 0;
}
Output: 0 1 2
3. It can
perform task of expression1 as well as expression3. That is it can initialize
the variables as well as increment the variables. For example:
(a)
#include<stdio.h>
int main(){
int i;
for(;i=0,i<=3 ;i++){
printf("%d ",i);
}
return 0;
}
Output: Infinite Loop
(b)
#include<stdio.h>
int main(){
int i=0;
for(;i+=2,i<5 ;i++){
printf("%d ",i);
}
return 0;
}
Output: 2
4. If
expression2 is zero means condition is false and any non zero number means
condition is true. For example
(a)
#include<stdio.h>
int main(){
int i;
for(i=0;-5 ;i++){
printf("%d ",i);
if(i==3)
break;
}
return 0;
}
Output: 0 1 2 3
(b)
#include<stdio.h>
int main(){
int i;
for(i=5;0 ;i++){
printf("%d ",i);
}
return 0;
}
Output: 5
Expression 3:
It is called as
instrumentation expression. Task of this expression is to increment the
variable. Properties:
1. We can
increment more than one variable at the same time in the expression3. For
example
(a)
#include<stdio.h>
int main(){
int i,j,k;
for(i=0,j=0,k=0;i<=5,j<=4,k<=3;i++,++j,k+=2){
printf("%d ",i+j+k);
}
return 0;
}
Output: 0 4
(b)
#include<stdio.h>
void main(){
int i,j=0;
for(i=0;i<=3;++i,i++,++j ){
printf("%d %d ",i,j);
}
return 0;
}
Output: 0 0 2 1
2. Expression 3 is also optional. For
example
#include<stdio.h>
int main(){
int i;
for(i=0;i<=3; ){
printf("%d ",i++);
}
return 0;
}
Output: 0 1 2 3
Loop
body:
Loop body
contains the part of code which we have to execute multiple numbers of times.
Properties of loop body:
1. If loop body
contain only one statement then brace is optional. For example:
(a)
#include<stdio.h>
int main(){
int i,j=0;
for(i=0;i<=3;++i,i++,++j )
printf("%d %d ",i,j);
}
return 0;
}
Output: 0 0 2 1
(b)
#include<stdio.h>
int main(){
int x,y=5;
for(x=0;x<3;x++)
if(y>=5)
printf(" %d",x);
return 0;
}
Output: 0 1 2
2. Loop without body is possible. For
example
#include<stdio.h>
int main(){
int i;
for(i=0;i<=10;i++);
printf("%d",i);
return 0;
}
Output: 11
3. Braces of loop body behave as block.
For example
#include<stdio.h>
int main(){
int i;
for(i=0;i<=2;i++){
int i=8;
printf("%d ",i);
}
printf("%d",i);
return 0;
}
Output: 8 8 8 3
While loop
Do while loop
break and continue
Nested loop
C tutorial home.

19 comments:
can any one help me why 4 b got answer as 5 even the condition is false
can anyone temme why the ans for 3a is infinite loop
Hi karthik,
You are right. It is bug of turbo c compiler. Loop will execute at one time even condition is false.
Hi sofi,
You can see in each iteration value of i becomes zero. so loop condition will always true.
Please explain 1(b) and 3(b) more clearly
sorry not 3(b) but 3(a) how it will be infinite loop and not 0 1 2 3
Hi Anish,
3(a)
As you know i=0,i<=3 will be evaluated in each iteration. So due to i=0 value of i will alway 0.
So loop will print 0, 0, 0 and so on
ya it is clear now ,thanks
thanks for this site dude
void main()
int i;
clrscr();
for(i=0;7<=6;i++)
{
printf("santosh");
}
getch();
}
HOW THIS CODE IS WORKING ..PLZ XPLAIN IT..I AM CHKED THIS CODE IN TURBO C , I AM GETTING O/P SANTOSH EVEN THE CODITION IS FALSE.
void main()
{
int i;
clrscr();
for(i=0;7<=6;i++)
{
printf("santosh");
}
getch();
}
HOW THIS CODE IS WORKING ..PLZ XPLAIN IT..I AM CHKED THIS CODE IN TURBO C , I AM GETTING O/P SANTOSH EVEN THE CODITION IS FALSE
nice yar! i love this
need triangular floyd's program code
can any one explain the last code plz
its confusing..
the condn value is i.e 8<2
Dear first Anonymous, see the comment below
Ritesh kumar said...
Hi karthik,
You are right. It is bug of turbo c compiler. Loop will execute at one time even condition is false.
7/31/10 12:10 AM
i think the condition 7>6 is not checked
@ 8<2:-Default storage class of local variable is auto. Scope of auto variables are block in which it has been declared. When program control goes out of the scope auto variables are dead. So variable i which has been declared inside for loop has scope within loop and in each iteration variable i is dead and re-initialized.
can anyone tell me the ans of last question is 8,8,2 how is it why it it is not 8,8,8,3
Hey Karhtik, brother u r doing such a nice job keep it up and God will keep u in hearts of us....
thank you and ur blogspot...:)
#include
int main(){
int i;
for(i=0;i<=2;i++){
int i=8;
printf("%d ",i);
}
printf("%d",i);
return 0;
}
Output: 8 8 3
but it should be 8 8 8 3.if not why
Post a Comment