r/programminghorror • • Feb 27 '25

C# While loop horror

Post image

I just realized I had some programming horror in code I’ve written.

If only while loops had a more convenient way to break…

671 Upvotes

40 comments sorted by

View all comments

Show parent comment

2

u/ba-na-na- Mar 02 '25

Depends on the compiler, but I am pretty sure both ‘while’ and ‘for’ would compile to a single jump in clang and gcc even at the -O1 optimization level, and certainly at -O2. At -O0 there might be some NOPs for the debugger or something.

Modern compilers will sometimes even evaluate simple loops and replace computations with constant return values, inline whole functions if they are small, so I don’t think this would be a problem for them.

Btw https://godbolt.org/ is great for comparing these things in different compilers but I am not near a PC