[Solved] Can I use the result of a comparison as an integer in C++?


Yes, C++ has implicit casting from bool to int.

However, I would suggest you make this a bit more clear that this is your actual intention, so that future readers understand your intent. Explicitly cast it to an int first.

int var = 0;
for (i = 0; i < 20; i++) {
    var += (int)(var < 10);
}

3

solved Can I use the result of a comparison as an integer in C++?