Follow

Let's start a war in here

@special-boy
Perfect anwser.
Also, goto saved my ass in uni several times. (The time crunch was indane)

@beardalaxy
yes

One of the first and most basic concepts: Loops
Two ways of writing them. One amazing, the other one retarded.

@LukeAlmighty I'm monkey brain so my concept of loops is just clicking the loop command in rpg maker xD
I'm guessing that goto just goes to a specific part of your code you want to call? That's normally what I do in rpg maker instead, they're called labels there and you can jump to a label. Bit more versatile than a loop imo.

@LukeAlmighty Would I be right in saying for loops can be optimised since the number of iterations can be known beforehand whereas while loops cannot?

@thatbrickster
I'm not sure about that. I believe, that you can change the iterated variable from inside the for loop.

@LukeAlmighty Disgusting. I instead vote for a third option, recursion.
@LukeAlmighty As an inexperienced programmer I wanted pure performance and would've been okay with mutability and reusing variables. As an experienced programmer I want correct programs that I know will perform well should they be designed properly.

@thatbrickster @LukeAlmighty Compilers often do tail calls to "unwrap" it into whats basically a for loop anyway if your function doesnt require any added stack space, and if it does, good luck

@LukeAlmighty @thatbrickster Compilers can unroll for loops with compile time values

One thing thats actually cooler though is that for loops can be parallalised with almost 0 effort while while loops can't, and these work on non-constants

omp parallel for

Before any for loop and it gets spread over as many threads as your cpu has

If you're iterating over a collection or just need to guarantee it stops after a set number of times, always for-loop for clarity. Only while-loop when you need that higher level of control over the exit condition.

@LukeAlmighty the zig way: for working as “for each element in”, while for everything else

@LukeAlmighty
Depends on the use case, but I've used for far more often than while.

Sign in to participate in the conversation
Game Liberty Mastodon

Mainly gaming/nerd instance for people who value free speech. Everyone is welcome.