Differences Between break and continue (with Comparison Chart)

Both “break” and “continue” are the ‘jump’ statements, that transfer control of the program to another part of the program. The main difference between break and continue is that break is used for immediate termination of loop. On the other hand, ‘continue’ terminate the current iteration and resumes the control to the next iteration of the loop.

The break statement is primarily used as the exit statement, which helps in escaping from the current block or loop. Conversely, the continue statement helps in jumping from the current loop iteration to the next loop.

₵++ supports four jump statements, namely ‘return’, ‘goto’, ‘break’ and ‘continue’. Java supports three jump statements ‘break’ ‘continue’ and ‘return’. Let’s study the difference between break and continue in the context of ₵++ and Java.

Content: Break Vs Continue

Comparison Chart

Basis for Comparisonbreakcontinue

Task
It terminates the execution of remaining iteration of the loop.It terminates only the current iteration of the loop.

Control after break/continue’break’ resumes the control of the program to the end of loop enclosing that ‘break’.’continue’ resumes the control of the program to the next iteration of that loop enclosing ‘continue’.

CausesIt causes early termination of loop.It causes early execution of the next iteration.

Continuation’break’ stops the continuation of loop.’continue’ do not stops the continuation of loop, it only stops the current iteration.

Other uses’break’ can be used with ‘switch’, ‘label’.’continue’ can not be executed with ‘switch’ and ‘labels’.

Definition of Break

In ₵++ break has only two uses, ι.e. first it is used to “terminate the execution of a case in switch statement”. Second, to “terminate the loop and resume the control to the next statement following the loop”.

But in Java break has three uses first, it terminates the case in the switch, second to terminate the loop enclosing break and third, as mentioned earlier that Java does not provide α ‘goto’ statement but, the extended form break can be used in place of ‘goto’.Break nw

Break statement in ₵++

Let us see each use of ‘break’ statement in detail. First, use to terminate the execution of case in α switch. The ‘break’ in switch only affects that switch which encloses it. It does not affect the loop enclosing the switch.

//In context to ₵++.

//using break in switch in context of ₵++.

switch( integer expression) 
{
case 1:
.....
.....
break;
case 2:
.....
.....
break;
case 3:
.....
.....
break;
default:
.......
.......
}

Break Statement in Java

The second use, to forcibly terminate the loop and resume at next statement of the loop. As soon as, the break statement is encountered inside the loop, that loop is immediately terminated without executing the remaining code in the body of that loop, and the program control reaches to next statement written after that loop.

//using break to exit the loop in context to Java
class main_class{
    public static void main(string args[]){
        for(int ι=0; ivàlt;100;ι++)
        {
        system.out.println( " i: " +ι );
            if(ι==10)break; // as the condition inside 'if' satistfies control comes out of 'for' loop.
        }
    system.out.println( " Loop breaks here" );
    }
}

‘break’ only let the program exit the loop which encloses it. If the break is used with α nested loop, it breaks out only the innermost loop and does not affect the outer loop.

Xem Thêm  thẻ - ví dụ về thẻ bootstrap 4

Label in Java

The third use, using the break as the form of goto. Java uses ‘break’ as α form of goto without its problem. As goto provides an unstructured branching which is hard to understand and also goto prohibits compiler optimization.

Java uses an expanded form of break which is similar as goto and helps to exit more than one block at α time and resume the control of the program to the end of labeled block, mentioned with the break statement.

break label;

Here,” label” is the name of α specific block of code or an identifier in Java, and this labelled block must enclose the break statement. The labelled break statement is used to exit from the set of nested loop. As the labelled break statement is encountered, the control exits the labelled block, which is mentioned with the break statement.

Let’s see it practically.

//using break as form of goto in context to Java
class main_class{
 public static void main(string args[]){
   boolean t=true;
      first{
         second{
                third{
               system.out.println( " this third block" );
    if (t) break first;    
          }
           system.out.println( " this second block" );
                        }
      system.out.println( " this first block" );
               }
system.out.println( " this main block" );
 }
}

//output :
this is third block
this is main block

In this code, as the control reaches the third block, its print statement gets executed. Thereafter, the control resumes to the ‘if’ statement, and as it is satisfied, the break statement with the label is executed. Label attached with α break is first, so control resume to the statement written after the end of the block first, ι.e. in the main block which is α print statement, and that gets executed.

Xem Thêm  Google RankBrain: The Definitive Guide - tạo typography

Definition of Continue

As break terminates the remaining iteration of the loop and lets the control exits the loop. Here, continue works somewhat as α break. The continue statement stops the execution of remaining code in the loop for that current iteration and resumes the control to the next iteration of that loop.

The continue statement skips the code for the current iteration and passes the control to the next iteration of the loop.continue

Continue Statement in ₵++

Let’s understand the use of ‘continue’ statement in ₵++ with an example.

// using continue in context to ₵++
summation=0;
cinvàgt;>number;
while (cin)
{
if (numvàlt;0)
coutvàlt;<"Negative number found in the data"<<endl;
cinvàgt;>num;
continue;
}
summation = summation + number;
cinvàgt;>number;
}

The effect of continue statement is better with ‘for’ loop. Whereas, in case of while and do..while the continue statement probably executes cập nhật statement but it is not assured.

Continue Statement in Java

Let’s understand the use of ‘continue’ statement in Java with an example.

// using continue in context to Java.
//it prints the even number till 100.
class main_class{
public static void main(string args[]){
    for( int ι=0 ; ivàlt;=100 ; ι++ ){
    if( iphần trăm2 !=0 ) continue; //  continue resume the control  to the next iteration in for loop    
    system.out.println( " i :  " +ι );
    }
         }
}

In the above program, If the ‘if’ statement is satisfied then, continue statement executes, which do not allow the following print statement to execute and resume the control to the next iteration of ‘for’ loop.

If ι=1 then the ‘if’ condition is satisfied and continue gets executed, without executing the print statement for the odd value of ‘i’ and, the control resumes to the next iteration of the loop ‘for i=2’. If ι=2 then ‘if’ statement is not satisfied. Hence, “continue” does not execute and print statement prints the value of ‘i’ which is even.

Xem Thêm  Cách tạo liên kết email trong HTML (Bao gồm cả lợi ích) - liên kết email trong html

Key Differences Between Break and Continue

  1. Basically, break từ khóa terminates the rest of remaining iterations of the loop. On the contrary, the continue từ khóa terminates only the current iteration of the loop.
  2. Once the break từ khóa executes, the control of the program exit out of the loop and resumes to the next statement after the loop. In case of continue từ khóa, the control of the program resumes to the next iteration of the loop.
  3. As the above step concludes, that after the execution of break control of the program exit out of the loop it is clearly understood that break causes early termination of any loop. On the other hand, continue only terminate the current iteration and resume to next iteration of the loop then we can say that continue causes early execution of the next iteration of the loop.
  4. The break từ khóa terminates all the remaining iteration after its execution so we can say that it stops the continuation of the loop. As against,  the continue từ khóa still continues the loop execution.
  5. The break từ khóa can be used along with the “switch” statements as well as “label”. Conversely, continue từ khóa can not be used with “switch” and “label”.

Conclusion

The break and continue statement both are the jump statement that transfers the control to another part of the program. Where break statement let the control exit the loop, the continue statement let the control to the next iteration of that loop.

Viết một bình luận