• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » C++ » Structures » Jump Statements in c++ or Goto statement
Next →
← Prev

Jump Statements in c++ or Goto statement

By Dinesh Thakur

Jump statements are used to alter the flow of control unconditionally. That is, jump statements transfer the program control within a function unconditionally. The jump statements defined in C++ are break, continue, goto and return. In addition to these jump statements, a standard library function exit () is used to jump out of an entire program.

The break Statement: The break statement is extensively used in loops and switch statements. A break statement immediately terminates the loop or the switch statement, bypassing the remaining statements. The control then passes to the statement that immediately follows the loop or the switch statement. A break statement can be used in any of the three C++ loops.

Note that a break statement used in a nested loop affects only the inner loop in which it is used and not any of the outer loops. Similarly, a break statement used in a switch statement breaks out of that switch statement and not out of any loop that contains the switch statement.

The continue Statement: The continue statement is used to ‘continue’ the loop with its next iteration. In other words, continue statement skips any remaining statements in the current iteration and immediately passes the control to the next iteration. The continue statement does not terminate the loop (as in the case of break statements), rather it only terminates the current iteration of the loop. Like a break statement, a continue statement can be used in any of the three loops.

To understand the concept of the break and continue statement, consider this example.

Example : A program to add the factors of a number

The output of the program is

#include<iostream>
using namespace std;
int main () {
  int x=0, y, sum=0;
  cout<<"Enter a number: ";
  cin>>y;
  while(1) {
     x++;
     if (x>y)
       break;
     if(y%x!=0)
       continue;
       sum=sum+x;
}
    cout<<"\n Sum of factors: "<<sum;
    return 0;
}

Enter a number: 8

Sum of factors: 15

The goto Statement: The goto statement can be used anywhere within a function or a loop. As the name suggests, goto statements transfer the control from one part to another part in a program which is specified by a label. Labels are user-defined identifies followed by a colon that are prefixed to a statement to specify the destination of a goto Statement.

To understand the concept of the goto statement, consider this example.

Example: A program to demonstrate the use of goto statement

#include<iostream>
using namespace std;
int main () {
 int x = 10;
 loop: cout<<x<<","; //loop is a label
 x--;
 if (x<0)
   goto loop;
   cout<<"\n Here is the example of goto !";
   return 0;
}

The output of the program is

10, 9, 8, 7, 6, 5, 4, 3, 2, 1,

Here is the example of goto

The exit () Function: The exit( ) function is a standard library function that terminates the entire program immediately and passes the control to the operating system. This function takes a single parameter, that is, exit status of the program and returns the same status to the operating system upon termination. The status can be either a zero or non-zero value, where zero shows successful termination and non-zero shows unsuccessful termination of the program.

To understand the concept of the exit () function, consider this example.

Example: A program to demonstrate the use of exit( )

#include<iostream>
#include<cstdlib> //for exit() function
using namespace std;
int main () {
  int a;
  cout<<"Enter the value for a: ";
  while(cin>>a) {
   if(a<0) {
     cout<<"This program is going" <<"to terminate!";
     exit(0) ;
   }
     cout<<"Enter another value for a: ";
}
return 0;
}

The output of the program is

Enter the value for a: 7

Enter another value for a: 8

Enter another value for a: -4

This program is going to terminate!

You’ll also like:

  1. Jump Statements in Java Example
  2. Block Statements in the if Statement
  3. When is a switch statement better than multiple if statements
  4. Conditional Statements in C++ or if Statements
  5. Write A C++ Program To Use Forward Goto.
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

C++ Tutorials

C++ Tutorials

  • C++ - Data Types
  • C++ - Operators Types
  • C++ - CPP Program Structure
  • C++ - Conditional Statements
  • C++ - Loop
  • C++ - do-While Loop
  • C++ - Control Statements
  • C++ - Tokens
  • C++ - Jump Statements
  • C++ - Expressions
  • C++ - Constants
  • C++ - Character Set
  • C++ - Iteration Statements
  • C++ - I/O Statements
  • C++ - String
  • C++ - Manipulators

C++ Operator

  • C++ - Input/Output Operator
  • C++ - Operator Overloading

C++ Functions

  • C++ - Functions
  • C++ - Member Functions
  • C++ - Returning Object from Function
  • C++ - Call by Value Vs Reference
  • C++ - Friend Function
  • C++ - Virtual Function
  • C++ - Inline Function
  • C++ - Static Data Members
  • C++ - Static Member Functions

C++ Array & Pointer

  • C++ - Array
  • C++ - Array of Objects
  • C++ - Arrays as Class Members
  • C++ - Vector
  • C++ - Pointer
  • C++ - 'this' Pointer

C++ Classes & Objects

  • C++ - Class
  • C++ - Program Structure With Classes
  • C++ - OOP’s
  • C++ - Objects as Function Arguments
  • C++ - Procedure Vs OOL
  • C++ - Object Vs Class
  • C++ - Creating Objects
  • C++ - Constructors
  • C++ - Copy Constructor
  • C++ - Constructor Overloading
  • C++ - Destructor
  • C++ - Polymorphism
  • C++ - Virtual Base Class
  • C++ - Encapsulation

C++ Inheritance

  • C++ - Inheritance
  • C++ - Multiple Inheritance
  • C++ - Hybrid Inheritance
  • C++ - Abstraction
  • C++ - Overloading

C++ Exception Handling

  • C++ - Exception Handling
  • C++ - Templates
  • C++ - Standard Template Library

C++ Data Structure

  • C++ - Link List

C++ Programs

  • C++ Program for Electricity Bill
  • C++ Program for Multiply Matrices
  • C++ Program for Arithmetic Operators
  • C++ Program For Matrices
  • C++ Program for Constructor
  • C++ Program Verify Number
  • C++ Program Array Of Structure
  • C++ Program to find Average Marks
  • C++ Program Add And Subtract Matrices
  • C++ Program Menu Driven
  • C++ Program To Simple Interest
  • C++ Program To Find Average
  • C++ program exit()
  • C++ Program Using Array Of Objects
  • C++ Program Private Member Function
  • C++ Program To Reverse A String
  • C++ Program to Operator Overloading

Other Links

  • C++ - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW