Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
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
6.
Advantages and Disadvantages of Jump Statements in C++
7.
Frequently Asked Questions
7.1.
What is the use of a jump statement in C/C++?
7.2.
What is the difference between the various control statements in C/C++: break, continue, and goto?
7.3.
What is the advantage of switch statements over if...else?
7.4.
Which control statements in C/C++ have to be used with the switch statement for this proper functioning.
7.5.
Which of the control statements in C/C++ are not available in Java?
7.6.
Among the various control statements in C/C++: break, continue, and goto, which one is the best?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

Jump Statement

gp-icon
Data structures & algorithms (Beginner to Intermediate)
Free guided path
13 chapters
99+ problems
gp-badge
Earn badges and level up

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

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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


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

 

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

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 continue statement continues to the next iteration and skips the remaining code.
The goto statement is used to transfer the program control to a labeled 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. 

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.

Previous article
Loops
Next article
Nested If Else Statement in C
Guided path
Free
gridgp-icon
Data structures & algorithms (Beginner to Intermediate)
13 chapters
109+ Problems
gp-badge
Earn badges and level up
Live masterclass