Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Jump statements are used to control the flow of the program if some conditions are met. It's far used to terminate or continue the loop inside a program or stop the execution of a function.
It is used to execute different loop elements while skipping some parts declared inside the condition. Rather than terminating the loop, it maintains to perform the next iteration of the same loop. It's used with a selection-making statement which should be present inside the loop. This statement can be used inside a for loop or while or do-while loop.
Let's see some examples of the continue statement in C++.
Examples
Using for loop:
#include <iostream>
using namespace std;
int main()
{
int sum = 0;
for(int i = 1 ; i <= 10 ; i++)
{
if(i%3 == 0)
continue;
sum =sum+ i;
}
cout<<"Sum= "<<sum<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
#include <iostream>
using namespace std;
int main()
{
int sum = 0, i = 0;
do
{
i++;
if(i%3 == 0)
continue;
sum =sum + i;
}
while(i < 10);
cout<<"Sum= "<<sum<<endl;
return 0;
}
You can also try this code with Online C++ Compiler
It is used to terminate the complete loop if the condition is met. Unlike the holding statement after the situation is completed, it breaks the loop, and the remaining part of the loop is not executed. Break announcement is used with decision-making statements such as if, if-else, or switch statements in the loop. It forces the loop to stop the execution of a similar iteration.
Syntax:
break;
You can also try this code with Online C++ Compiler
Let's see an example of the break statement in C++.
Examples
Using for loop:
#include <iostream>
using namespace std;
int main()
{
int n = 5, sum = 0;
for(int i = 1 ; i <= n ; i++)
{
int number;
cout<<"Enter a positive integer: \n";
cin>>number;
if(number < 0)
{
cout<<"Negative number entered\n";
cout<<"Loop breaks\n";
break;
}
else
{
sum = sum + number;
}
}
cout<<"Sum= "<<sum<<"\n";
return 0;
}
You can also try this code with Online C++ Compiler
MCQs on Jump Statements are frequently asked in various competitive exams. Let's look at some additional examples of Break and Continue in C++ in this short video before moving on.
Return Statement
Syntax:
return [expression];
You can also try this code with Online C++ Compiler
The return statement is mainly used in methods to terminate a method in between and return to the caller method. It is greater essential than a destroy. Each function has a return statement with a few returning values except the void() function. However, the void() function can also have the return statement to end the function's execution.
Let's see an example of the break statement in C++.
Example
#include <iostream>
using namespace std;
int main()
{
cout << "Start ";
for (int i = 5; i < 10; i++)
{
if (i == 10)
return 0;
cout << i << " ";
}
cout << "end";
return 0;
}
You can also try this code with Online C++ Compiler
1. Which 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.
2. Among the various control statements in C/C++: break, continue, and goto, which one is the best?
The given control statements in C/C++, all have different use cases and must be used. However, it's far better to avoid the goto statement.
3. 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.
4. What is the advantage of switching statements over if-else?
The switch statement makes the code structured and readable.
Key Takeaways
This blog covered the jump statements in detail, one of the control statements in C/C++. The jump statements covered are broken, continue announcements, and goto statements. It also covered the switch statement, selection statements, and examples. For more Data structure problem practice, you can visit CodingNinjas.