QuestionFor vs. While Loops

Posts 1–15 of 20 · Page 1 of 2
For vs. While Loops
So, I understand both of these concepts, but to be honest, I don't see a difference or a benefit using of using one over the other. From what I've heard, the compiler doesn't differentiate between the two. I personally use while loops over for loops.
The only real difference that comes to mind is with 'do while' loops. Since their body is executed once no matter the condition.
So writing out things the same such as this:

Code:
    int x = 10;

    for (x; x < 10; x++)
    {
        std::cout << "For was ran.." << std::endl;
    }

    while (x < 10)
    {
        std::cout << "While was ran.." << std::endl;
    }

    do
    {
        std::cout << "Do was ran.." << std::endl;
    } while (x < 10);
The output of this will be:
Do was ran..

Given that do loops run at least once regardless of the condition since the body is executed before the condition.

Granted, each has their purpose on some aspects and are generally used more often than others in those positions. For example:
- For loops are generally used when iterating a container with no intent to alter the container.
- While loops are generally used when doing something until a specific condition is met.
- Do loops are generally used when stepping a data type that is iterated from a handle. (First/Next style API.)

Although these are common uses of each, it certainly isn't a requirement.
@OP
For loops have the built in initializing and updating parts: about the only difference I can think of. Generally* For loops run a known amount of times: if you know the list is 100 long, the For is expected to run 100 times. Use a do loop when you don't know how many times it should loop. Of course you can make a do loop work where a For would normally be used..but it just feels wrong.
the FOR loop is the fastest one.

the WHILE loop is used when you can't know how many times you will do that cycle in spite of the FOR
Quote Originally Posted by Sixx93 View Post
the FOR loop is the fastest one.

the WHILE loop is used when you can't know how many times you will do that cycle in spite of the FOR
r u reterded m8
Quote Originally Posted by Shanks_ View Post
r u reterded m8
Just reporting what my teacher said...

No insults needed....
Quote Originally Posted by Sixx93 View Post
Just reporting what my teacher said...

No insults needed....
who taugt u
Quote Originally Posted by Shanks_ View Post
who taugt u
you know the opcodes it all gets compiled to? for m$ and other compilers? gt fo.
Quote Originally Posted by abuckau907 View Post
you know the opcodes it all gets compiled to? for m$ and other compilers? gt fo.
show test caes pls
Quote Originally Posted by Shanks_ View Post
show test caes pls
This is what you should have asked six93. Don't be cruel...
Quote Originally Posted by Sixx93 View Post
the FOR loop is the fastest one.
. Not true as "while" and "for" loops in most cases result in the same set of opcodes.
Quote Originally Posted by Sixx93 View Post
the FOR loop is the fastest one.
Not true. As I said before, they're the same thing when they get to the compiler.
Quote Originally Posted by Woodhouse View Post

Not true. As I said before, they're the same thing when they get to the compiler.
I've never checked it... I'm just saying what my teacher told me some years ago
Quote Originally Posted by Woodhouse View Post
... I don't see a difference...
1: for (int x = 0; x < 100; ++x) // all the control logic in one line, not throughout code body
2: {
3:
4: }

.................
1: int x = 0;
2: while (x < 100)
3: {
4:
5: ++x; // I almost forgot to add this. Silly mistake.
6: }

The first is arguably cleaner if you know loop count. Functionally equivalent: it comes down to style/compactness/readability.
Quote Originally Posted by Woodhouse View Post
So, I understand both of these concepts, but to be honest, I don't see a difference or a benefit using of using one over the other. From what I've heard, the compiler doesn't differentiate between the two. I personally use while loops over for loops.
For loops are usually used when you know exactly the number of required iterations, while loops are useful when you're just waiting for a certain condition to end the loop. Generally you can always tweak your algo to use one or the other
Posts 1–15 of 20 · Page 1 of 2

Post a Reply

Similar Threads

Tags for this Thread

Need help?