In C++, control statements are used to change the normal flow of program execution. Among them, break, continue, and goto statements are important jump statements that help control loops and program flow.
C++ programming – break, continue and goto
The break statement is used to stop the loop immediately. When the break statement is executed inside the loop, then the loop execution terminates the loop immediately.
Example,
#include <iostream>
using namespace std;
int main() {
// break example
for(int i = 1; i <= 5; i++) {
if(i == 3)
break;
cout << i << " ";
}
return 0;
}continue statement
The continue statement is used to skip the remaining statements of the current iteration and move directly to the next iteration of the loop. The loop continues running after continue.
Example,
#include <iostream>
using namespace std;
int main() {
// continue example
for(int i = 1; i <= 5; i++) {
if(i == 3)
continue;
cout << i << " ";
}
return 0;
}goto Statement
The goto statement is also known as the unconditional jump statement. The goto statement is used to transfer program control directly from one place to another within the same function.
Syntax:
goto label;
// statements
label:
statement;Example,
Question: Write a program to print the number from 1 to 5.
#include <iostream>
using namespace std;
int main() {
int i = 1;
start:
cout << i << " ";
i++;
if(i <= 5)
goto start;
return 0;
}Disclaimer: We have provide you with the accurate handout of “C++ programming – break, continue and goto“. If you feel that there is any error or mistake, please contact me at anuraganand2017@gmail.com. The above study material present on our websites is for education purpose, not our copyrights.
Images and content shown above are the property of individual organisations and are used here for reference purposes only. To make it easy to understand, some of the content and images are generated by AI and cross-checked by the teachers.