bool w;
int x[1000];
int y[1000];
for(int i=0;i<1000;i++){
x[i] += y[i];
}
if(w){
for(int i=0;i<1000;i++){
y[i] = 0;
}
}
Slower but prettier with the if pushed down.Easier for humans to reason about the branch, when they know that it happens without input from i or y[i].
This is helpful when the `if` statement is complex, or there are multiple if statements, some of which do take y[i] as input.
A loop contains a branch - keep looping or break. At least according to structured programming -- https://en.wikipedia.org/wiki/Structured_program_theorem.
interesting question...
> A loop is a chunk of code that will be run N times depending
oh! turns out you do!
for (fn, n) = {
stop = void (i) {}
run = void (i) { fn(i); go(i + 1) }
go = void (i) { *((i < n) * &run + (i >= n) * &stop)(i) }
go(0)
}
Or you could write all the implementations for each possible n and dispatch through a registry.The machine?
The CPU.
Just a simple example, but the conditional branch occurs on line 14 of the generated assembly. It does a comparison (line 13) and then a conditional jump (jl, line 14).
Anyone who started out programming in assembly languages