Print Numbers 1 to 3
This program prints numbers from 1 to 3 using a do-while loop.
Problem Statement
Write a Java program that prints numbers from 1 to 3 using a do-while loop.
Source Code
| 1 | public class PrintDoWhile { |
| 2 | public static void main(String[] args) { |
| 3 | int i = 1; |
| 4 | do { |
| 5 | System.out.println(i + " - Eduinq"); |
| 6 | i++; |
| 7 | } while(i <= 3); |
| 8 | } |
| 9 | } |
Program Output
1 - Eduinq 2 - Eduinq 3 - Eduinq
Explanation
The program initializes i=1. The do block executes first, printing i and incrementing it. Then the condition i<=3 is checked. This ensures the loop runs at least once, even if the condition is false initially. Shows how do-while loop guarantees at least one execution.