Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Last Updated: Apr 28, 2024
Difficulty: Easy

Jump Statements in C++

Leveraging ChatGPT - GenAI as a Microsoft Data Expert
Speaker
Prerita Agarwal
Data Specialist @
23 Jul, 2024 @ 01:30 PM

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.

Also, see Literals in C. Fibonacci Series in C++

Types of Jump Statements

There are four types of jump statements

  1. Continue
  2. Break
  3. Return 
  4. goto 

Let us discuss each one of them in detail.

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

Continue Statement

Syntax

continue;

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

Output:

Sum= 37

 

You can try by yourself with the help of online c++ compiler.


Using while loop:

#include <iostream>
using namespace std;
 
int main() 
{
 int sum = 0, i = 0;
 while(i < 10) 
{
   i++;
   if(i%3 == 0)
     continue;
   sum =sum + i;
 }
 cout<<"Sum= "<<sum<<endl; 
 return 0;
}

Output:

Sum= 37

Using do-while loop:

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

Output:

Sum= 37

Break Statement

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;

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

Output:

Enter a positive integer:
14
Enter a positive integer:
26
Enter a positive integer:
-13
Negative number entered
Loop breaks
Sum= 40

Using while loop:

#include <iostream>
using namespace std;
 int main()
 {
 int i = 1, sum = 0;
 while(i <= 5) 
{
   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;
}

Output:

Enter a positive integer: 14
Enter a positive integer: 26
Enter a positive integer: -13
Negative number entered
Loop breaks
Sum= 40

Using do-while loop:

#include <iostream>
using namespace std;
 
int main() 
{
 int i = 1, sum = 0;
 do 
{
   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 += 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

 

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

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

Output:

Start 5 6 7 8 9 end

Goto Statement

Syntax:

goto identifier;


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 (:).

Let's see an example of the goto statement in C++.

Example

#include <iostream>
using namespace std;
int main()
{
    int n = 4;
    if (n % 2 == 0)
        goto L1;
    else
        goto L2;
  
L1:
    cout << "Even" << endl;
    return 0;
  
L2:
    cout << "Odd" << endl;
}

Output:

Even


Check out this problem - Subarray Sum Divisible By K

Frequently Asked Questions:

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.

Topics covered
1.
Introduction
2.
Types of Jump Statements
3.
Continue Statement
3.1.
Examples
3.1.1.
 
3.1.2.
Using do-while loop:
4.
Break Statement
4.1.
Examples
4.1.1.
Using for loop:
4.1.2.
Using while loop:
4.1.3.
Using do-while loop:
5.
Return Statement
5.1.
Example
6.
Goto Statement
6.1.
Example
7.
Frequently Asked Questions:
8.
Key Takeaways