The loop will run to completion with no side effects. Unless it gets optimized away by the compiler or CPU.
What will happen if we put a semi-colon after a for loop in C++?
Submitted 8 months ago by Albin7326@suppo.fi to programming@programming.dev
Comments
JakenVeina@lemm.ee 8 months ago
vext01@lemmy.sdf.org 8 months ago
Now slap some commas in there and you can have side effects in the loop header.
Guaranteed to confuse :)
Miaou@jlai.lu 8 months ago
It’s actually ub, so it could also find the cure to cancer
otter@lemmy.ca 8 months ago
Might help if you add some sample code
pelya@lemmy.world 8 months ago
Pro tip:
for(;;)
takes less to type thannwhile(true)
deegeese@sopuli.xyz 8 months ago
My programs cause enough tears on their own without having to put them in the code itself.
Joey@programming.dev 8 months ago
#define EVER (;;)
GammaGames@beehaw.org 8 months ago
Nothing
plistig@feddit.de 8 months ago
A semicolon ends a statement, and semicolon is a statement on its own. One that does nothing. That’s why you can write
int i; for (i = 0; i < 3; i++);
to seti = 3
. You can use that pattern to find something in an iterator, etc. But I would preferint i = 0; while (i < 3) { i++; }
for readability.octoperson@sh.itjust.works 8 months ago
Your less thans got HTML-escaped into < and I spent embarrassingly long trying to figure out what pointer magic you were trying to demonstrate
plistig@feddit.de 8 months ago
Yeah, both Voyager and the normal lemmy web client escape the less-than sign. I tried it twice on both clients.