0% found this document useful (0 votes)
24 views

Key Difference Break and Continue N Java-2

Uploaded by

Roo Anu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Key Difference Break and Continue N Java-2

Uploaded by

Roo Anu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Switch

The switch statement is java’s multiway branch statement. It provides an easy way to dispatch execution
to different parts of your code based on the value of an expression.
Example 1

package kiruba1;

public class Switchfirst {

public static void main(String[] args) {

char grade = 'F';

switch(grade) {

case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}

Output
Better try again
Your grade is F

Example 2

package kiruba1;

public class Switchsecond {

public static void main(String[] args) {

for (int i=0;i<6;i++)


switch(i) {

case 0:
System.out.println("i is Zero.");
break;
case 1:
System.out.println("i is one.");
break;
case 2:
System.out.println("i is Two.");
break;
case 3:
System.out.println("i is three.");
break;
default:
System.out.println("i is greater than three.");
}

Output

i is Zero.
i is one.
i is Two.
i is three.
i is greater than three.
i is greater than three.

Example 3

package kiruba1;

//In a switch, break statements are optional.


public class Switchthree {

public static void main(String[] args) {

for (int i=0; i<12; i++)


switch (i) {

case 0:
case 1:
case 2:
case 3:
case 4:
System.out.println( "i is less than 5");
break;
case 5:
case 6:
case 7:
case 8:
case 9:
System.out.println( "i is less than 10");
break;

default:
System.out.println( "i is 10 or more");

}
}

Output

i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 5
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is less than 10
i is 10 or more
i is 10 or more

Example 4

package kiruba1;

public class Switchfour {

public static void main(String[] args) {

int month =4;

String season;

switch(month) {
case 12:
case 1:
case 2:
season = "Winter";
break;
case 3:
case 4:
case 5:
season = "Spring";
break;
case 6:
case 7:
case 8:
season = "Summer";
break;
case 9:
case 10:
case 11:
season = "Autum";
break;
default:
season = "Bogus Month";
}

System.out.println("April is in the " + season + ".");


}
}

Output

April is in the Spring.

Jump statements
Java support three jump statements break, continue and return. These statements transfer
control to another part of your program.

Break
Example 1

package kiruba1;
// Using break to exit a loop
public class breakone {

public static void main(String[] args) {

for (int i=0; i<100;i++) {


if (i==10) break; // terminate loop if i is 10
System.out.println("i: " + i);

System.out.println ("loop complete ");


}

}
// Although the for loop is designed to run from 0 to 99, the break statement
causes it to terminate early, when i equals 10

Output

i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
i: 8
i: 9
loop complete

Example 2

package kiruba1;

// Using break to exit a while loop

public class breaktwo {

public static void main(String[] args) {

int i = 0;

while (i< 100) {


if (i==8) break; //teerminate loop if i is 8
System.out.println("i: " +i);
i++;

System.out.println("loop complete");

Output
i: 0
i: 1
i: 2
i: 3
i: 4
i: 5
i: 6
i: 7
loop complete

Example 3
package kiruba1;

//break statement will only break out of the innermost loop in inside a set
of nested loops
public class breakthree {

public static void main(String[] args) {

for (int i=0; i<3; i++) {

System.out.print("Pass " +i+ ": " );

for (int j=0; j<100; j++){

if(j ==5) break;//terminate the loop if j is 5


System.out.print( j + " ");
}
System.out.println();

}
System.out.println("Loops complete.");
}

Output

Pass 0: 0 1 2 3 4
Pass 1: 0 1 2 3 4
Pass 2: 0 1 2 3 4
Loops complete.

Example 4
package kiruba1;
// Using break to exit from nested loops
public class breakfour {

public static void main(String[] args) {

outer:for (int i=0; i<3; i++) {

System.out.print("Pass " +i+ ": " );


for (int j=0; j<100; j++){

if(j==5) break outer; // exit booth loops

System.out.print( j + " ");


}
System.out.println(" This will not print");

}
System.out.println("Loops complete.");
}

Output
Pass 0: 0 1 2 3 4 Loops complete.
Continue
Example 1

package kiruba1;
// continue
public class continueone {

public static void main(String[] args) {


for (int i=0;i<10;i++) {

if (i%2 ==0) continue; // code uses the % operator to check if i


is even
System.out.print(i+ " ");
// System.out.println(" "); }

Output
1 3 5 7 9

Example 2

package kiruba1;

public class continuetwo {

public static void main(String[] args) {

outer: for (int i=0; i<10; i++) {


for (int j=0; j<10; j++) {
if (j>i) {
System.out.println();
continue outer;
}
System.out.print(" "+(i* j) );
}
}

System.out.println();
}

}
Output

0
0 1
0 2 4
0 3 6 9
0 4 8 12 16
0 5 10 15 20 25
0 6 12 18 24 30 36
0 7 14 21 28 35 42 49
0 8 16 24 32 40 48 56 64
0 9 18 27 36 45 54 63 72 81

Return
Example
package kiruba1;

public class returnone {

public static void main(String[] args) {

boolean t = true;
System.out.println("Before the return.");
if (t) return; // return to caller
//if (!t) return; // return to caller
System.out.println ("This won't execute.");

Output
Before the return.

{If (not t)

Output

Before the return.


This won't execute.}

Key Difference – break vs continue in Java


In programming, sometimes it is required to repeat a statement or a set of statements multiple
times. Loops are used to iterate a number of times the same set of instructions. Some examples of
loops are the while loop, do while loop and for loop. In the while loop, the test expression is
evaluated first. If it is true, the statements inside the while loop execute. In the end, the test
expression is evaluated again. If it is true, the statements will execute again. When the test
expression becomes false, the loop terminates. The do while loop is similar to the while loop. But
the statements execute once before the test expression is checked. The for loop is used when the
number of iterations is known at the beginning. The initialization happens first. Then the test
expression is checked. If it is true, the loop executes. Then the update expression is evaluated.
Again, the test expression is checked. If it is true, the loop executes. This process repeats until the
test expression becomes false. It is sometimes required to skip some statements inside the loop or
to terminate the loop immediately without checking the test expression. The break and continue
statements can be used to achieve this task. The break is used to terminate the loop immediately
and to pass the program control to the next statement after the loop. The continue is used to skip
the current iteration of the loop. That is the key difference between break and continue in Java.

What is break in Java?


The break is used to terminate from the loop immediately. When there is a break statement,
the control is passed to the statement after the loop. The ‘break’ keyword is used to indicate
the break statement. Even though the program is executing the loop, if a break occurs, the
execution of the loop terminates. Therefore, if the programmer wants to stop execution
when a specific condition is met, then he can use the break statement.

According to the above program, the for loop iterates from 1 to 10. When the i value
becomes 6, the test expression becomes true. So, the break statement executes, and the
loop terminates. So, the value after 6 will not print. Only the value from 1 to 5 prints.

What is continue in Java?


The continue is used to used to skip the current iteration of the loop. The keyword ‘continue’
is used to indicate the continue statement. When continue executes, the control of the
program reaches the end of the loop. Then the test expression is checked. In a for loop, the
update statement is checked before the test expression is evaluated.
According to the above program, the for loop iterates from 1 to 10. When i is 1, the
remainder after dividing by two is 1. So, the if condition is true. Therefore, the continue
statement executes and the iteration skips to the next. Then i comes 2. When dividing 2 by
2, the remainder is 0. The condition is false. So, continue does not execute. Therefore, the
value 2 gets printed. In the next iteration, i is 3. When dividing it by 2, the remainder is 1.
The condition is true. So, continue executes and the iteration jump to the next and i
becomes 4. This process repeats until i becomes 10. If the remainder is one, the iteration
skips to the next because of the continue statement. Only the even numbers get printed.

What is the Similarity Between break and continue in


Java?
Both break and continue in Java are used to change the execution of the loop.

What is the Difference Between break and continue in


Java?
break vs continue in Java
The break is a loop control structure that causes the The continue is a loop control structure that
loop to terminate and pass the program control to the causes the loop to jump to the next iteration
next statement flowing the loop. of the loop immediately.

Main Purpose

The continue is used to skip statements


The break is used to terminate the loop.
inside the loop.

Summary – break vs continue in Java


In programming, it is required to repeat a statement of a group of statements multiple times.
The loops are used for that tasks. Sometimes it is required to skip some statements inside
the loop or to terminate the loop immediately. The break and continue can be used to
achieve that task. The break is used to terminate the loop immediately and to pass the
program control to the next statement after the loop. The continue is used to skip the
current iteration of the loop. That is the difference between break and continue in Java.

You might also like