What's new

Closed c++ alternative for goto?

Status
Not open for further replies.

nirv47a

Eternal Poster
Joined
Nov 10, 2018
Posts
958
Reaction
154
Points
301
mga sir pahelp naman po, beginner po ako sa pagcocode and habang ginagamit ko ung goto START; ay ayaw nya pumunta sa pinakataas which is the username, instead sa may password sya nagtutuloy, pahelp naman po.
 
Bakit nung kapanahunan namin wala kaming goto sa c++?
Well ito ang reason.

"NOTE − Use of goto statement is highly discouraged because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. Any program that uses a goto can be rewritten so that it doesn't need the goto. "
You do not have permission to view the full content of this post. Log in or register now.

Better use function and make use of while loops for iterations.
Kasi sa ibang programming languages, wala yang goto.
Mahihirapan ka mag adapt ng ibang language kung yan tatangkilikin mo.
Just an advice.
 
To better understand what mrHazan is trying to say, here is an example: (Note: both programs have the same output.)

Using the goto function
Code:
int main()
{
    int retry = 1;
    char input = 'y';

    LOOP: // a label used for goto 
        cout << "\nTry #" << retry++;
        cout << ": Continue (Y)? ";
        cin >> input;

        switch (input)
        {
            case 'n':
            case 'N': break; // terminates the loop
            case 'y':
            case 'Y':
            default: goto LOOP;
        }

    // exit the program
    return 0;
}

using a user-defined function:
Code:
// a user-defined function
bool FunctionLOOP()
{
    char input = 'y';
     cout << ": Continue (Y)? ";
     cin >> input;

     switch (input)
     {
         case 'n':
         case 'N': return false; // terminates the loop
         case 'y':
         case 'Y':
         default: break;
     }
    return true;
}

int main()
{   
    int retry = 1;
    bool flag = true;

    while (flag)
    { 
        cout << "\nTry #" << retry++;

        flag = FunctionLOOP();
    }

    //exit the program
    return 0;
}
 
Status
Not open for further replies.

Similar threads

Back
Top