X

Play AlgoQuiz

Rate Us :



Share with Friends :

Menu

Transfer statements :

break keyword :-

The break keyword is used to stop the entire loop.

The break keyword will stop the execution of the innermost loop and start executing the next line of code after the block.

We can use break keyword in the following cases :

  • within the switch to stop fall through.

  • inside loop to break the loop execution based on some condition.

  • inside labelled block to break that block execution based on some condition.

Syntax: The syntax of a break is a single statement inside any loop : break;

Example 1:

class Example { public static void main(String [ ] args) { int a = 1; switch(a) { case 1 : System.out.println("Algo"); break; case 2 : System.out.println("Valley"); break; default : System.out.println("Java"); } } }

Output:

Here, output would be - Algo

Example 2:

class Example { public static void main(String [ ] args) { for(int i = 0 ; i < 5 ; i++) { if(i == 3) break; System.out.println(i); } } }

Output:

Here, output would be - 0 1 2

Example 3:

class Example { public static void main(String [ ] args) { int i = 5; label : { System.out.println("Algo"); if(i == 5) break label; System.out.println("Valley"); } System.out.println("Java"); } }

Output:

Here, output would be - Algo Java

Example 4:

public class Example { public static void main(String [ ] args) { int[ ] numbers = {10,20,30,40,50}; for(int x : numbers ) { if( x == 30) { break; } System.out.print( x ); System.out.print("\n"); } } }

Output:

Here, output would be -
10 20

If we are using break keyword in any other place then we will get compile time error.

Example:

class Example { public static void main(String [ ] args) { int i = 5; if(i == 5) break; System.out.println("Algo"); } }

Output:

Compile time error : break outside switch or loop

continue keyword :-

The continue keyword can be used in any of the loop control structures. It causes the loop to immediately jump to the next iteration of the loop. In a for loop, the continue keyword causes flow of control to immediately jump to the update statement in for loop. In a while loop or do...while loop, flow of control immediately jumps to the boolean expression.

X

iteration :

It refers to the repetition of a process.

Syntax: The syntax of a continue is a single statement inside any loop : continue;

Example 1:

class Example { public static void main(String [ ] args) { int[ ] numbers = {10,20,30,40,50}; for(int x : numbers) { if( x == 30) { continue; } System.out.print( x ); System.out.print("\n"); } } }

Output:

Here, output would be -
10 20 40 50

Example 2:

class Example { public static void main(String [ ] args) { for(int i = 0 ; i < 5 ; i++) { if(i % 2 == 0) continue; System.out.println(i); } } }

Output:

Here, output would be -
1 3

If we are using continue outside of loop then we will get compile time error.

Example:

class Example { public static void main(String [ ] args) { int i = 5; if(i == 5) continue; System.out.println("Algo"); } }

Output:

Compile time error : continue outside of loop


X

MiniDoll :

MiniDoll is an application to learn with fun. You can play it to solve some tricky questions related to Java Programming. Either enjoy playing it alone or discuss with your friends to solve it. It is a simple approach to learn by solving questions. This application has different topics of Java and each topic is divided into three phases. Start solving from initial topic to final topic and by the end of every topic you can boost your concept of that topic.