Looping in c


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.

56 comments:

karthik said...

can any one help me why 4 b got answer as 5 even the condition is false

sofi said...

can anyone temme why the ans for 3a is infinite loop

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.

Hi sofi,
You can see in each iteration value of i becomes zero. so loop condition will always true.

anish said...

Please explain 1(b) and 3(b) more clearly

anish said...

sorry not 3(b) but 3(a) how it will be infinite loop and not 0 1 2 3

Priyanka kumari said...

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

anish kumar said...

ya it is clear now ,thanks

Anonymous said...

thanks for this site dude

Anonymous said...

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.

Anonymous said...

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

cyberway91@gmail.com said...

nice yar! i love this

chandan said...

need triangular floyd's program code

Anonymous said...

can any one explain the last code plz
its confusing..
the condn value is i.e 8<2

Anonymous said...

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

Anonymous said...

i think the condition 7>6 is not checked

Anonymous said...

@ 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.

jyoti said...

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

Unknown said...

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...:)

vivek said...

#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

Anonymous said...

question 4 part b
...pls check it...

vishnuu said...

please explain me the difference between 1(A) nd 1(b)......

Unknown said...

how to...manage a....numbers in incresing or decresing ...e.g....
enter number - 5
enter number - 8
enter number - 7....

o/p- 5,7,8,...and
8,7,5,...

Abhishek Alwani said...

Nice Work Dude..!!!

seb said...

Write a program that reads a number, then reads a single digit and determines how many times the digit
occurs in the number.

Anonymous said...

write a program to enter the number till user wants and at the end it ahould display the count of positive, negaitve and zeros entered.

འབྲུག། said...

write a program to receive an integer and find its octaleqivalent

Anonymous said...

Is 1(b) from "Properties of expression2" correct?

Anonymous said...

Hey please explain 1 (b)
How 2 get printed 6 times though j>=0 gets false
Please explain this I am really confused

Anonymous said...

yes

Preeti said...

#include
#include
int main()
{ int no,i=1,pos=0,neg=0,zero=0;
printf("Enter five no \n");
while(i<=5)
{
scanf("%d",&no);
if(no>=0)
if(no==0)
zero+=1;
else
pos+=1;
else
neg+=1;
i++;

}
printf("\npositive =%d ",pos);
printf("\nzero =%d",zero);
printf("\nnegative =%d",neg);

return 0;
}

Anonymous said...

>> please write a program that outputs the given whole number using 3 looping statements use increment and another program using decrement. thank you :)

Anonymous said...

write a C program that accepts a text message entered by user; display number of occurrences of each letter that occurs in the text message, print number of words in the message, quit.

saesha said...

please explain expression 3 -> 1 ->b

Yogesh Gaikwad said...

add fflush(stdin); function after i++...

Unknown said...

hello i want to ask u the question

Anonymous said...

nothing will come on the screen

Anonymous said...

Since the declaration has been done already in for loop. It Doesn't affect for that value ; i.e whether the loop works or not.

Pain_of_love said...

hi...its a bug of turbo c compiler

Pain_of_love said...

can anyone explain the expression 2 output

Unknown said...

Sir Explain Fabonacci series

Unknown said...

what is the code of the program with this output..?
1 1
2 22
3 333
4 4444

Mekelle University Alumni Director Office said...

goodf

Unknown said...

i have compiled it in linux ....it doesn't show any output...means that when the condition is false next step will not execute...it simply come out of the program

Unknown said...

for(;i=0,i<=3 ;i++){

Here you are not using expression 1. But in condition (expression 2) you are initializing "i" as zero. So each & every time you check the condition the value of "i" becomes zero. The condition never fails. So it becomes an infinite loop

Unknown said...
This comment has been removed by the author.
Unknown said...

To get rid of those kind of bugs use "LINUX" or http://code.geeksforgeeks.org/index.php (online compiler)

Unknown said...

See the condition part clearly... for(;i=0,i<=3 ;i++)
Here we are not using initialization part. "i=0" is in conditional part. Thats why it become infinite loop.

Suppose if you write i=0 in initialisation part then it will print 0 1 2 3

Unknown said...

Here the scope of the variables is within the braces only.

for(i=0;i<=2;i++)
{
int i=8; //i=8 is in these two lines only
printf("%d ",i); //After closing the braces the scope of i will lost

}

So it will print 8 three times only

Unknown said...

The condition is i<=2. So even i=2 the condition is true and its print 8. after that i is incremented & become 3. Now the condition fails. And print the i value as 3

Unknown said...

for(i=0;i<=5,j>=0;i++) Here the compiler will take j>=0 as condition (1A)

for(i=0;j>=0,i<=5;i++) Here the compiler will take i<=5 as condition (1B)

because the comma operator will work on the most right operand.


for(i=0;j>=0,i<=5;i++) is equal to for(i=0;0,i<=5;i++)

The compiler wont check the remaining conditions. It will check only the most right expression

If you want to check two conditions then use && operator between them.

Ex: for(i=0;j>=0 && i<=5;i++)

Unknown said...

No, Its wrong. The compiler will check only consider the most right-side expression if we use comma operator. If you want to check two conditions the use && operator.

Unknown said...

The given statement is wrong. It will not check two conditions if you use comma operator. It will check only the most right-sided expression. In 1(A) the right side expression is j>=0. So it print three times. But in 1(B) the right sided expression is i<=5. So it prints 6 times. If you want to work with two conditions then use && operator not comma operator

Unknown said...

#include
int main()
{
int i,j,k;
for(i=1;k=0,i<5;i++)
{
for(j=0;j<i+1;j++,k++)
{
printf("%d",i);
if(k==0)printf(" ");
}
printf("\n");
}
}

Unknown said...

it,s very good

Unknown said...

Clear and straightforward explaination of every pcode snippet.thanks keep going..

Unknown said...

#include
int main(){
int i=0;

for(;i+=2,i<5 ;i++){
printf("%d ",i);
}
return 0;
}
pls anyone explain this ques