user7423098
user7423098

Reputation: 321

Using cin in for loop defiition

I am creating a simple program with std::cin in the for loop body.

#include <iostream>

int main()
{
    float a = 0;
    for (std::cin >> a; a >= 0;std::cin >> a) {
        std::cout << "\t "<<a<<"\n";
    }
    return 0;
}

This program takes value in a and prints it until you enter a negative number. But when I modify for loop definition as for(std::cin >> a; a >= 0;) it just prints first number forever. I am wondering why this is happening. Is it because loop increment part missing?

Upvotes: 0

Views: 12588

Answers (4)

Vlad from Moscow
Vlad from Moscow

Reputation: 310950

In this for loop

for(std::cin >> a; a >= 0;) 

the first expression of the statement std::cin >> a is evaluated only once before the first iteration of the loop. So the variable a will not be changed any more within the loop and the loop will be infinite.

Take into account that this loop

for (std::cin >> a; a >= 0;std::cin >> a) {
    std::cout << "\t "<<a<<"\n";
}

is also wrong because the user can interrupt the input. In this case the variable a will not be changed and you will again get an infinite loop.

Instead of these statements

float a = 0;
for (std::cin >> a; a >= 0;std::cin >> a) {
    std::cout << "\t "<<a<<"\n";
}

it will be better to write

for ( float a; std::cin >> a && a >= 0; ) {
    std::cout << "\t "<<a<<"\n";
}

There is no need to declare the variable a outside the loop because it is used only within the loop.

Upvotes: 1

volvox
volvox

Reputation: 91

for is not proper for these situations. Use while instead of for.

A for loop is generally used when you know how many iterations of the loop you want to execute - or it is calculable. A while loop, or do...while loop is more appropriate here than a for loop because you do not know how many times the user wants to execute the loop.

Here are some references that can give you more information on the differences between the types of loop structures in C++.

https://www.programiz.com/cpp-programming/do-while-loop http://www.cprogramming.com/tutorial/lesson3.html

#include <iostream>

int main()
{
    float a = 0;
    while(a>=0){
        std::cout << "value: ";
        std::cin >> a;
    }

    return 0;
}

Better way is below.

#include <iostream>

int main()
{
    float a;
    do{
        std::cout << "value: ";
        std::cin >> a;
    }
    while(a>=0);

    return 0;
}

Upvotes: 1

Stephan Lechner
Stephan Lechner

Reputation: 35154

I think there is nothing to wonder about; omitting the "third" part of the for-loop means that there is - apart from the loop's body - nothing that will be executed after each iteration; hence, once the loop is entered by having provided a value for a >= 0, and as long as you do not alter the value of a in the loop's body (and std::cout << "\t "<<a<<"\n" will not alter a), the loop will run "for ever", always printing a's initial value.

Upvotes: 4

Moshe D
Moshe D

Reputation: 778

You are right!

When a loop is called first is this code execute

int i = 0

after that it checks the test

i >= 0

and for every iteration it checks the test and then to the incresment part

for (int i = 0; i >= 0;std::cin >> a) {
   //code
}

and since you left empty the incresment part, the a is printed forever!

Upvotes: 0

Related Questions