+ 1

Exam question

İnt main () {int a=1 , b=2 , x; while (a + b == 3) { do { b++ ; x = a + b ; } while (b == 3 ) ; } printf("%d\n , x) ; } Answer is 5, can you explain this?

15th Apr 2019, 12:07 PM
Osman Uçkan
Osman Uçkan - avatar
1 Odpowiedź
+ 2
Pythontutor.com has a nice visualization tool (also for C). Paste the code there and read those comments while you do that: In the code there is a do/while loop nested inside a while loop. The while loop executes once because a+b == 3 a the start. The do/while executes once for the "do" and once because b == 3 after that. On pythontutor you can nicely see hwo that brings x to 5.
15th Apr 2019, 1:00 PM
Thoq!
Thoq! - avatar