goto statements in cpp - ProgrammerTech
Language:

Search

goto statements in cpp

  • Share this:
goto statements in cpp

The goto statement in C++

The goto statement is used to unconditionally move from one place to another within a procedure. And in order to use it, we must put a label inside the program so that we can move to it, and we choose the name for the address normally, as we used the names of the variables and put two points after it:.

The general form of a goto . statement

goto label;
.
.
label : statement;
goto statements cpp

The goto statement goes to another address within the same procedure in which it is located, and it cannot be in another procedure, although goto is easy to use but not popular with many programmers. And because of its frequent use, the program becomes incomprehensible, illegible, and not easy to follow. And it is overlapping with each other, and most of the time it is likened to “spaghetti”, meaning pasta, because the program codes are intertwined with each other, similar to pasta wrapped together.

Yet most write their programs and divide them into separate files. This is better so that each file contains one or more procedures, and each procedure has a clear and specific function because it is in this way. It is easy to understand and follow and has procedures that can be reused later in other programs.

Example 1

#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    LOOP:do {
    if (a == 15){
    a = a + 1;
    goto LOOP;
    }
    cout << "value of a: " << a << endl;
    a = a + 1;
    }
    while (a < 20);
    return 0;
}

We have a value of 10 which is the start of the iteration loop and we put a condition in the iteration loop when it reaches 15 it ignores it. And it adds in every cycle the number 1 until it is completed for the number 20 when the value of a is less than 20. When the code is run, the result will be.

value of a:10
value of a:11
value of a:12
value of a:13
value of a:14
value of a:16
value of a:17
value of a:18
value of a:19

Example 2

#include <iostream>
using namespace std;
int main()
{
    int a = 10;
    MyLabel: do{
        if(a==15)
        {
            a=a+1;
            goto MyLabel;
        }
        printf("Value is %d\n", a);
        a++;
    } while(a<21);
    return 0;
}

When running the code

Value si 10
Value si 11
Value si 12
Value si 13
Value si 14
Value si 16
Value si 17
Value si 18
Value si 19
Value si 20

Practical 1

Write a program that enters a number. If the number is less than 10, it will print the phrase: "number less ten". If the entered number is greater or equal to 10, the expression: "number bigger or equall ten" is printed.

#include <iostream>
using namespace std;
int main(){
    int x;
    cin>>x;
    if (x<10)
    goto one;
    else goto two;
    one: cout<<"number less ten"<<endl;
    two: cout<<"number bigger or equall ten"<<endl;
    return 0;
}

We notice that we have entered in the program the number 15, which is greater than 10, so it prints for us that the number is greater or equal to 10. But if we write a number less than 10, it will print the two expressions for us together. The reason for this is that the program executes instructions line by line. If the second statement comes immediately after the first statement, it prints it. When running the code

15
number bigger or equall ten

Practical 2

Write a program in which the user enters several numbers /n/ and adds them so that the user determines the number of numbers /n/ before entering them.

#include <iostream>
using namespace std;
int main(){
    int sum = 0,x,i,n;
    cout<<"enter n"<<endl;
    cin>>n;
    for(i=1; i<=n; i++)
    {
    cout<<"enter number ="<<endl;
    cin>>x;
    sum=sum+x;
    }
cout<<"the sum of number is = ";
cout<<sum<<endl;
    return 0;
}

Muhammad Alush

Muhammad Alush

إسمي محمد تركي العلوش من سورية من ريف حلب الشرقي 1994, محب للتقنية و التعلم الذاتي, و جميع خبراتي تعلمتها ذاتياً, و اللغات البرمجية التي أتقنها هي html, css, javascript, php, c, python بالإضافة إلى حبي الكبير لمجال الشبكات.

موقعي programmer-tech.com