Hello everyone. I need your help. How is it possible that output of this code is 2. | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
0

Hello everyone. I need your help. How is it possible that output of this code is 2.

https://code.sololearn.com/cJ3oAtW49eGH/?ref=app

9th Nov 2020, 11:22 AM
Phidan Makhmudova
3 Antworten
+ 3
What would you have expected instead? In the first iteration, i = 1, so neither i < 1 nor i > 1 are true. Therefore, the condition false || false is false and the loop goes on. During the second iteration however, i = 2, so i > 1 is now true. Since the logical OR is true if at least one operand is true, the condition is true as well and we print the value of i, which is 2.
9th Nov 2020, 11:27 AM
Shadow
Shadow - avatar
+ 1
because the check is, "if i is less than 1 or greater than 1...". The first iteration, i is equal to 1 so nothing happens. Then i is incremented by 1. Then it's checked again, since i is now equal to 2, the check triggers and prints i's value, wich is 2.
9th Nov 2020, 11:27 AM
Slick
Slick - avatar
+ 1
Thank you guys for your help. I was considering i<=1 instead of i>=1 in the loop. The real crackhead is here😆
9th Nov 2020, 11:53 AM
Phidan Makhmudova