1,105,406 Community Members

Difference between "WHILE" loop and "FOR" Loop in C language?

Member Avatar
pittdaniweb
Newbie Poster
2 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hello everybody,
I was asked by an interviewer the following question. but I was not able to answer. Please get back to me if you know the answer. thank you in advance.


Que: What is the exact difference between the "for" loop and "while" loop in C language ?

Member Avatar
Ancient Dragon
Achieved Level 70
27,645 posts since Aug 2005
Reputation Points: 5,232 [?]
Q&As Helped to Solve: 3,038 [?]
Skill Endorsements: 115 [?]
Team Colleague
Featured
Sponsor
 
4
 

Any first year C programming student should be able to answer that question. If you could not then you are not yet ready to work as a professional programmer.

Member Avatar
Narue
Bad Cop
12,139 posts since Sep 2004
Reputation Points: 5,693 [?]
Q&As Helped to Solve: 1,537 [?]
Skill Endorsements: 81 [?]
Team Colleague
 
1
 

That was a question for a job interview? If you couldn't answer it, I'm inclined to say that you're a perfect candidate. :icon_rolleyes:

Anyway, the expected answer is probably that the for loop is counted and the while loop is not. Though that's only a superficial difference because you can simulate the behavior of either loop with the other.

Member Avatar
N1GHTS
Posting Whiz in Training
289 posts since Sep 2010
Reputation Points: 98 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 1 [?]
 
0
 

Que: What is the exact difference between the "for" loop and "while" loop in C language ?

Ans: About 20 lines of code.

Ba-dum-PSS! *Insert laughter Here*

http://www.codingunit.com/c-tutorial-for-loop-while-loop-break-and-continue

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
0
 

You're all off the mark. The answer is "Whatever you want the difference to be." ;)

Member Avatar
nmaillet
Posting Pro
535 posts since Aug 2008
Reputation Points: 97 [?]
Q&As Helped to Solve: 106 [?]
Skill Endorsements: 7 [?]
 
0
 

Seriously, take some courses (or better yet, get a degree). For and while loops are used in so many common languages. You are not ready for any kind of software development career.

Member Avatar
pyTony
pyMod
6,104 posts since Apr 2010
Reputation Points: 818 [?]
Q&As Helped to Solve: 1,056 [?]
Skill Endorsements: 42 [?]
Moderator
Featured
 
0
 

For me the only significant difference except for superficialities (put first statement of for as first inside while and third at end of while and keep the middle one) is that for parameters are optional, while parameter is not, so for( ; ; ) is correct infinite loop but while() is not.

Member Avatar
caut_baia
Posting Whiz
386 posts since Apr 2010
Reputation Points: 9 [?]
Q&As Helped to Solve: 49 [?]
Skill Endorsements: 0 [?]
 
0
 

I too am looking for a job:">. Is this the idea? (I don't know assembly too well)

For loop:

for (int x=0;(x*2)<100;x++)  {}
   
   mov   eax,   0  
@head:

   ;;loop body

   inc  eax
   imul 2
   cmp  eax,   100
   ja   @end  
   jmp  @head     
@end:

While loop:

int x=5;
while (x)  {
   x--;
   //loop
 }

@head:
   mov   ecx,   5  ;;number of loops

   ;;loop body

   loop  @head     ;;jumps at the beginning of the loop if ecx!=0
Member Avatar
pittdaniweb
Newbie Poster
2 posts since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

1. To : Ancient Dragon,Adak,namillet,WaltP
If you think the question is basic,Just do not respond to the post. I do not need your crap replies. because I do not want to reply you in the same way which is not professionalism.

2. To : Others who were able to give me information.

Thank you guys for your time.

3. To : DaniWeb blog administrator
Request you to delete my account from this blog as soon as possible because I see some worst people here who are making the blog environment dirty.


Thank you

Member Avatar
Adak
Posting Virtuoso
1,711 posts since Jun 2008
Reputation Points: 419 [?]
Q&As Helped to Solve: 207 [?]
Skill Endorsements: 10 [?]
 
1
 

OK, you're serious.

The for loop is the more complete loop. In fact the Go language from Google, doesn't have any loop other than the for loop. Anything you can do with a while loop, you can do with a for loop.

The while loop doesn't include an inital assignment to the format, so it's standard idiom to see one or more assignments before the while loop begins:

i=0;
while(i<10) {
  printf("%d\n", i);
  ++i;
}

The typical while loop includes just a test between it's parenthesis, but here's one with an assignment first:

while((ch=getchar()) != '\n') {
  countchar++;
}

The for loop syntax in C, requires more than just a single value in the while test:

i = 0;
while(1) {
  if(i++ == 9) break; //a break is required or it's an endless loop
}
for(1) { <<== illegal syntax

for(;;) { //legal, note the two semi-colons
  if(i++== 9) break;
}

So at a deep logic level, there is no difference between them. Anything you can do in a for loop, you can also do in a while loop, and vice versa.

In common usage, when you know the number of times the loop will need to iterate around, then a for loop is more intuitive. When you don't know how many times a loop will need to iterate, then a while loop is slightly clearer, and is usually used.

Member Avatar
N1GHTS
Posting Whiz in Training
289 posts since Sep 2010
Reputation Points: 98 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 1 [?]
 
0
 

1. To : Ancient Dragon,Adak,namillet,WaltP
If you think the question is basic,Just do not respond to the post. I do not need your crap replies. because I do not want to reply you in the same way which is not professionalism.

2. To : Others who were able to give me information.

Thank you guys for your time.

3. To : DaniWeb blog administrator
Request you to delete my account from this blog as soon as possible because I see some worst people here who are making the blog environment dirty.


Thank you

In DaniWeb, posters are discouraged from completely giving away answers for the benefit of the student programmer. They are supposed to teach you the answer and let you carve out your own path to success.

Your post incorrectly emphasised the need for an answer alone. It implies that you want to take the quick answer and use it next time in an interview so you can prove how smart you are at C.

You should have instead told us what you already know and why you are confused by the two loops. It is hard to teach someone when we don't know what they are thinking first.

We are not here to give out answers, especially for someone who appears to be cheating.

Member Avatar
sergent
Posting Pro
593 posts since Apr 2011
Reputation Points: 52 [?]
Q&As Helped to Solve: 26 [?]
Skill Endorsements: 0 [?]
 
3
 

Anything you can do with a while loop, you can do with a for loop.

Anything you can do with a for loop, you can do with a while loop.
To the OP: If you get ANY programming book about C or C++, you WILL find both kinds of loops in the first or second chapter. It is THE most basic part of programming. STOP BEING SO LAZY!

Member Avatar
N1GHTS
Posting Whiz in Training
289 posts since Sep 2010
Reputation Points: 98 [?]
Q&As Helped to Solve: 21 [?]
Skill Endorsements: 1 [?]
 
0
 

Anything you can do with a for loop, you can do with a while loop.

The way I see it, this topic concerns the functionality of the loop syntax itself. In C you can do anything with anything (literally), but the loop operators themselves have a defined function and inherent limitations.

With the for loop you can initialise variables, check for a series of conditions, and do work that affects the conditions. All this without adding any extra code (which would defeat the purpose of displaying the power of the loop operator.)

for (i=0;check(i);do_work(i));

The while loop simply cant do the same number of things without adding code outside the while loop.

i=0; while (check(i)) do_work(i);
Member Avatar
amrita111
Junior Poster in Training
79 posts since Aug 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 1 [?]
 
0
 
#include<stdio.h>
int main()
{
    int i=1;
    while()
    {
        printf("%d\n", i++);
        if(i>10)
           break;
    }
    return 0;
}

will report an error because there isnocondition in while brackets while

#include<stdio.h>
int main()
{
    int i=1;
    for(;;)
    {
        printf("%d\n", i++);
        if(i>10)
           break;
    }
    return 0;
}

will give you the answer..THIS IS ONE OF THE DIFFERENCE. :)

Member Avatar
Anuradha Mandal
Junior Poster
122 posts since Sep 2010
Reputation Points: -28 [?]
Q&As Helped to Solve: 9 [?]
Skill Endorsements: 0 [?]
 
0
 
Member Avatar
sergent
Posting Pro
593 posts since Apr 2011
Reputation Points: 52 [?]
Q&As Helped to Solve: 26 [?]
Skill Endorsements: 0 [?]
 
0
 
    #include<stdio.h>
    int main()
    {
        int i=1;
        while()
        {
            printf("%d\n", i++);
            if(i>10)
               break;
        }
        return 0;
    }

will report an error because there isnocondition in while brackets while

#include<stdio.h>
int main()
{
    int i=1;
    for(;;)
    {
        printf("%d\n", i++);
        if(i>10)
           break;
    }
    return 0;
}

will give you the answer..THIS IS ONE OF THE DIFFERENCE.
What about while(true), or while(1). Doesn't seems that hard to me :D

Member Avatar
amrita111
Junior Poster in Training
79 posts since Aug 2010
Reputation Points: 4 [?]
Q&As Helped to Solve: 6 [?]
Skill Endorsements: 1 [?]
 
1
 

@sergent
Whenever we mention condition in the bracket,they are evaluated to either true or false.In the above program,nothing is mentioned in the braces and thats why an error will be generated.Mr you have mentioned the condition in while loop.:D

Member Avatar
jnawrocki
Light Poster
49 posts since Aug 2010
Reputation Points: -3 [?]
Q&As Helped to Solve: 5 [?]
Skill Endorsements: 0 [?]
 
0
 

wow

"for" is a complete loop control statement. for(assignment component; test component; indexing component)
i.e. for(int i = 0; i < 10; ++i)

"while" is a limited "for" while(test component) i.e. while (i < 10)

You could code while (i++ <10) but that would be rather convoluted.

Member Avatar
ashok1514
Newbie Poster
17 posts since Aug 2011
Reputation Points: -4 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
-1
 

The main difference between while and for loop is "for" can execute without condition but when we execute while function the condition is mandotary........


Ex:

void main()
{
int i;
for(;;) //Means an infinate loop
{
if(i<10)
break;
i++;
}
printf(" %d ",i);
}

-----No error executes succesfully----

Ex:

void main()
{
int i;
while()
{
if(i<10)
break;
i++;
}
printf(" %d ",i);
}

-----Gives an error message-----

Member Avatar
Schol-R-LEA
Nearly a Posting Virtuoso
1,397 posts since Oct 2010
Reputation Points: 639 [?]
Q&As Helped to Solve: 226 [?]
Skill Endorsements: 28 [?]
 
0
 

Interesting. There have been more answers to this question since the OP announced that he was deleting his account than before he did. Given that the OP is almost certainly not reading this, it seems rather quixotic to be answering this now.

You
Post:
Start New Discussion
View similar articles that have also been tagged: