written 5.7 years ago by | • modified 3.0 years ago |
0
1.3kviews
Explain the following statement with example
ADD COMMENT
EDIT
1 Answer
0
12views
written 3.0 years ago by | • modified 2.9 years ago |
Explain the following statement with example
i) continue
a) It terminate only the current iteration of the loop.
b) It will bring the next iteration early.
c) It will not stop the execution of the loop.
d) We cannot use it with a switch statement.
For eg:-
Code:-
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
// If the number is 2
// skip and continue
if (i == 2)
continue;
System.out.print(i + " ");
}
}
}
Output:- 0 1 3 4 5 6 7 8 9
Here as the if condition checks if the value of i==2 then it skips the loop and continues with next iteration.
ii) break
a) It terminates the entire loop immediately.
b) It will terminate the entire loop early.
c) It will stop the execution of the loop.
d) We can use it with a switch statement.
For eg:-
Code:-
public class Main {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
// If the number is 2
// break and exit
if (i == 2)
break;
System.out.print(i + " ");
}
}
}
Output:- 0 1
Here as the if condition checks if the value of i==2 then it ends the entire loop immediately.
ADD COMMENT
EDIT
Please log in to add an answer.
@moderators delete this question. Statement is missing