-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBreakContinueWhileLoop.java
More file actions
29 lines (25 loc) · 910 Bytes
/
BreakContinueWhileLoop.java
File metadata and controls
29 lines (25 loc) · 910 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.Scanner;
/**
* 18. While loop using break and continue program in java
*/
public class BreakContinueWhileLoop {
public static void main(String[] args) {
try (Scanner scan = new Scanner(System.in)) {
while (true) {
System.out.println("Enter a positive number:");
if (!scan.hasNextInt()) {
System.out.println("Invalid input! Please enter a valid positive integer.");
scan.next();
continue;
}
int num = scan.nextInt();
if (num == 0 || num == 999) {
System.out.println("Entered number matched!");
break;
}
}
} catch (Exception e) {
System.out.println("Unexpected error! Please try again.");
}
}
}