Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
17 views

Key Difference Break and Continue N Java

Uploaded by

Roo Anu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Key Difference Break and Continue N Java

Uploaded by

Roo Anu
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Switch in Java

Break and continue


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