cpp goto statement

goto statement in C

The goto statement is unconditional control statement that transfers the cursor of current to the another part of the program. goto statement can be used as—

goto;

goto label;
………
………
………
label:
statement
……..
……..
……..

label is any valid identifier and it is followed by semicolon. whenever the goto label is encounter then control is transfer to the define label, that is immediately after the label.

/* program to find the number is even or odd using goto statement*/
#include< iostream.h >
int main()
{
int num;
cout<<“Enter A number to check even or odd”;
cin>>num;
if(num%2==0)
{
goto even;
}
else
{
goto odd;
}
even:
cout<<“number is even \n”;
goto end;
odd:
cout<<“Number is ODD \n”;
goto end;
end:
cout<<“\n”;
return 0;
}

we can be place label anywhere. if the label is after goto statement then the control is transferred forward and this is know forward jump and if the label is before goto statement then control is transfer to the backwards this is known as backward jumping.

note: The control can be transfer with in the function using goto statement not outside of function

Break keyword in C

Break statement used inside in any loop or switch statements. Sometimes it is necessary to go outside from the loop even before the loop condition becomes false. in such conditions when terminates the loop break statement is used. This break statement cause an immediate exit from the loop in which break statement appear. example it is written as

break;

when break statement is encounter control is transfer immediately out from the loop. If break written a nested loop structure then it causes exit from the innermost loop.

Let take Example to understand the break statement uses

/* program to understand the use of break*/
#include < iostream.h >
void main()
{
int i;
for(i=0;i<=10;i++)
{
if (i==5)
break ;
cout << i;
}
return 0;
}

in this example its normally executes 11 (0 to 10) times but here the value of i becomes 5 break is encounter and loop is terminated. .

  • Basic OOPS Concepts
  • Benefits of OOPs
  • C++ History
  • C++ Applications
  • C++ features
  • C++ program structure
  • C++ Installation
  • C++ Comments
  • C++ output operator
  • C++ input operator
  • C++ Header files
  • C++ variables
  • C++ Compiling and linking
  • C++ Exercise
  • C++ Tokens
  • C++ Identifiers & constants
  • C++ Data types
  • C++ User defined data type
  • C++ derived Data types
  • C++ Symbolic constant
  • C++ Type compatibility
  • C++ Declaration of variable
  • C++ Reference Variables
  • C++ Exercise
  • C++ operators
  • C++ Scope Resolution operators
  • C++ Member Dereferencing Operator
  • C++ Memory Management operator
  • C++ Manipulators
  • C++ Type Cast Operator
  • C++ Exercise
  • C++ Expressions
  • C++ Special Assignment
  • C++ Implicit Conversions
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Operator Precedence
  • C++ Exercise
  • C++ if statements
  • C++ switch statement
  • C++ Do-while statement
  • C++ while statement
  • C++ for statement
  • C++ Exercise
  • C++ function introduction
  • C++ function prototyping
  • C++ Call by reference
  • C++ Inline function
  • C++ Default Arguments
  • C++ function overloading
  • C++ Math Library Functions
  • C++ Exercise
  • C++ class Introduction
  • C++ structure Revisited
  • C++ Specifying a Class
  • C++ Creating Objects
  • C++ Accessing Class Members
  • C++ Defining Member Functions
  • C++ Nested OF Member Functions
  • C++ Private Member Functions
  • C++ Array with in a class
  • C++ Memory Allocation for objects
  • C++ Static Data Members
  • C++ Array of objects
  • C++ Friendly functions
  • C++ Returning Objects
  • C++ Member Functions
  • C++ Pointer to Members
  • C++ Local Classes
  • C++ Exercise
  • C++ Constructor Introduction
  • C++ Parameterized Constructor
  • C++ Multiple Constructor
  • C++ Dynamic Initialization of Objects
  • C++ Copy Constructor
  • C++ Dynamic Constructor
  • C++ Destructors
  • C++ Exercise
  • C++ Operator Overloading
  • C++ Overloading Unary Operators
  • Const pointer in C
  • Void pointer in c
  • C++ Overloading Binary Operators
  • C++ Overloading Binary Operators Using Friends
  • C++ Manipulation String Using Operators
  • C++ Rules for overloading operators
  • C++ Exercise
  • C++ Basic To class Type
  • C++ Class TO Basic Type
  • C++ One class To another class type
  • C++ Exercise
  • C++ Inheritance introduction
  • C++ Single Inheritance
  • C++ Multiple Inheritance
  • C++ Ambiguity Resolution in inheritance
  • C++ Hierarchical Inheritance
  • C++ Hybrid Inheritance
  • C++ Virtual Base Classes
  • C++ Exercise
  • C++ abstract class
  • C++ nesting of classes
  • C++ Exercise
  • C++ polymorphism
  • C++ Exercise
  • C++ pointers
  • C++ Pointers TO object
  • C++ this pointer
  • C++ Pointer to Derived class
  • C++ Virtual functions
  • C++ Exercise
  • C++ streams
  • C++ unformatted I/O operations
  • C++ Put() and get()
  • C++ getline() and write()
  • C++ Formatted console I/O
  • C++ Manipulators
  • C++ Exercise
  • C++ file handling
  • C++ file stream classes
  • C++ Open and closing file
  • C++ open using constructor
  • C++ open using open()
  • C++ Detecting End of file
  • C++ File modes
  • C++ File pointers and Manipulators
  • C++ Sequential I/O
  • C++ Reading and Writing
  • C++ Updating a File
  • C++ Error handling In File
  • C++ Command Line Arguments
  • C++ Exercise
  • C++ Template introduction
  • C++ Class Templates with multiple Parameters
  • C++ Function templates
  • C++ Function templates with multiple parameters
  • C++ member function Template
  • C++ Exercise
  • C++ Exception handling
  • C++ Basics of Exception Handling
  • C++ Exception Handling Mechanism
  • C++ Throwing Mechanism
  • C++ Catch Mechanism
  • C++ Catch all Exceptions
  • C++ Re-Throwing An Exception
  • C++ Specifying Exceptions
  • C++ Exercise