unconditional variable assignment after ""If""

I'm Used to Basic so there is a learning curve for me here. I know = assigns a value and == but how do I do a conditional assignment? in the following it assigns "val" regardless whether or not it meets the condition. if (val > 150); (val = 0);

If shouldn't have a semicolon.

if ( variable operator value ) {
     variable = new value;


if ( val > 150 ) {
     val = 0;


oh so the semi colon ends the if so then it treats the second line as a separate unconditional assignment thanks for that guys

oh so the semi colon ends the if

… yes. kinda. The semicolon is the empty statement. if. while, do while, and for control the execution of one other subordinate statement. This subordinate statement may be

  • an expression terminated with a semicolon,
  • an empty statement (semicolon b itself),
  • some other control statement (another if, a while a for, etc),
  • a list of statements enclosed in parenthesis.

Hi, I check that

any { should never be immediately PRECEEDED by a ;

Tom.. :)

Sparks32: if (val > 150); (val = 0);

Like this:

val = val > 150 ? 0 : val;