Hello friends, In Assembly editor in KEIL (for 89LPC9xx series), things go easy. You make a jump point AT ANY LINE of main code(like point0:) and then you can conditionally jump back there to do something or reloop, according to program needs. Is there any chance you can do it with C ( ? ? ). Ofc there are (as i, a newbie knows) some looping statements like while() or for () - but in a specific application i am working these arent enough :(
I have the full flowing diagram of the code i need and i can email it to you just to see. If it was assembly it should be done by me and ready to release the project it in production line, but for C.... I am stuck for now...
Best regards all :) Timothy Kondos
yes, C has a keyword goto.
it is very useful sometimes.
this is an example of how you can use it
void main(void) { int a; int b; label: a = call_a_function(); b = call_another_function(); if ( a < b ) goto label; }
BUT BE CAREFUL.
Language purists (and biassed amateurs) will probably say 'never use a goto'.
They DO have their use ........ sometimes.
Thanks King,
Imagine a keyboard switch debouncer and full conditional jumps to processes dependable from user keyboard commands..... Goto seems a one way solution.
Anyway, dont tell that to purists :(
Thanks again Timothy
Yes, goto statements have their uses. But they are few!
One of the very few reasons is to do a mult-block exist instead of using a large number of tests of a break variable. Similarly, it might be used as a multi-block continue statement. C++ can use exceptions instead of goto for multi-level breaks.
The example above is a good example of where goto should _not_ be used, since it represents a standard do { ... } while() block.
If a flowchart isn't implementable without goto statements, then it is probably drawn at the wrong conceptual level, or the program flow should be better broken down and analysed and a new flowchart drawn.
A function should normally not have more source lines than what you can see at the same time in the editor. The exception is if the code contains very trivial and repetitive code, such as a long switch block, or a huge number of printf() etc.
When the problem is broken down into functions of no more than 50 lines of code, there will not be too many conditions or loops in the individual functions, so they can be written without the need of goto statements.
The goto statements can feel nifty, but the cost of maintaining a program that contains goto (other than multi-level-break or multi-level-continue) will very quickly skyrocket.
"The example above is a good example of where goto should _not_ be used, since it represents a standard do { ... } while() block."
For a moment, I though I hit the wasp nest of a purist!
Per is right, I might not have given the best of examples, but I think I showed the mechanism accurately.
What the OP needs to do is find himself a good book on C programming - Hopefully one that explains a pragmatic 'real life' approach.
Purism is totally beside the point. Code with goto is expensive to maintain. And it often breaks the back of the optimizer in the compiler. Tools to analyse the execution flow gets it harder (and will definitely complain).
Imagine a function that perform debounce.
Imagine a second function that proceses debounced key presses and has a jump table or switch block for the individual actions to peroform.
No need at all for any goto to perform that.
I magine a keyboard switch debouncer and full conditional jumps to processes dependable[sic] from user keyboard commands..... Goto seems a one way solution. Anyway, dont tell that to purists :(
the goto accepted by the purists is called a switch statement which seems to be exactly what you discuss in your 'example'
a switch statement, basically, is a "goto if"
Erik
If you think you need a goto, these are the rules for its usage:
#1 You are too stupid to use the language to implement the algorithm correctly.
#2 If you still think you need a goto, make sure that somebody somewhere won't claim that you were "#1"
#3 If you pass rule #2, then use it. Then document it so that another engineer, won't call you "#1" and attempt to re-write everything only to find out that you were not "#1" (If he succeeds in this re-write, then you were indeed "#1")
#4 If you ignore these rules and think that 'goto' is a proper construct because it is intrinsic within the language's keywords such as 'for', 'switch', 'while', etc,... and "since it is used all the time in assembly", then you don't understand a major element and purpose of a higher-level programming language: the controlled use of 'goto' (then see rule #1)
Also, when people talk about using 'goto' in deeply nested constructs, I still think that the code wasn't implemented correctly. The use of 'goto' becomes a lazy way to avoid the task of re-working your methods and algorithm (the deeply nested construct usually means that you've invested much effort into it to begin with, and dread the idea that you'll need to totally re-vamp 'all that code'). Just my opinion.
--Cpt. Vince Foster 2nd Cannon Place Fort Marcy Park, VA
#3 If you pass rule #2, then use it. Then document it so that another engineer, won't call you "#1" and attempt to re-write everything only to find out that you were not "#1" (If he succeeds in this re-write, then you were indeed "#1") I guess that for the last 15 years or so, for more than half my working time I have been coding in 'C'. I have yet to use a goto, simply because I have never found a reason to use it.
If you Think in ASM which only has Gotos and try to write a C program you get a mess that is neither C or ASM. People write in C for a reason. The ability to jump any where at any time is not one of them. The Goto is there, but should be seldom used. Use the tools that C gives you and you get the benefits. Spaghetti is best for eating not coding.
Spaghetti is best for eating not coding. and your listing get so messy when you pour tomato sauce on it :)
sorry, couldn't help that one
The biggest reason for goto is people who wants to implement the full logic in a single function, just as they can (but should they?) implement the full in a single big block of assembler instructions.
One problem with splitting an alorithm into multiple functions is how to share state information between the individual functions. But that is the reason why the world got object-oriented languages.
The funny thing is that when a book describes the use of goto, the typical example is almost always trivially simple to rewrite to use the normal loop constructs. Just as all programming books has to take the factorial or Fibonacci series as examples of recursive functions, even if they are even simpler (and way more efficient) to implement as loops.