Recap of If Statements
What We’ve Covered So Far
What an If Statement Looks Like:
if (condition)
{
statement
}
What an If Else Statement Looks Like:
if (condition)
{
statement1
}
else
{
statement2
}
What Nested Else Statements Looks Like:
if (condition1) // first if statement
{
statement1 // code runs when first if statement is true
if (condition2) // second if statement
{
statement2 // code that runs when both if statements are true
} // end of second if statement
} // end of first if statement
What Conditions Look Like (And Logical Operators):
These are the parts of the if statements that live inside the parentheses. Sometimes conditions will be one or more boolean variables, but often they look like equations. Below are the logical operators that may be present in the equation conditions.
== equals:
Checks to see if both sides of the equation are equal. If they are equal this logical operator returns true.
!= does not equal:
Checks to see if both sides of the equation are NOT equal. If they not are equal this logical operator returns true.
< less than:
Checks to see if the value on the left side of the equation is less than the value on the right side of the equation. If it is this logical operator returns true.
<= less than or equal to:
Checks to see if the value on the left side of the equation is less than or equal to the value on the right side of the equation. If it is this logical operator returns true.
> greater than:
Checks to see if the value on the left side of the equation is greater than the value on the right side of the equation. If it is this logical operator returns true.
>= greater than or equal to:
Checks to see if the value on the left side of the equation is greater than or equal to the value on the right side of the equation. If it is this logical operator returns true.
How to Use Multiple Conditions:
&& and:
Only if all of the conditions joined together by this operator are true will this logical operator will return true.
|| or:
If just one of the conditions joined together by this operator are true will this logical operator will return true.
Next Step:
Next we’ll take a look at while statements. They’re sort of like if statements, except they keep happening over and over again until the answer to the condition is false.