We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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?
You didn't specify how the actual behavior of the code differs from your expectation, so the diagnosis of the problem is a bit difficult.
Unless the two pointers point to a volatile data type, the compiler can optimize out all but the first access to the variables.
Some other, minor issues are:
1. The possibility of an endless loop. If the condition is never met, is it okay if the program just sits there for all eternity, waiting?
2. Unnecessary use of a goto - a while()-loop could do the same job here.
In addition to everything Chistoph said, there is no end label to goto.
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.