-
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBreakContinue.java
More file actions
executable file
·43 lines (37 loc) · 1.52 KB
/
BreakContinue.java
File metadata and controls
executable file
·43 lines (37 loc) · 1.52 KB
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// BREAK KEYWORD
//1. break Keyword:
// The break keyword is used to terminate the loop prematurely.
// When encountered, it immediately exits the loop, and the control flow
// moves to the statement immediately following the loop.
public class BreakContinue {
public static void main(String[] args){
System.out.println("THE Break Keyword");
for (int number = 1; number <= 10; number++) {
if (number == 3) {
break; // exit the loop when number equals 3
}
System.out.println("Count is: " + number);
}
// 2. CONTINUE KEYWORD:
//
// The continue keyword is used to skip the remaining code inside the
// loop for the current iteration and proceed with the next iteration.
//
System.out.println("");
System.out.println("THE CONTINUE KEYWORD");
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // skip printing when i equals 3
}
System.out.println("Count is: " + i);
}
//Output:
//Count is: 1
//Count is: 2
//Count is: 4
//Count is: 5
//Key Points:
// break is used to completely exit the loop, while continue is used to skip the remaining code for the current iteration and move to the next iteration.
// Both keywords can be used in for, while, and do-while loops to control the loop's behavior based on certain conditions.
}
}