break and continue statements in Java
In Java, we use break and continue statements frequently. We use in conditions where we want to get out of the loops. Normally we have to wait for the condition for getting an exit from the loops, this is performance hit as we are done with our task and still loop is getting executed. Keyword break is used to get such functionality. Whenever program execute a break statement in a loop, the control of execution passes to the next statement right after the currently executing loop. Whereas, in some cases we may want to take control to the beginning of the loop, bypassing the loop statements which have not yet been executed. This can be done using the continue statement.
A Simple Program demonstrating use of break and continue statements in Java
0 8 16 24 32 40 48 56 64 72
In Java, we use break and continue statements frequently. We use in conditions where we want to get out of the loops. Normally we have to wait for the condition for getting an exit from the loops, this is performance hit as we are done with our task and still loop is getting executed. Keyword break is used to get such functionality. Whenever program execute a break statement in a loop, the control of execution passes to the next statement right after the currently executing loop. Whereas, in some cases we may want to take control to the beginning of the loop, bypassing the loop statements which have not yet been executed. This can be done using the continue statement.
A Simple Program demonstrating use of break and continue statements in Java
package com.hubberspot.control.example; public class BreakAndContinueDemo { public static void main(String[] args) { for(int i = 0; i < 100; i++) { if(i == 80) break; if(i % 8 != 0) continue; System.out.print(i + " "); } } }Output of the program :
0 8 16 24 32 40 48 56 64 72