Skip to content

The while loop

Watch it run · 1 of 3

Counting digits of 345

1public class Main {
2 public static void main(String[] args) {
3 int n = 345;
4 int digits = 0;
5 while (n > 0) {
6 n /= 10;
7 digits++;
8 }
9 System.out.println(digits);
10 }
11}

Memory

int n

345

Output

(nothing yet)

line 3Start with 345.

step 1 of 8