As written, the code doesn't compile, since operator precedence means it's i = ((4 || i) = 5)
or something, and you can't assign to a temporary value like (4 || i)
.
If the operations are supposed to be assignment =
rather than comparison ==
for some reason, and the assignment expressions are supposed to be the operands of ||
, then you'd need parentheses
(i = 4) || (i = 5)
As you say, the result of i=4
is 4 (or, more exactly, an lvalue referring to i
, which now has the value 4). That's used in a boolean context, so it's converted to bool
by comparing it with zero: zero would become false
, and any other value becomes true
.
Since the first operand of ||
is true, the second isn't evaluated, and the overall result is true. So i
is left with the value 4, then the function returns. The program won't print anything, whatever values you pass to the function.
It would make rather more sense using comparison operations
i == 4 || i == 5
meaning the function would only print something when the argument is neither 4 nor 5; so it would just print once in your example, for f(3)
.
Note that <iostream.h>
hasn't been a standard header for decades. You're being taught an obsolete version of the language, using some extremely dubious code. You should get yourself a good book and stop wasting time on this course.