this post was submitted on 29 Nov 2023
-7 points (40.0% liked)
Programming
17398 readers
104 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities [email protected]
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
I can understand telling you not to use
break
andcontinue
if the point is to teach you to think about different ways to solve problems, but saying it's because "it makes the code harder to read" is bullshit. Readable code flow is important, but if using those makes your code too hard to read, your problem is most likely that you've just written shitty code.To get really into the technical weeds, what
break
andcontinue
boil down to in the compiled machine code is a non-conditional branch instruction. This is just going to move the execution pointer to a different location in memory. Other keywords, such asif
,elif
, andelse
, will compile down to conditional branch instructions. Basically the same thing, but they have the added cost of having to evaluate some data to see if the branch should happen at all. You can achieve the same things with both, but the high level code might need to look different.For instance, if you're in a loop,
continue
will let you skip the rest of the code in the loop to get to the next iteration. Not a huge deal to instead make the entire code block conditional to skip it. However, thebreak
keyword will let you exit the loop at any point, which is more complicated to deal with. You would have to conditionalize your code block and force the looping condition to something that would stop it on the next iteration. If you ask me, that has the potential to be much more complicated than necessary.Also, good luck using
switch
without anybreak
s, but I'm guessing that's not quite what your teacher had in mind.In short, just go with it for now. Be creative and find a way to make it work to your teacher's liking, but always try to be aware of different ways you can accomplish a task. Also, I don't know what language you're using, but if you're in C/C++ or C# and you feel like getting really cheeky, it doesn't sound like she disallowed the use of
goto
. It's kinda likebreak
with fewer safeguards, so it's super easy to write broken code with it.The teacher, probably: “You must always put a
switch
in its own function! Then usereturn
at the end of each case.”@zib @UnRelatedBurner @programming
Good point, that is a valid way to do it sometimes, but it's extremely situational and trying to do that for everything would be absolute nonsense.
Oh absolutely. I can think of several situations where that wouldn't work well or at all, for example, a switch statement that sets up variables to be used in the rest of the function.
@zib @UnRelatedBurner @programming
thanks for the breakdown, I don't know if my grade would be too high of a sacrifice to try goto, but I'll have it in mind. thanks.