I just learned if statements like 3 days ago and was just messing around and came up with this.But than when i type 2 it said thank you or and other number..it should only say thank you for number 1 please help.thanks
I know i can do it with switch statements but i only wanted to use if statements so i can learn
Thanks
This is a mistake a lot of people make when first using if statements. The reason that it always says "Oh thank you." is that you're using = instead of ==. = assigns a value to a variable so your first condition (if (val = 1) ) will always be executed. == compares two values to see if they are equal. change if (val = 1) and elseif (val = 2) to if (val == 1) and elseif (val == 2).
I must say that the program works, however, with the exception of the else statement, that in case you are using (=) instead of (==) returns random results.
I must say that the program works, however, with the exception of the else statement, that in case you are using (=) instead of (==) returns random results.
I'm not quite sure what you're trying to say. Having:
if (val = 1)
does not cause any random results. The statement val = 1 always evaluates to 1, and since 1 is non-zero, it is always considered true in a conditional statement. Therefore, the first conditional block in the posted code will always execute, and the other blocks (the elseif and the else) will never execute.
Nothing random about it at all. It's well-defined behaviour.