A Conditional state if another type of logical statement. You have probibaly
seen these before in math class in the for of an if p then q statement. An
example of this could be p:I study for the exam
and q: I
get an A on the exam.
Then the p→q statement would be If I study
for the exam then I will get an A on the exam.
As you can see this would be a
true statement. But it gets a bit more tricky when not both statements are true. Here is a truth table for if p then q statements to help you understand.
As a matter of fact condidtion statements are extremely important in programmingand most programs contain at least on or two. We will see them in the for of If Then statements. These statements will run only when the If statement is true.
You can think of it as a door. If the statement is true then you open the door.
If the if statment is true the condtion inside the if statement aka the
thenn
statement will be preformed. An example of this would be if x=5 then
add 6 to x. If x were 5 then it will be increased by 6 but if x is not 5 then
the program will not run the if statement and it will be skipped. Lets see what
this will look like.
I bet you are wondering now, Well what happens if the statement isn't true,
will nothing happen at all?
Oh no that would be far too boring. This is why
we have something called If Else
statements. This makes it so when
the if statement is false then the complier will next run the else statement.
An example of this would be, If x=5 then add 5 to x. Else add 9 to x
So only if x is 5 then 5 will be added to x. But if that is not true and x is
7 then 9 will be added to x.