0
OR operator
How do these two programs really work? i know o/p. but don't know logic. 1. #include <stdio.h> int main() { // Write C code here int x=-2; while(++x || x==0){ printf("X"); } } 2. #include <stdio.h> int main() { // Write C code here int x=-2; while(x++ || x==0){ printf("X"); } }
1 Antwort
+ 1
Shubham Rampurkar When using OR operator if the 1st operand(++x) is true, no need to check 2nd (x==0). Loop become false only if both operand are false.
Case 1:
Here pre increment when x=-1
(++x||x==0) => (0 || 0==0) 1st operand(0) is false but 2nd condition(0==0) is true, so loop continus infinite times
Case 2:
Here post increment when x=0
(x++||x==0) => (0 || 1==0) both operand become false loop ends here.