GOTO Statement in C++

GOTO Statement

    Goto statement perform unconditional transfer of control to specified named location or label.A label is only meaningful for goto statement.
  • The label must in the same function.

General form:

goto label_name:
..........................
.........................
statement(s);
........................
.......................
label:
statement(s);

Execution:

    When the control of execution of program reached at goto statement with label. Compiler read the label name and then compiler find that label (in lame word we say that label in term heading) in the  program or a function then when the compiler find that label then control of execution is transferred to that label then the statements after the label is executed.
  • One program or function may have many labels but when we call it then it is meaningful otherwise these labels don't interrupt normal execution.

Example:

#include<iostream>
#include<cmath>
using namespace std;
void main()
{
int num=0;
positive:
cout<<"Enter Number:";
cin>>num;
if(num<0)
goto positive;
else
{
cout<<"Square root of "<<num<<"is: "<<sqrtl(num)<<endl;
}
system("pause");
}
  • sqrtl is a built-in function for square root that is defined in cmath library.

Output:



Video:



Comments

Popular Posts