What Is Jump Statements In C/C++?

What Is Jump Statements In C/C++?
What Is Jump Statements In C/C++?

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, the logic behind them along with some examples.

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. (https://www.vidaliaonion.org)

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

1. break 

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 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
  1. continue 

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 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
  1. goto

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

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

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

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

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;
}

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;
}

Output:

Enter the month number:
5
May

Frequently Asked Questions

What is the use of a jump statement in C/C++?

The jump statement is used to control the flow of the program if some condition is satisfied.

What is the difference between the various control statements in C/C++: break, continue, and goto?

The break statement terminates the current loop. The continued statement continues to the next iteration and skips the remaining code. The goto statement is used to transfer the program control to a labelled statement.

What is the advantage of switch statements over if…else?

The switch statement makes the code structured and readable.

Which control statements in C/C++ have to be used with the switch statement for this proper functioning?

The break statement has to be used with the switch statement to terminate the switch statement effectively.

Which of the control statements in C/C++ are not available in Java?

Java does not support the goto statement. Instead, it uses the label to achieve the same functionality.

Among the various control statements in C/C++: break, continue, and goto, which one is the best?

Among the following control statements in C/C++, all have different use cases and have to be used accordingly. However, it is better to avoid the goto statement.

Key Takeaways

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

You can now practice some MCQs based on switch statements, default case, goto statement, jump statements, and if condition.

Don’t stop here. Check out our C++ guided path to learn C++ from Scratch. We hope you found this blog useful. Feel free to let us know your thoughts in the comments section. 

By Hari Sapna Nair