0
Confused Logical operator "OR"
Why Code not work int population, area; cin >> population >> area; if(population < 10000 || area < 10000) { cout << "Small country"; } else { cout << "Not small country"; } } if i input 15000 output result "small country" if i change code "||" to "&&" code work why use "OR" not work, i input greater than 10000 output Small country not "Not small country? can you give me an explanation?
2 ответов
+ 4
your program expects two inputs.
in Sololearn you input both, then click the submit button.
15000
15000
submit
if you only input one value, then your area is some garbage value, which will affect the result if you use &&
did you edit your question?
I thought you used | instead of || ,
so I replied with:
| and & are bitwise operators. they are use for computing values.
|| and && are logical operators. they are used for evaluating boolean expressions.
+ 2
I don't fully understand your question, but I run your code with input 15000 and inputs 15000 and 15000 (in two lines). In both cases, it prints "Not small country".
I guess you are confuse why it prints "Not small country", with input or inputs of 15000.
If we separate two comparisons, with the values of 15000:
population < 10000
area < 10000
Both evaluate to False, which turns out to be 0.
When we combine it, it becomes 0 || 0, which is False OR False, ultimately returns False, so it prints "Not small country".
If the input is 100 for example, if(population < 10000 || area < 10000) evaluates to True, then prints "Small country".
https://sololearn.com/compiler-playground/csRmv53dpU9P/?ref=app