Conditional statements are statements that help coders execute a code based on certain conditions of a code. There are 3 types of conditional statements: if; if/else; if/else-if/else, form which you can view below. think of conditions as checks, where it'll check something and do something based on what condition that thing is IN.
The best way to describe the ternary operator which is ? is that it's a more efficient way to execute the if-else statement. It woild store teh result of the if statemnt in a single variable.
The output below would be "It's true!".
The output below would be nothing.
The if statement is the simplest one out of the bunch. The statement will look as follows if (conditional), in the brackets you put your condition. The if statement will execute the code you put inside the curly brackets ONLY if that condition is true. If not, the code won't execute.
Think of it this way, IF something is true, then the code will execute. IF a stop light is red, my car will STOP.
The output below would be "It's true!".
The output below would be "Not true!".
The if/else statement uses the same philosophy as the if statement. IF the statement is true, the code associates with it will execute, and the else statement will be ignored. This time IF the code is false, it will automatically execute the code under the else statement.
Under your if statement, before you need it add the statement else, and below add your code.
Think of it this way, IF the time is 12:00, I go outside, if it isn't then I'll do work.
The output below would be "It's equal!".
This is used in situations where there are multiple conditionals to check. Sometimes one if statement won't be enough and creating more for multiple checks is a pain. This is where else if statements come into play. They are a collection of if statements that will be checked individually until one is true, then it stops the code. The other statements not checked will be ignored.
To create, after your if statement add a statement else if(), in the brackets add your conditional and below add the code you want to execute IF that condition is true.
Think of it this way, IF it's cold outside, I need to wear a jacket, IF it's hot outside I need to wear shorts. IF it's warm outside need to wear a sweater. I check if it's hot, it isn't, I check if it's cold, IT IS! So I wear my jacket, ignoring to check if it's warm or not, because I don't need to.
IF both items are true it returns true, if one of them isn't, it returns false.
IF either the first item or second item is true, it returns true. IF none are true, it returns false.
It acts as a inverter, it will reverse the value of a Boolean expression. If the item inside of it is true it'll revert to false. If false, it'll revert to true.