Search code examples
c++while-loopcomma-operator

How does this C++ while loop work?


I learned the basics of C++ few months ago. Recently I found a while loop that looks like this. I want to understand how it works.

while(cin>>n>>m,n||m)
{
    does something;
}

Solution

  • operator , (operator comma) executes all instructions in list and returns value of last expression, so cin>>n>>m,n||m is equal to

    cin >> n >> m;
    n || m;
    

    And whole loop will work like this one:

    int n, m;
    cin >> n >> m;
    while(n || m){
        //some action
        cin >> n >> m;
    }