continue – Programmerbay https://programmerbay.com A Tech Bay for Tech Savvy Wed, 17 Aug 2022 08:09:35 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.5 https://programmerbay.com/wp-content/uploads/2019/09/cropped-without-transparent-32x32.jpg continue – Programmerbay https://programmerbay.com 32 32 Difference Between Break and Continue in Java in Tabular Form https://programmerbay.com/difference-between-break-and-continue-java/ https://programmerbay.com/difference-between-break-and-continue-java/#respond Tue, 21 Jun 2022 17:28:38 +0000 https://www.programmerbay.com/?p=3042 Both Break and Continue are jump statements in Java. These statements shift control from one part to another part of a program.

The break statement is mainly used to terminate the currently executing loop or block in which it’s encountered and move to next immediate program statement. On other hand, Continue statement only terminates the current iteration of the loop and hops to next iteration, without processing statements that come after it within the loop body. Sometimes, it requires  If – condition to avoid Java’s unreachable statement error.

 

Difference between Break and Continue statement in Java in Tabular form

BasisBREAK STATEMENTCONTINUE STATEMENT
DefinitionIt is used to terminate the execution of the loop and shift the control over very next statement after the loop. It also terminates the sequence of cases of the switch statementIt is used to terminate the execution of the current iteration and jumps to the next iteration of the loop
ControlIt transfers control to the very first statement after the loopIt transfers control to the conditional expression of the loop. In other words, it goes back to the entry point of the loop and begins next iteration
TerminationIt causes termination of the loopIt causes termination of the current iteration of the loop, without processing other statements that come after it within the loop body
Used withIt can be used with loops, switch statements and labelsIt can only be used with loops
KeywordBreak keyword is used for break statement in JavaContinue keyword is used for continue statement in Java

Break Statement

Break statement is a jump statement that terminates a loop or a case sequence of switch statement in which it’s encountered.Therefore, it breaks the flow of a program.

It can be used to achieve the following functionality :
1) Escape from currently executing loop
2) Break the frequent execution of cases in Switch Statement
3) It can be used as Go-to Statement

Break Statement
Figure : Working of Break Statement in Loop

Java program to show working of break statement in a loop 

public class BreakWithLoop
{
  public static void main (String[]args)
  {
    for (int i = 1; i < 10; i++)
      {
  if (i == 5)
    {
      System.out.
        println
        ("Break Encountered ! \n ************* Terminating the loop ! ");
      break;
    }
  System.out.println ("Executing " + i + " times. ");
      }
  }
}

Output:

Executing 1 times. 
Executing 2 times. 
Executing 3 times. 
Executing 4 times.
 Break Encountered ! ************* Terminating the loop !

 

  • When break statement occurs in a loop, it forces immediate termination of that particular executing loop in which it’s encountered
  • It skips all the remaining iteration and immediately moves control to next statement in the program
  • In case of nested loop, if it is occurred in innermost loop, then it would be responsible for terminating inner loop only
  • It is used in such situations when number of iterations are uncertain and therefore, it is companion with if statement

Java program to show working of break statement in Switch statement 

public class BreakWithSwitch
{
  public static void main (String[]args)
  {
    switch (2)
      {
      case 1:
  System.out.println ("case 1 :");
  break;
  case 2:System.out.println ("case 2 :");
  break;
  case 3:System.out.println ("case 3 :");
  break;
  default:System.out.println ("nothing matched! :");



      }
  }

Output:

case 3

When break statement triggers in a switch case, it terminates that case body in order to prevent from execution of later cases. In Switch statement, usually each and every case ends with a break statement to stop subsequent execution of cases.

Java program to show working of break statement with label 

public class BreakWithLabel
{
  public static void main (String[]args)
  {
        dog :{
            
            cat :{
                
                rat :{
                    
                    System.out.println("Rat killed !");
                    break rat;
                }
                System.out.println("Cat killed !");
                break cat;
                
            }
                System.out.println("Dog killed !");
                break dog;
        }

      }
  }

Output:

Rat killed ! 
Cat killed ! 
Dog killed !

Java uses label and break statement in order to get some sort of functionality of go-to statement. In this, label works as a target point and break statement considers the label to jump out of that particular target point.

Continue Statement

It only terminates the current iteration of the loop and starts next iteration again, without processing statements that come after it within the enclosing loop body. It is used with looping statements only.

Continue Statement
Figure : Working of Continue Statement in Loop

 

Java program to show working of continue statement in a loop 

public class ContinueWithLoop
{
  public static void main (String[]args)
  {
    for (int i = 1; i < 10; i++)
      {
  if (i <= 5)
    {
      continue;
    }
  System.out.println ("Executing " + i + " times. ");
      }
  }
}

Output:

Executing 6 times. 
Executing 7 times. 
Executing 8 times. 
Executing 9 times.

When continue statement occurs in a loop, it forces immediate termination of the current iteration and jump back to beginning of the loop for next iteration. It skips only the current iteration and immediately moves control to next iteration in the loop.

It is important to have If – condition to avoid Java’s unreachable statement error.

]]>
https://programmerbay.com/difference-between-break-and-continue-java/feed/ 0
Difference Between Continue And Break Statement in C++ in Tabular Form https://programmerbay.com/break-and-continue-statement-in-c/ https://programmerbay.com/break-and-continue-statement-in-c/#respond Sun, 22 Mar 2020 09:14:07 +0000 https://www.programmerbay.com/?p=2627 In C or C++, break and continue statements are the control statements that can change the flow of program execution.

The main difference between break and continue is, break statement is used to break the loop execution based on some conditional expression, whereas the continue statement skips the code that comes after it in a loop and begins a new iteration based on a certain condition.

Difference Between Continue And Break Statement in C++ in Tabular Form

BasisBreak StatementContinue Statement
DefinitionIt is a control statement that is responsible for immediate termination of current enclosing loop (innermost loop in case of nested loop statements)It is a control statement that is responsible for termination of current iteration of the loop and start next iteration again, without processing statements that come after it within the enclosing loop body
UseIt is widely used in switch and loopsIt is used in loops only
Functionality It terminates iteration of the loop or switch statement( in case of loops, it forces to exit from the loop and execute the code which immediate loop, in case of Switch it terminates the statement)It skips the statements that come after it and goes for the next iteration of the loop
BehaviourWhen the break statement is encountered within the loop or switch statement, the execution is stopped immediatelyWhen the continue statement is encountered within the loop during program execution, the statements after it is skipped and the control of loop jumps to the next iteration
TerminationIt causes early termination of the current loopIt causes an early execution of the next iteration of the loop
Program flowIt typically stops the continuation of the current loop of the programIt only stops the current loop iteration but does not hampers the continuation of the loop in the program

Break Statement

A break statement is a control statement that terminates the execution of a loop or a block in which it occurs. Therefore, it breaks the flow of a program.

In C++, the break statement is mainly used in these two cases :

a) Within a loop: When a break is triggered in a loop, it stops the loop execution immediately and transfers the control to the very next statement after it.

Figure: Break Statement Working inside a loop
Figure: Break Statement Working inside a loop

In the case of a nested loop, if a break is present in an inner loop, then it would terminate only the execution of that particular loop only.

b) In Switch Statement: In order to prevent consecutive execution of cases, every case in the switch statement comes with a break statement.

Syntax :

break ;

C++ program to show the working of Break Statement : 

#include<iostream> 
using namespace std; 
int main() {
    for(int a=0;a<5;a++) 
    {if(a==3) 
    break; 
    cout<<a<<" ";
    } 
    return 0; 
}

Output :

0 1 2

Continue Statement

It only terminates the current iteration of the loop and starts the next iteration again, without processing statements that come after it within the enclosing loop body. It is used with looping statements only.

continue statement 1
Figure: Continue Statement Working inside a loop

 

In simple words, it skips the current iteration and jumps to the next loop iteration without executing statements in the loop body that comes after it.

Syntax : 

continue ;

C++ program to show the working of Continue Statement : 

#include<iostream> 
using namespace std; 
int main() 
{ 
    for(int a=0;a<5;a++) 
    { 
        if(a==3) 
        continue; 
        cout<<a<<" "; 
    } 
    return 0; 
}

Output : 

0 1 2 4

]]>
https://programmerbay.com/break-and-continue-statement-in-c/feed/ 0