Home > Mobile >  For loop and while loop comparison
For loop and while loop comparison

Time:01-11

So I did the next exercise, just with while loop:

Write a program that prompts the user for two integers. Print each number in the range specified by those two integers.

Here is the code:

#include <iostream>

int main()
{
    std::cout << "Write two numbers: " << std::endl;
    int v1 = 0, v2 = 0;
    std::cin >> v1 >> v2;
    std::cout << "The numbers between " << v1 << " and " << v2 << " are: " << std::endl;

    while (v2 < v1 &&   v2 < v1)
    {
        std::cout << v2 << std::endl;
    }

    while (v1 < v2 &&   v1 < v2)
    {
        std::cout << v1 << std::endl;
    }

}

Now I have to do it with the for loop, which I did like this:

#include <iostream>

int main()
{
    std::cout << "Write two numbers: " << std::endl;
    int a, b;
    std::cin >> a >> b;
    std::cout << "The numbers between " << a << " and " << b << " are: " << std::endl;

    for (; a < b &&   a < b; a)
    {
        std::cout << a << std::endl;
    }
    
    for (; b < a &&   b < a; b)
    {
        std::cout << b << std::endl;
    }
}

It looks almost the same, but it works.

My questions is: I'm I missing something about the for loop, could I do it simpler?

PD: Just for loop, I'm not in the If chapter yet, I want to go step by step on the "C Primer 5th edition".

CodePudding user response:

for is specified in terms of while, you aren't missing anything.

for (init-statement conditionopt; iteration-expressionopt) statement

produces code equivalent to:

{ init-statement while (condition) { statement iteration-expression; } }

Except that

  1. Names declared by the init-statement (if init-statement is a declaration) and names declared by condition (if condition is a declaration) are in the same scope (which is also the scope of statement).
  2. continue in the statement will execute iteration-expression
  3. Empty condition is equivalent to while(true)

from cppreference

You don't need anything in iteration-expression for(;;) is equivalent to while(true)

it would be more normal to increment in the iteration-expression, and not repeat almost the same test.

#include <iostream>

int main()
{
    std::cout << "Write two numbers: " << std::endl;
    int a, b;
    std::cin >> a >> b;
    std::cout << "The numbers between " << a << " and " << b << " are: " << std::endl;

    for (; a < b;   a)
    {
        std::cout << a << std::endl;
    }
    
    for (; b < a;   b)
    {
        std::cout << b << std::endl;
    }
} 

CodePudding user response:

There is no need on using something like a < b && a < b since a < b is contained in a < b condition. So just using a < b you will get the same results.

Now about the for you should write it like this just to make your code a bit clear:

for (; a < b;   a)
{
    std::cout << a << std::endl;
}

I am not using the initialization sentence as you have already initialized your variables, however I encorage you to initialize a in the for sentence

In general terms, the for is divided in three sections:

for (<initialization sentence>; <condition sentence>; <post-execution sentence>)

The initialization only runs when the for sentence is reached, and the loop is running while the condition is met. The post-execution it is normally used to increase or change state of the variables involved in the condition criteria. The only constraint you have is that condition must be a boolean sentence.

None of those sections should be an assigment, a common comparison or a variable increment. You could use whatever fits your requirements and met the for constraints.

To sum up, a for sentence is a 'wrapped' structure of a while. You could get the same results with both. The difference is on a cleaner image of your code and a better understanding of the algorithms.

CodePudding user response:

The for loop is intended to loop between two numbers. The use of your for loops unreadable for it's intentions. Take a look at this

int end = 10;
for (int begin = 0; begin < end;   begin){/*do something*/}

This is the standard structure of a for loop. Now for your example you will get the following

#include <algorithm>
if (a > b) std::swap(a, b);
for (int begin = a; begin <= b;   begin){
    std::cout << begin << std::endl;
}
  •  Tags:  
  • Related