The infamous "if loop" actually exists
The infamous "if loop" actually exists
The infamous "if loop" actually exists
found the following in our codebase the other day.
csharp
while(booleanFlag) return;
thhere wasn't more in the loop body, that was it. the loop conditional does exist and it can hurt you.
Can it hurt you, though? My guess would be every sane compiler and interpreter optimizes this.
the codebase readability certainly suffers, and this isn't the only case of shenanigans like this
I once went to the computer of a class mate and opened stdio.h
and added #define while(x) if(x)
. He was so confused. 🤣
Did you know python for loops have an else clause?
Did you also know it runs after the loop iterator is done, and not when there was never anything to iterate over at all?
That actually seemed quite useful at first.
Your point about it not running when there is nothing to iterate over is incorrect. The else-statement runs when the iterator is exhausted; if the iterator empty, it is exhausted immediately and the else-statement is executed.
Through the magic of make, you can write code that changes if statements to while loops then changes it back after compilation passes or fails.
I only give good advice.
int main() { useless: int x = 10; if (1) { goto useless; } }
Yea, I saw C style and was disappointed when it didn't abuse gotos.
This is the real way!
ELI5 plz
Statements that start with # in C/C++ are known as preprocessor directives, that is, they are executed before compilation begins. OP has used a #define
which will replace any instance of A ( IF(x)
) with B ( while (x)
) in the code.
So the IF
statement is really just a while
statement.
They used a macro to make 'IF' (which is distinctly not 'if') map to 'while'. So it's really a while(1) loop, which will repeat forever, or until the program is terminated, whichever comes first.
Someone's just being silly.
#define if while // 🤪
10 print "All Work, No Play"
20 goto 10
30 end
int* ihadatonepointforgottenaboutpoundefinesyoubastard {};
i have a colleague always saying if loop (in our language). I'm a bit to shy to tell him his mistakes... People easily hate me but i would like to...