Loops and ranges in Julia
start:stop and start:step:stop drive for; while guards.
# Ranges are first-class: start:stop and start:step:stop.
for pass in 1:4
println("Guiding pass $pass")
end
for alt in 80:-15:35
println("Slewing through $alt degrees")
end
# while loops on a condition; global mutation is explicit in scripts.
battery = 100
while battery > 70
global battery -= 12
end
println("Battery at $battery%")
How it works
1:4is a real value, iterated directly.80:-15:35counts down by fifteens.- In a script, mutating a loop-external binding takes
global.
Keywords and builtins used here
endforglobalinwhile
The run, in numbers
- Lines
- 15
- Characters to type
- 334
- Tokens
- 54
- Three-star pace
- 70 tpm
At the three-star pace of 70 tokens a minute, this run takes about 46 seconds.
Step 2 of 3 in Control flow, step 5 of 27 in Language basics.