views:

429

answers:

7

In the spirit of the latest podcast where Joel mentioned he'd like some simple questions with possibly interesting answers ...

In the environments we have to programme in today we can't rely on the order of execution of our langauage statements. Is that true? Should we be concerned?

Will 30 GOTO 10 always go to 10?*

*I didn't use 20 on purpose ;)

[edit] for the four people voting for closure of this question ...

"Runtime compilers use profiling information to help optimize the code being compiled. The JVM is permitted to use information specific to the execution in order to produce better code, which means that the compiling method M in one program may generate different code than compiling M in another"

(from Java concurrency in practice, 2006, Goetz et al)

.. have a look at "monomorphic call transformation"

[edit]

and another edit, your processor is allowed to swap the order of execution of statements ...

+6  A: 

Not if you've got

40 COMEFROM 30

somewhere in your code.

Joachim Sauer
You forgot to say "PLEASE"
1800 INFORMATION
The language description specifies a certain range of ratios of "PLEASE" clauses(IIRC), lest your program seem either impolite or obsequious to the compiler. Is it defined whether a single-line program needs a "PLEASE"?
David Thornley
I admit, my INTERCAL-fu is lacking. This is of course the BASIC-variation. In ITERCAL it would be "PLEASE COMEFROM .3" or something like that.
Joachim Sauer
A: 

Well, it won't if it doesn't execute, for example if there's a 20 GOTO 40.

McWafflestix
+1  A: 

In BASIC, yes. In other languages, it will generate a compile error.

Jekke
+14  A: 

No, it will not. But it will always behave as if it did. One of the basic rules in compiler design is the "as if" rule. Your compiler can make whatever changes it likes, as long as the result is going to behave as if you'd followed the language rules.

GOTO 10 may decide to jump to some other location, as long as the compiler can guarantee that it won't change the outcome of the program.

So no, it shouldn't worry you.

jalf
For example, if 10 GOTO 100, the compiler is likely to compile it as 30 GOTO 100. (This doesn't often happen in hand-written code, but this sort of thing happens all the time in compiler-generated intermediate code.)
David Thornley
+1 at the moment this is the only sensible answer here
Bedwyr Humphreys
A: 

What are you trying to say? Anyone claiming to be a programmer should know (especially after your hint) that it depends on what's on lines 10 and 20, and anyone who's been on SO for longer than a week should realize that this question doesn't seem to add much.

We should rely on the code doing exactly what we tell it to. If we tell it to GOTO 10 on line 30, but also tell it to skip line 30, we shouldn't be the least surprise when line 30 is skipped. We told the program to.

Tomas Lycken
You compiler is allowed to make changes to the order of execution of your statements. So is your processor. Are you aware of this?
Bedwyr Humphreys
As jalf pointed out, it doesn't matter for the results. Why bother?
Tomas Lycken
Because it's a vaguely interesting question?
Bedwyr Humphreys
+6  A: 
DJ
A: 

True, if you consider multi-core CPUs executing a BASIC program, one of the cores might accidentally miss the GOTO (hint: "TLB Bug") and continue as if it weren't there.

devio