0

how does this code work?

there seems to be no special condition inside the loop(not explicitly), so how and why does it end when the value of it becomes 0? Is an integer variable containing 0 treated as a False boolean value(or implicitly converted to it) and then compared with True, when used alone in a condition? https://code.sololearn.com/cfIwhO3k506V/?ref=app

26th Dec 2020, 5:18 AM
salar vahidi
salar vahidi - avatar
2 Answers
+ 5
For example: INPUT: 100 while(100) ---> Any value except 0 is True - - - - - LOOPS- - - - - First loop: while ( 100 ) 100 / 10 ---> m = 10 cnt++ ---> cnt = 1 Second loop: while ( 10 ) 10 / 10 ---> m = 1 cnt++ ---> cnt = 2 Third loop: while ( 1 ) 1 / 10 ---> m = 0 cnt++ ---> cnt = 3 Fourth loop: while ( 0 ) //never executed because False - - - - - - - - - - - - - - - - - On the Third loop, the value of m became 0 so their will be no Fourth loop therefore it will then print the value of cnt. Output: 3 - - - - - - - - - - - - - - - - - The output is three and so it means the number has three digits because the loop runs 3 times and thus the cnt++ also run 3 times. Then the final value of cnt became 3 and it indicates the number of digits. I hope this hepls, if it is still not clear to you, feel free to ask. Happy Coding!
26th Dec 2020, 5:26 AM
noteve
noteve - avatar
+ 1
《 Nicko12 》 your answer was really helpful and thorough. thank you 😊
26th Dec 2020, 5:45 AM
salar vahidi
salar vahidi - avatar