you can debug your program to trace a
and b
variables and get what happens exactly at each iteration :
a=1 ; b=1;
when the logical operator is ||
if compiler found first operand false
he will go to the next operand to check it so the ++a
and ++b
will be both executed in first iteration so a=2; b=2;
now next iteration compiler find first operand a=3
is true
and it will not go to the next operand to check it so value of b
will not increase by 1
and stay b=2
a=3; b=2;
next iteraion also in the same way until finish of loop so you will get output:
a=10
b=2
check this program to understand how ||
operator work :
int a = 1;
int b = 1;
int d = 1;
for (int c = 0; c < 5; c++) {
if ((++a > 2) || (++b > 2) || (++d > 2)) {
a++;
}
}
when c=1
variables will be:
a =2
b =2
d =2
next iteration c=2
variables will be:
a=4
b=2
d=2
in this iteration compiler will only check first operand a=3
which is true
and it will not check others operands so its values will stay the same b=2
and d=2
next iteration c=3
variables will be:
a=6
b=2
d=2
and so on until finsh of loop output :
a=10
b=2
d=2