C# is not Python. Lining an else
up with if
doesn't make that else
go with that if
. You need braces ({
and }
) to surround a block if you don't want the if in that block to get connected with the else after that block. You also need to make sure you always set the Text
property even when the inner condition of an if
block doesn't pass.
I would also modify your logic a bit so you don't have to repeat code:
if ((season == 1 || season == 2) && job == 1)
label3.Text = "There is a 20% discount on the exterior job";
else if (season == 3 && job == 2)
label3.Text = "There is a 30% discount on the interior job";
else
label3.Text = "No discount, regular prices apply";
Of course, in the above I didn't put braces, even though I said you needed them in your original code. In fact, I personally prefer to always use braces, but the above is more consistent with your original formatting.
(In fact, if you look at any of my other posted answers with code, I doubt you'll find even a single example of an un-braced block of code following a flow-control statement, even when that block is just a single statement. If you find any, it will be like above where I'm intentionally just trying to match the original question's code.)
The difference between the two is that the braces designate a block of C# statements, which are treated as a single statement for the purpose of flow-control statements like if
, else
, for
, while
, etc. To fix your original code with braces would have required putting the inner if (job == 1)
part inside the braces, and the else
outside of them. That way, that inner if
would be included in the entire block defined by the braces and not eligible to be matched up with the subsequent else
(because branching/looping statements can't span code blocks defined by braces).
In the above, there is no inner if
. There's just the single statement that is executed when the if
condition is true
. So the subsequent else
, matching the nearest eligible if
statement, works correctly.
Again: braces are only required when you have some block of statements that has to be made separate from the flow-control statements around it. It is fine to have just a bare statement if there's only one, and it's not an if
that's going to get incorrectly matched with a following else
without braces.
But, they may be used in other cases, even when not strictly necessary. And many people, myself included, feel it's better to use them after every flow-control statement to define the block you want controlled by that statement, even if not necessary.
The reason to do this is specifically to prevent the problem you ran into. By being explicit about which blocks of code go with which flow-control, you avoid such problems. Note that this isn't just something for novice programmers. Even experienced programmers can accidentally create bugs in the same way you did. It typically happens for a different reason: the code was originally fine with a single brace-less statement, but at a later date someone had to modify the code to add some logic. When they do this, they fail to notice the structure of the code and break the flow-control logic that was intended. Sometimes it's just that a statement winds up outside the flow control, other times it's more like your scenario where an else
winds up matched with the wrong if
. Either way, it's bad news and easy enough to avoid simply by always including the braces.