Tsallaka zuwa abun ciki
Justice Tech School

while and do while

while checks first, do while runs once before checking.

Example
public class Main {
  public static void main(String[] args) {
    int n = 3;
    while (n > 0) { System.out.print(n-- + " "); }
    do { System.out.print("once"); } while (false);
    System.out.println();
  }
}

Result

3 2 1 once

Exercise

Run once first.

 { } while (c);

Quiz Test

Which runs the body first?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.