for loop question

Hello I am reading a piece of code, and it contains a for loop that is not what I am used to seeing (that basic for loop format) and I am hoping someone could help me understand it. The code is below. Is the string temp the initalisation part of the for loop, and the cin >> temp the incrementation part? or would the be the conditional part of a for loop?

I have pasted all the code under the for loop just incase that is needed to be seen.

 
   for (string temp; cin>>temp; ) 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int main() 
{
    vector<string> dislikedVector = {"Broccoli", "Puree", "Cauliflower", "Cabbage"};
    bool disliked = false;
    for (string temp; cin>>temp; ) // read
    {
        for (auto word : dislikedVector)
        {
            if (word == temp)
            {
                disliked = true;
                break;
            }
        }
        if (disliked)
            cout << "BLEEP" << '\n';
        else
            cout << temp << '\n';

        disliked = false;
    }

    return 0;
}

> for (string temp; cin>>temp; )

string temp initialisation statement (a simple declaration here)

cin>>temp condition (expression which is contextually convertible to bool)
evaluated before each iteration; if the result is false ( ie. if cin>>temp failed ), exit the loop

an empty iteration expression (a null statement)
Expressed with a while loop it'd look like this:
1
2
3
4
5
    string temp;
    while(cin>>temp) // read
    {
...
    }
Topic archived. No new replies allowed.