There are actually two different if..else constructs. The first is a simple if-else:
if ( condition ) {
// Do stuff if condition is true
} else {
// Do stuff if condition is not true
}
The next is an if-else-if:
if ( condition ) {
// Do stuff if condition is true
} else if ( differentCondition ) {
// Do stuff if differentCondition is true
} else {
// Do stuff if neither condition nor differentCondition is true
}
You can also use else-if as many times as you like, e.g.:
if ( condition ) {
// Do stuff
} else if ( condition2 ) {
// etc
} else if ( condition3 ) {
// etc
} else if ( condition4 ) {
// etc
} else {
// etc
}
And every part of an if..else is optional, except for the initial if block. So if you don't want to do anything if condition
is not true, you can just omit the else block entirely:
if ( condition ) {
// do stuff if condition is true
}
HTH
Going beyond your question for a moment, the expression being evaluated in your if statement's condition is a bit wibbly-wobbly.
willOrderPackage will always be true
or false
, but it's important to note that true
and "true"
or false
and "false"
are different. The first is boolean, the second is a string.
So, your if statement should be asking:
if ( willOrderPackage == true ) {
Even better than that, when you evaluate an expression in an if statement, there's an implied == true
at the end of it which is invisible. For instance:
if ( willOrderPackage == true ) {
would be interpreted as :
if ( (willOrderPackage == true) == true )
The benefit of this is that you can actually omit the whole == true
bit from your code, so you can just write:
if ( willOrderPackage ) {
And effectively you're still saying "if willOrderPackage is true"
Hope that helps clear up a few things for you!