Table of contents
1.
Introduction 
2.
Jump Statements in C/C++
2.1.
break statement
2.2.
continue statement
2.3.
goto statement
3.
Should the goto statement be used?
4.
switch
5.
Advantages and Disadvantages of Jump Statements in C
5.1.
Advantages:
5.2.
Disadvantages:
6.
Advantages and Disadvantages of Jump Statements in C++
6.1.
Advantages:
6.2.
Disadvantages:
7.
Frequently Asked Questions
7.1.
Q1. What is a jump statement in C?
7.2.
Q2. Which statement is used for the jump?
7.3.
Q3. What is the goto statement in C?
7.4.
Q4. What are two jump statements?
8.
Conclusion
Last Updated: Aug 25, 2024
Easy

Jump Statement

Introduction 

When we write a program, there might be situations where the program flow has to be controlled differently. For example, we might want a task to be performed repeatedly or jump from one section to another, etc. For this, something known as Control Statements is used.

Control Statements in C/C++ are of three types:

This blog will focus on Jump statements, logic behind them along with some examples.

C Static Function

Jump Statements in C/C++

Jump statements in C/C++ are a type of Control Statements in C/C++ used to interrupt the normal flow of the program. It makes the program jump to another section of the program unconditionally when encountered. It can also be used to terminate any loop.

C/C++ provides three jump statements, namely the break, continue, and goto statements. Let’s learn about them in detail.

Also read - Jump statement in java

break statement

The break statement is used to end the loop immediately after the encounter. It is used in C/C++ in the following scenario:-

  • It is used to terminate the loop, and program control resumes at the next statement following the loop.
  • It is used to terminate a case in the switch statement, which will be discussed later in the blog.
control flow of break statement in for loop

Control Flow of break statement in for loop

 

control flow of break statement in while loop

Control Flow of break statement in while loop

 

control flow of break statement in do-while loop

Control Flow of break statement in do...while loop


Let’s see an example of the usage of the break statement. Problem statement: Calculate the sum of five positive numbers entered by the user. If a negative number is encountered, break the loop and print the sum till that point.

C Code: 

  • Using for loop
     
#include <stdio.h>
int main() {
int n = 5, sum = 0;
// repeat for at most 5 times
for(int i = 1 ; i <= n ; i++) {
  int number;
  printf("Enter a positive integer: \n");
  scanf("%d", &number);
  // check if number is negative
  if(number < 0) {
    printf("Negative number entered\n");
    printf("Loop breaks\n");
    break;
  } else {
    sum += number;
  }
}
printf("Sum: %d \n", sum);
return 0;
}

 

  • Using while loop
     
#include <stdio.h>
int main() {
int i = 1, sum = 0;
// repeat for at most 5 times
while (i <= 5) {
  int number;
  printf("Enter a positive integer: \n");
  scanf("%d", &number);
  // check if number is negative
  if(number < 0) {
    printf("Negative number entered\n");
    printf("Loop breaks\n");
    break;
  } else {
    sum += number;
  }
  i++;
}
printf("Sum: %d \n", sum);
return 0;
}

 

  • Using do...while loop
     
#include <stdio.h>
int main() {
int i = 1, sum = 0;
// repeat for at most 5 times
do {
  int number;
  printf("Enter a positive integer: \n");
  scanf("%d", &number);
  // check if number is negative
  if(number < 0) {
    printf("Negative number entered\n");
    printf("Loop breaks\n");
    break;
  } else {
    sum += number;
  }
  i++;
} while(i <= 5);
printf("Sum: %d \n", sum);
return 0;
}


C++  Code: 

  • Using for loop
     

#include <iostream>
using namespace std;

int main() {
 int n = 5, sum = 0;
 // repeat for at most 5 times
 for(int i = 1 ; i <= n ; i++) {
   int number;
   cout<<"Enter a positive integer: \n";
   cin>>number;
   // check if number is negative
   if(number < 0) {
     cout<<"Negative number entered\n";
     cout<<"Loop breaks\n";
     break;
   } else {
   sum += number;
 }
}
 cout<<"Sum: "<<sum<<"\n";
return 0;
}

 

  • Using while loop
     
#include <iostream>
using namespace std;

int main() {
 int i = 1, sum = 0;
 // repeat for at most 5 times
 while(i <= 5) {
   int number;
   cout<<"Enter a positive integer: \n";
   cin>>number;
   // check if number is negative
   if(number < 0) {
     cout<<"Negative number entered\n";
     cout<<"Loop breaks\n";
     break;
   } else {
   sum += number;
 }
}
 cout<<"Sum: "<<sum<<"\n";
return 0;
}

 

  • Using do...while loop
     
#include <iostream>
using namespace std;

int main() {
 int i = 1, sum = 0;
 // repeat for at most 5 times
 do {
   int number;
   cout<<"Enter a positive integer: \n";
   cin>>number;
   // check if number is negative
   if(number < 0) {
     cout<<"Negative number entered\n";
     cout<<"Loop breaks\n";
     break;
   } else {
   sum += number;
  }
 } while(i <= 5);
 cout<<"Sum: "<<sum<<"\n";
return 0;
}


Output:
 

Enter a positive integer:
14
Enter a positive integer:
26
Enter a positive integer:
-13
Negative number entered
Loop breaks
Sum: 40

continue statement

The continue statement is also one of the loop control statements in C/C++. When the continue statement is encountered, the code below the continue statement is skipped, and the next iteration begins.

control flow of continue statement in for loop


Control Flow of continue statement in for loop

 

control flow of continue statement in while loop

Control Flow of continue statement in while loop

 

control flow of continue statement in do-while loop

Control Flow of continue statement in do...while loop


Let’s see an example of the usage of the continue statement. Problem statement: Calculate the sum of numbers from 1 to 10 which are not the multiple of 3.

C Code: 

  • Using for loop
     
#include <stdio.h>
int main() {
int sum = 0;
for(int i = 1 ; i <= 10 ; i++) {
  // skip if i is a multiple of 3
  if(i%3 == 0)
   continue;
  sum += i;
}
printf("Sum: %d \n", sum);
return 0;
}

 

  • Using while loop
     
#include <stdio.h>
int main() {
int sum = 0, i = 0;
while(i < 10) {
  i++;
  // skip if i is a multiple of 3
  if(i%3 == 0)
   continue;
  sum += i;
}
printf("Sum: %d \n", sum);
return 0;
}

 

  • Using do...while loop
     
#include <stdio.h>
int main() {
int sum = 0, i = 0;
do {
  i++;
  // skip if i is a multiple of 3
  if(i%3 == 0)
   continue;
  sum += i;

} while(i < 10);
printf("Sum: %d \n", sum);
return 0;
}

 

C++ Code: 
 

  • Using for loop
     
#include <iostream>
using namespace std;

int main() {
 int sum = 0;
 for(int i = 1 ; i <= 10 ; i++) {
   // skip if i is a multiple of 3
   if(i%3 == 0)
   continue;
   sum += i;
 }
 cout<<"Sum: "<<sum<<endl; 
 return 0;
}

 

  • Using while loop
     
#include <iostream>
using namespace std;

int main() {
 int sum = 0, i = 0;
 while(i < 10) {
   i++;
   // skip if i is a multiple of 3
   if(i%3 == 0)
     continue;
   sum += i;
 }
 cout<<"Sum: "<<sum<<endl; 
 return 0;

}

 

  • Using do...while loop
     
#include <iostream>
using namespace std;

int main() {
 int sum = 0, i = 0;
 do {
   i++;
   // skip if i is a multiple of 3
   if(i%3 == 0)
     continue;
   sum += i;
 } while(i < 10);
 cout<<"Sum: "<<sum<<endl; 
 return 0;
}


Output:

 

Sum: 37

goto statement

The goto is one of the control statements in C/C++ that allows the jump to a labeled statement in the same function.

The labeled statement is identified using an identifier called a label. It is preceded by an identifier followed by a colon (:).

control flow of goto statement


Control Flow of goto statement 

Let’s see an example of the usage of the break statement. Problem statement: Print sum of first three positive integers.

C Code: 

  • Using for loop
     
#include <stdio.h>

int main() {
int sum=0;
 for(int i = 0; i <= 10; i++){
    sum = sum+i;
   // stop calculation
    if(i == 3){
     goto addition;
    }
  }

  addition:
  printf("%d", sum);
return 0;
}

 

  • Using while loop
     
#include <stdio.h>
int main() {
int sum=0, i = 1;
 while(i < 10){
    sum = sum+i;
   // stop calculation
    if(i == 3){
     goto addition;
    }
   i++;
  }

  addition:
  printf("%d", sum);
return 0;
}

 

  • Using do...while loop
     
#include <stdio.h>

int main() {
int sum=0, i = 1;
 do{
    sum = sum+i;
   // stop calculation
    if(i == 3){
     goto addition;
    }
   i++;
  } while(i < 10);

  addition:
  printf("%d", sum);
return 0;
}

 

C++  Code: 

  • Using for loop
     
#include <iostream>
using namespace std;

int main() {
int sum=0;
for(int i = 0; i <= 10; i++){
   sum = sum+i;
  // stop calculation
   if(i == 3){
    goto addition;
   }
 }
 addition:
 printf("%d", sum);
return 0;
}

 

  • Using while loop
     
#include <iostream>
using namespace std;

int main() {
int sum=0, i = 1;
while(i < 10){
   sum = sum+i;
  // stop calculation
   if(i == 3){
    goto addition;
   }
  i++;
 }
 addition:
 cout<<sum<<endl;
return 0;
}

 

  • Using do...while loop
     
#include <iostream>
using namespace std;

int main() {
int sum=0, i = 1;
 do{
    sum = sum+i;
   // stop calculation
    if(i == 3){
     goto addition;
    }
   i++;
  } while(i < 10);
 
  addition:
  cout<<sum<<endl;
return 0;
}

 

Output:

6


Also read,  Short int in C Programming Introduction to C Programming

Should the goto statement be used?

"The fact that 'goto' can do anything is exactly why we don't use it."

  • Bjarne Stroustrup ( creator of C++)
     

The use of the goto statement is highly discouraged. It makes it difficult to trace a program's control flow, making it hard to understand and modify. This will eventually lead to a buggy code, and it becomes challenging for the programmer to debug. 

It can also cause scope accessibility issues. In most cases, any program that uses goto can be rewritten without the use of goto. One good use of goto is to exit from a nested loop. The break does not work in this case as it causes only the innermost loop to terminate.

switch

The switch statement allows executing a block of code among many other alternatives. if...else also has the same use case. However, with the help of the switch statement, the readability and writability of the code increase.

control flow of switch statement

Control Flow of switch statement 


The switch expression is evaluated and compared with each case value. If the match is found, the corresponding code is executed. Else the default statement is executed. Finally, the break statement terminates the switch statement. If the break statement is not used, all the code blocks after the matching label are executed.

Note: The default case inside the switch statement is not compulsory. Let’s see an example to understand the usage of the switch statement. 

Problem statement: Display the month name according to the month number.

C Code: 

#include <stdio.h>

int main() {
int monthNumber;
printf("Enter the month number: \n");
scanf("%d", &monthNumber);
// select the code block according to the month number
switch(monthNumber) {
   case 1:
           printf("January");
           break;
    case 2:
           printf("February");
           break;
    case 3:
           printf("March");
           break;
    case 4:
         printf("April");
           break;
    case 5:
           printf("May");
           break;
    case 6:
           printf("June");
           break;
    case 7:
           printf("July");
           break;

    case 8:
           printf("August");
           break;
    case 9:
           printf("September");
           break;
    case 10:
           printf("October");
           break;
    case 11:
           printf("November");
           break;
    case 12:
           printf("December");
           break;
    default:
           printf("Invalid Month number\n");
           break;
}
return 0;
}
You can also try this code with Online C Compiler
Run Code


C++ Code: 

#include <iostream>
using namespace std;
int main() {
int monthNumber;
printf("Enter the month number: \n");
scanf("%d", &monthNumber);
// select the code block according to the month number
switch(monthNumber) {
   case 1:
           cout <<"January";
           break;
    case 2:
           cout <<"February";
           break;
    case 3:
           cout <<"March";
           break;
    case 4:
         cout <<"April";
           break;
    case 5:
           cout <<"May";
           break;
    case 6:
           cout <<"June";
           break;
    case 7:
           cout <<"July";
           break;
    case 8:
           cout <<"August";
           break;
    case 9:
           cout <<"September";
           break;
    case 10:
           cout <<"October";
           break;
    case 11:
           cout <<"November";
           break;
    case 12:
           cout <<"December";
           break;
    default:
           cout <<"Invalid Month number\n";
           break;
}
return 0;
}
You can also try this code with Online C Compiler
Run Code


Output:

Enter the month number:
5
May

 

Must Read Passing Arrays to Function in C

Advantages and Disadvantages of Jump Statements in C

Following are some advantages and disadvantages of Jump Statements in C:

Advantages:

  • You can change the program's flow.
  • Jump statements can be used to skip over unnecessary code.
  • Break statements allow you to specify when to exit the loop.

Disadvantages:

  • Potential for spaghetti code
  • Decreased readability
  • Misuse can lead to unexpected behaviour and difficult debugging

Advantages and Disadvantages of Jump Statements in C++

Following are some advantages and disadvantages of Jump Statements in C++:

Advantages:

  • Ability to exit loops early and skip over code blocks
  • Ability to jump to a different part of the program
  • It can be used to reduce code redundancy in certain cases
  • It can be useful for error handling in complex functions

Disadvantages:

  • Decreased readability
  • Misuse can lead to unexpected behaviour and difficult debugging
  • It can make code difficult to maintain and update
  • Can violate encapsulation and other principles of object-oriented programming in C++


Check out this problem - Subarray Sum Divisible By K, Tribonacci Series

Must Read What are Loops in Java.

Frequently Asked Questions

Q1. What is a jump statement in C?

Jump statements allow the control flow of a program to jump to a specific point within a function.

Q2. Which statement is used for the jump?

In C, jump statements include goto, break, continue, return, and exit().

Q3. What is the goto statement in C?

The goto statement provides an unconditional jump from the goto to a labeled statement within the same function.

Q4. What are two jump statements?

Two common jump statements in C are break, which exits loops or switch cases, and continue, which skips the current loop iteration.

Conclusion

This blog covered the jump statements, which is one of the control statements in C/C++, in detail. The jump statements covered are break statement, continue statement, and goto statement. It also covered the switch statement, one of the selection statements, along with examples. 

Recommended Reading: Characteristics of OOPS

You can now practice some MCQs based on switch statementsdefault casegoto statementjump statements, and if condition.

Live masterclass