Now that we know a little bit more about how to work with the switch conditional statements, it is time for us to move on to looking at the second kind of conditional statement, which is known as the if statement.
One of the most basic things that you can do with these statements as a whole is this one.
They are going to be based on the simple idea of true or false.
If the system decides that the input it gets is going to match up to the conditions that you are able to set, then the condition is true, and the program will be able to run the part that you have set up here.
We need to look at an example of how this is going to work.
Let’s say that you set up a system that is going to ask the user what 2 + 2 is.
The user then puts in the right answer of 4 here.
This would be seen by the compiler as true, and then the message would be able to pick out a statement based on what you add there, like “That is Correct!” and then the message is going to show up on your screen.
Any time that you go through the process of having the user add in the necessary input, there is some risk, though.
If they work through this and add in an input that meets your conditions, then the code will proceed with the statement, or whatever else you add to that part of the code.
But there are also times when the user could add in the wrong answer.
Going back to our previous example of asking the user what 2 + 2 is, if they answer with 5, then the compiler will see that this is not the right answer, and the number is not going to be able to match up to the conditions you set, resulting in a false.
Since we are working with the if statements here and they are seen as the most basic out of all the conditional statements that are out there, you may find that this is not going to have a method to help answer for the false input here.
If the user gets the problem above and puts in any answer that is not 4, then the if statement is going to see this as being false, and then the screen is going to be left blank since nothing can happen.
This is going to turn into a problem for most of the codes that you want to write out, so we can imagine that it is not often that you will work with the if statement.