what is th deffirence

C

CDROOM

I want know what is the deffirence between
while loop , for loop and do while loop.
and when every one use.
 
K

Keith Thompson

CDROOM said:
I want know what is the deffirence between
while loop , for loop and do while loop.
and when every one use.

If you have a C textbook (or perhaps a decent online tutorial), it
will tell you.

If you don't, I'm afraid you're not going to be able to learn the
language well enough to be able to use while loops, for loops, or do
while loops.

We welcome questions about the C language (and the FAQ is at
<http://www.c-faq.com/>), but you can't expect to learn the entire
language by asking questions here.
 
C

Clever Monkey

CDROOM said:
I want know what is the deffirence between
while loop , for loop and do while loop.
and when every one use.
Any good C book will tell you this. Start here:
<http://en.wikipedia.org/wiki/Control_flow#Loops>

- for: when you want to iterate a relatively known and fixed number of times
- while: you want to iterate 0 or more times
- do ... while: you want to iterate 1 or more times

Each one of these can be coerced to behave like the others so style has
a lot to do with how they are used (in many cases). Also, you can exit
early from any of these loops, regardless of the test(s) you have in place.
 
R

Richard Bos

Clever Monkey said:
Any good C book will tell you this. Start here:
<http://en.wikipedia.org/wiki/Control_flow#Loops>

- for: when you want to iterate a relatively known and fixed number of times

This is C, not BASIC. The number is not necessarily even relatively
known; but the set of things you want to iterate over is. In BASIC (or
Pascal, or...) you'd have to know the number of members of your set
before for-looping; in C, all you need to know is what its end looks
like.
- while: you want to iterate 0 or more times
- do ... while: you want to iterate 1 or more times

for: you want to iterate over a set of items (numbers, list members,
user input, etc.)
while: you want to iterate 0 or more times, over a non-listable set or
not over a set at all, _and_ where you can test the condition
before the loop body has been executed.
do: see while, but for 1 or more times; or where you can't test the
condition until your loop body has processed its data once.

Richard
 
C

Clever Monkey

Richard said:
This is C, not BASIC. The number is not necessarily even relatively
known; but the set of things you want to iterate over is. In BASIC (or
Pascal, or...) you'd have to know the number of members of your set
before for-looping; in C, all you need to know is what its end looks
like.
I'm not sure I see the difference you are stressing here. My advice was
correct enough in the general sense. There is no requirement that the
loop contain a set of anything, in which case it is just a number of
times to iterate.

The differ loop forms are essentially syntactic sugar. I'm speaking of
typical usage. I see for loops used when one knows they have a fixed or
finite number of things to do.
 
D

Duncan Muirhead

Any good C book will tell you this. Start here:
<http://en.wikipedia.org/wiki/Control_flow#Loops>

- for: when you want to iterate a relatively known and fixed number of times
- while: you want to iterate 0 or more times
- do ... while: you want to iterate 1 or more times

Each one of these can be coerced to behave like the others so style has
a lot to do with how they are used (in many cases).

Well yes and no. It is true that for( init; cond; ) { some_code }
is the same as init; while( cond) { some_code } but a while
equivalent of for( init; cond; incr) { some_code } has to modify
some_code when it contains continue instructions.
I'd say the essential difference between for loops and the others is that
in a for loop a continue jumps to the the third clause of the for
instruction, but for a while or do loop continue jumps to the condition.

Duncan
 
R

Richard Bos

Clever Monkey said:
I'm not sure I see the difference you are stressing here. My advice was
correct enough in the general sense. There is no requirement that the
loop contain a set of anything, in which case it is just a number of
times to iterate.

The C for loop, unlike most other for loops, is not limited to a fixed
or even semi-fixed number of times, and this can be very useful at
times.

for (member=head; member; member=member->next) ...
for (key=strtok(text, ",;"); key; key=strtok(NULL, ",;")) ...
for (input=open_log(logfile); input; input=read_log(logfile)) ...

In the first case, the number of times is only known by counting the
number of members first (which will itself require a similar loop!), and
may even be changed by the body itself. In the second case, an upper
limit on the number of times can be determined, but the actual number of
tokens found is unknown before the loop. In the third case, which
assumes you've written functions to open and browse a logfile, the
number of items isn't necessarily even known in advance; someone could
have attached your logfile to a console for testing purposes...

Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top