Hello and Asalam o Alikum. I hope you all are fine by the grace of Allah Almighty.
Today I am here with a new program that explains the purpose of "goto" statement in C++.
Today I am here with a new program that explains the purpose of "goto" statement in C++.
"Goto" statement
The "goto" statement is used to unconditional transfer of control to a named label. The label must be in the same function otherwise it will not jump to the label. A label has a specified name. A label is meaningful only to a "goto" statement.
Syntax
goto label ;
.....
.....
label: statement
OR
label:statement:
........
..........
goto label ;
Below is the source code of an program that will determine whether a number entered by user is positive or not using "goto" statement.
Source code
Output
Explanation:
In this program, I have used a simple "if" condition and "goto" statement.
It will take a number as input from the user and it will determine whether is is positive or not.
If the user enters a negative number, then it will jump to the label,"Positive" and then again it will ask the user to enter the number again and continues to display this message until the user enters a positive number.
No comments:
Post a Comment