Skip to main content
Making more explicit
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Remove the semicolon at the end of your if statements.

That semicolon tells the compiler "I'm done with this" and whatever comes next is a new, unrelated statement

So this (note semicolon at the end of the if):

if (condition); { DoStuff(); } 

is the same as

if (condition) { ; // Empty statement, do nothing. } { // New block, unrelated to the prior if. DoStuff(); } 

so it says "check condition, but do nothing with the result. Then DoStuff()"

But this (note no semicolon before the opening braces):

if (condition) { DoStuff(); } 

Says "check condition, and only if it's true should you execute the stuff in the braces and DoStuff()"

Remove the semicolon at the end of your if statements.

That semicolon tells the compiler "I'm done with this" and whatever comes next is a new, unrelated statement

So this (note semicolon at the end of the if):

if (condition); { DoStuff(); } 

is the same as

if (condition) { ; // Empty statement, do nothing. } DoStuff(); 

so it says "check condition, but do nothing with the result. Then DoStuff()"

But this (note no semicolon before the opening braces):

if (condition) { DoStuff(); } 

Says "check condition, and only if it's true should you execute the stuff in the braces and DoStuff()"

Remove the semicolon at the end of your if statements.

That semicolon tells the compiler "I'm done with this" and whatever comes next is a new, unrelated statement

So this (note semicolon at the end of the if):

if (condition); { DoStuff(); } 

is the same as

if (condition) { ; // Empty statement, do nothing. } { // New block, unrelated to the prior if. DoStuff(); } 

so it says "check condition, but do nothing with the result. Then DoStuff()"

But this (note no semicolon before the opening braces):

if (condition) { DoStuff(); } 

Says "check condition, and only if it's true should you execute the stuff in the braces and DoStuff()"

Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Remove the semicolon at the end of your if statements.

That semicolon tells the compiler "I'm done with this" and whatever comes next is a new, unrelated statement

So this (note semicolon at the end of the if):

if (condition); { DoStuff(); } 

is the same as

if (condition) { ; // Empty statement, do nothing. } DoStuff(); 

so it says "check condition, but do nothing with the result. Then DoStuff()"

But this (note no semicolon before the opening braces):

if (condition) { DoStuff(); } 

Says "check condition, and only if it's true should you execute the stuff in the braces and DoStuff()"