Turning while loop into a for statement

closed account (EwCjE3v7)
Is it possible to turn this while loop program and use for statement ..if so ..can u write the for statement code under ..thanks


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream> // Include iostream
using namespace std; // we are using namespace standard

int main()
{
    int num = 0, num2 = 0; // Two ints,one called num and another called num2,both installized to 0
    cout << "Type two numbers to see the numbers in range: " << endl; // send text to user
    cin >> num >> num2; // user input the new value for num and num2
    cout << "" << endl; // Just a blank line

    while (num > num2) { // keep executing until the condition is false
        cout << num << endl; //send value of num
        --num; //take 1 away from num
    }
    while (num2 > num) { // keep executing until the condition is false
        cout << num2 << endl; //send value of num2
        --num2; //take 1 away from num2
    }
    while (num = num2) { // keep executing until the condition is false
        cout << num << endl; //send value of num
        break; //breaks
    }

    return 0; //We return 0
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream> // Include iostream
using namespace std; // we are using namespace standard

int main()
{
    int num = 0, num2 = 0; // Two ints,one called num and another called num2,both installized to 0
    cout << "Type two numbers to see the numbers in range: " << endl; // send text to user
    cin >> num >> num2; // user input the new value for num and num2
    cout << "" << endl; // Just a blank line

    for (;num > num2;) { // keep executing until the condition is false
        cout << num << endl; //send value of num
        --num; //take 1 away from num
    }
    for (;num2 > num;) { // keep executing until the condition is false
        cout << num2 << endl; //send value of num2
        --num2; //take 1 away from num2
    }
    for (;num == num2;) { // keep executing until the condition is false
        cout << num << endl; //send value of num
        break; //breaks
    }

    return 0; //We return 0
}
closed account (EwCjE3v7)
Thanks Pebble.
Even though it is possible to convert any of the 3 looping statements to another, there are times where one particular form is preferred.

In this case, I think one is better off staying with the while loops.

For loops are good when the number of times to loop is known, while loops are better when the end condition depends on something else.

The last example

1
2
3
4
5
while (num == num2) { // equality comparison, not assignment
    cout << num << endl; //send value of num
    break; //breaks
}
is equivalent to an if statement. :

1
2
3
if (num == num2) {
     std::cout << num << std::endl;
}


And the form while(num == num2) {} could easily lead to an infinite loop, if one forgot to break out of it. So for this reason, I would avoid it.

Here is some more info, just in case you weren't aware already :

Infinite loops are needed when the end condition is complicated and / or dependent on other variables. They must have a coherent base case, that is, a sane exit point.

I am old fashioned, so I always use for(;;) rather than while(true) for infinite loops, because it will always work in any C like language.

IMO, beginners should not routinely use infinite loops because they are too lazy to think of an end condition. Obviously they are fraught with danger when no end condition is provided at all, anywhere in the loop.

Hope all goes well :-)
closed account (EwCjE3v7)
Thanks theideasman
In my book it told me to create this with while loops....so I couldn't use if statement...than later it told me to convert all my while loop projects into a for statement ....and than told me what are the advantages and disadvantages...so that's why..than again
I guess the aim of the exercise in your book was to show how to convert between the 3 forms of loops - it is just that they provide bad examples.
Topic archived. No new replies allowed.