Is it valid to use an integer variable as a condition?
I mean:

int var = 20;
if (var) {
    //code here
}

I know that when var == 0 it's equivalent to false and true if 1, but otherwise could it be a valid question?

Is it valid to use an integer variable as a condition?

Yes. This:

if (var)

Is logically synonymous with this:

if (var != 0)

Provided var has an implicit conversion to an integer or boolean type, it's perfectly valid and makes for nice shorthand in some cases. In this case, 0 is false and any non-zero value is true.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.