Using Conditional Statements
Conditional statements can be used to take actions depending on whether various conditions evaluate to true or false. Grouping using parentheses ( and ) can be used for more complex conditions.
Condition |
Description |
---|---|
Equals | expression = = expression |
Not Equals | expression != expression |
Greater Than | expression > expression |
Greater Than or Equal To | expression >= expression |
Less Than | expression < expression |
Less Than or Equal To | expression <= expression |
Not condition | !condition |
And | condition && condition |
Or | condition || condition |
If Statement
if(condition)
{
actions to take if condition is true
}
Brackets are needed only if more than one statement is run after the "if."
If-Else If Statements
if(condition)
{
actions to take if condition is true
}
else if(condition)
{
actions to take if condition is true
}
else if...
if(SideLength != NaN)
{
AreaOfPolygon=
((SideLength^2)*NumberOfSides)/
(4*Tan(pi/NumberOfSides));
}
else if(Radius != NaN)
{
AreaOfPolygon=
(Radius^2)*NumberOfSides*Sin((2*pi)/NumberOfSides)/2;
}
One or more else if statements can be specified. Brackets are needed only if more than one statement is run after the "if-else- if-else."
Else-If Statement
if(condition)
{
actions to take if condition is true
}
else if(condition)
{
actions to take if condition is true
}
else if...
else
{
actions to take if no conditions are met
}