This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

what is wrong here

ive got this code and some works but this is the problem

tryagin:
if ( *var1 == 22 && *var2 == 75 ) goto end;

goto tryagin;

what is wrong?

Parents Reply Children
  • Whenever you get an urge to use goto - purge it. Think about how you can rewrite the code. A good teacher will reject code containing goto. Especially since you can _often_ rewrite the code in a very elegant way.

    And also try a bit of logic rewrites:

    repeat:
        if ( *var1 == 22 && *var2 == 75 )
            goto end;
        goto repeat;
    end:
    


    Without goto:

    while (!(*var1 == 22 && *var2 == 75))
        ;
    


    Rewritten logic that is easier to read:

    while (*var1 != 22 || *var2 != 75)
        ;
    

    Note that a list of && operations can be converted to a list of || operations whith each term and the end result inverted.

    And a list of || operations can be converted to a list of && operations with each term and the end result inverted.

    As you can see, the goto normally doesn't help to produce shorter or more elegant code. It produces hard-to-read code, and can destroy some of the optimizations in the compiler because the goto is harder to analyse.