Question regard = or ==

So I've just started learning about the arduino and have completed project 2 from the handbook as below. My question is regarding ==/= in the if (switchState == LOW) bit of the code. When I change the == to = Can somebody please explain the difference and how this effects the if statement?

I was hoping if somebody could please explain why this is the case.

The if-else reference will answer your question.

Maybe tell us which one is the green light?

In that code, == will trigger the if block, and = will trigger the else block

Ah, is this explanation correct - the if( switchState = 0 ) is being evaluated as false (as the value is 0) and therefore the else statement is implemented conversly if I had said if (switchState = 1) then it is been evaluated as True (as the value is greater than 0)

I appreciate the help, thanks everyone!

NewProgrammer2018: Ah, is this explanation correct - the if( switchState = 0 ) is being evaluated as false (as the value is 0) and therefore the else statement is implemented conversly if I had said if (switchState = 1) then it is been evaluated as True (as the value is greater than 0)

Well the expression is an assignment so it sets the value of switchState to 0 or 1 and because the evaluation of a assignment is the value being assigned, the condition is seen (after modifying the value of switchState) either as if (0) {..} or if (1) {..} and that’s where Implicit conversions / Contextual conversions come into play. In a if a boolean expression is expected and thus a Boolean conversion happens and the spec states

Boolean conversions A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type bool. The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.

So your 0 becomes false and your 1 becomes true.

But you did not compare the value of switchState with 0 or 1, you actually modified it