3 ms·
That actually shows two gotos (as you've written it) because the if is itself a goto, under the hood. The way to get it down to one is this pseudo-code: ju
by Jtsummers 4y ago
That actually shows two gotos (as you've written it) because the if is itself a goto, under the hood. The way to get it down to one is this pseudo-code:
jump test
loop:
stuff
test:
test X
jump if (not-equal/greater-than/less-than/etc) loop
other stuff
It still uses two jump or branch instructions, but the loop body only uses one. It falls through if the test fails. In the case of a do/while it's the same except you remove the initial jump.
- benj111 4y agoTrue, but The parents comment >since a while is just an if with two gotos isn't it? The if has to include at least one branch instruction so you already have 2 branches to play around with. Plus python doesn't have a goto so my code is pseudo code so I don't think that you can declare it is a goto under the hood.
- gpderetta 4y agoI would call an unconditional jump inherently different from a conditional one, i.e. both if and goto are primitives. Of course on some machines everything can be predicated and the line between an if and a goto is blurred. Then there are indirect jumps...