+ 2

Explain the logic...🙏

#include <iostream> using namespace std; int main() { int i=1; if(i++ && (i==1)) cout << i << " YES" << endl; else cout << i << " NO" << endl; return 0; } Can anyone give me the answer. Why this code give output "2 NO" ... How is it possible when we know that the priority of == is greater than ++?

8th Feb 2021, 4:16 AM
Janvi Desai
1 Resposta
+ 7
priority only apply inside a single expression, while the 'if' test for 2 expressions (concatenated by logic &&): first i is evaluated to 1, then i is incremented (i==2), then as 1 is true, the second expression i==1 is evaluated to false, as now i==2!=1...
8th Feb 2021, 4:21 AM
visph
visph - avatar