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 1 year ago by Albin7326@suppo.fi to programming@programming.dev
Comments
JakenVeina@lemm.ee 1 year ago
vext01@lemmy.sdf.org 1 year ago
Now slap some commas in there and you can have side effects in the loop header.
Guaranteed to confuse :)
Miaou@jlai.lu 1 year ago
It’s actually ub, so it could also find the cure to cancer
otter@lemmy.ca 1 year ago
Might help if you add some sample code
pelya@lemmy.world 1 year ago
Pro tip:
for(;;)
takes less to type thannwhile(true)
deegeese@sopuli.xyz 1 year ago
My programs cause enough tears on their own without having to put them in the code itself.
Joey@programming.dev 1 year ago
#define EVER (;;)
GammaGames@beehaw.org 1 year ago
Nothing
plistig@feddit.de 1 year 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 1 year 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 1 year ago
Yeah, both Voyager and the normal lemmy web client escape the less-than sign. I tried it twice on both clients.