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:
- Decision Making Statements
- Iterative Statements
- Jump Statements
This blog will focus on Jump statements, 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.
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 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 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
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