question about for loops

Hi all!

I was just trying out some C++ practice exercises at <a href="http://en.wikibooks.org/wiki/C++_Programming/Exercises/Iterations">Wikibooks</a> and one of the solutions to the second exercise uses a for loop that I don't understand completely.

Specifically this part:
i+=(i == input)

What is this called?

It seems to be a conditional statement, but with no "if".

Is there a "longhand" method that is identical.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
using namespace std;

int main() {
    // Input and loop defined, only adds if input is correct.
    for (short int i=8, input; i <= 23; i+=(i == input) ) {
        cout << "Enter the number " << i << ": ";
        cin >> input;
    }

    return(0);
}

conditional operators evaluate to 'true' (1) or 'false' (0)

i += (i == input)
is the same as:

1
2
3
4
if(i == input)
  i += 1;
else
  i += 0;
Cool.

Thanks!

-Clay



Last edited on
Topic archived. No new replies allowed.