Code after if block inside a for loop doesn’t work

Your code add one to the number passed as digit array.

So when you add 1 to 123, you get 124. The code starts with the last digit, looks whether it is less than 9, then add 1 only to the last digit.
this happens in the if-block. The return ends the function

The code which sets a digit to 0 will only reached when there is some overflow. This overflow can only happen when you pass a number where the last digit is 9.

To reach this case you must pass something like 129 (or {1,2,9}). Then the last digit become 0 and the second last digit is checked. In this case added by one, return 130

To reach the code behind the loop, you have to pass a list where all digits are set to 9. For example 99 (or {9,9}).

In this case, the last digit will set to 0, the first digit will set to 0, then a new list will be generates with one more digit. Initially all digits are set to 0. Then the first digit will be set to 1. This results in 100.

return leaves the function/method
break leaves the surrounding loop (for,while)

So the answer to your question in bold is YES

Leave a Comment