Digits, reversal and sum
Reversed is 1209, not 01209: an int has no leading zeros.
A while loop repeats a block as long as a condition stays true, checking the condition before each pass, so it may run zero times. A do-while checks after each pass, so its body always runs at least once.
Change the code and press Run (Ctrl+Enter). Try to predict the output first, then break it on purpose and read the error. Your edits are saved and match the lesson page.
Practice questions
Write the code in the editor, run it, then open the model answer to compare.
Using a while loop, print the powers of 2 that are less than 100, on one line separated by spaces.
Using a while loop, check whether 12321 is a palindrome number (reads the same backwards).
Using a do-while loop, print the numbers from 5 down to 1, then Lift off!.
Explain it without notes
What is the difference between while and do-while? Give a situation suited to each.
What three things does a counting while loop need, and what goes wrong if each is missing?
Why is code after while (true) { ... } with no break a compile error?
When would you choose a while loop over a for loop?
Expected output
Number: 90210
Digits: 5
Sum of digits: 12
Reversed: 1209