CpSc 130 : Introduction to Programming and Information Systems

CpSc 130 Notes Conditionals


If statements
if ( someTFexpression )
   trueStatement ;

Simplest conditional. The trueStatement is only executed if the condition is true (or the answer is yes).
Example: "If the fire alarm is sounding, exit the building."

if ( someTFexpression )
   trueStatement ;
else
   falseStatement ;

An if-else conditional. The trueStatement is executed if the condition is true (or the answer is yes), otherwise the falseStatement is executed. One of these is always executed. Never both.
Example: "If you are late for class, hurry. Otherwise, take your time."

if ( someTFexpression;
   trueStatement ;

A common error — an errant semicolon. The semicolon ends the true part of the if statement with an unexpected result... the trueStatement is always executed.

As far as JS is concerned, you said if someTFexpression is true, do nothing. Then continue with the next statement. (Indentation is for people. JS ignores it.)


Terms
if
A conditional statement. The expression provided is tested at run time to see if it is true or false (think of the expression as an assertion). Only if the expression is true is the statement executed.
if-else
A conditional statement with two options, one of which will always execute. If the supplied expression is true, the trueStatement is executed. Otherwise, the falseStatement is executed. Both expressions cannot be executed.
nested ifs
Any statement may be used inside the if as trueStatement and falseStatement, including another if.
switch
The switch statement may be used to replace a sequence of nested if statements that all use an equality test. It is generally considered more readable, but it is not more efficient (despite what the author says). You can always use an if. Syntax...
compound statement
Any single statement in JS may be replaced with a compound statement. A compound statement consists of zero or more JS statements enclosed in matching curly braces ({ }). The opposite is not true. You have seen these repeatedly in the functions we have written. Functions require a compound statement.
equality ==
We test if two values are equal using the == equality operator. Note the visual similarity to the = assignment operator.
inequality !=
We test if two values are not equal using the != inequality operator
other comparisons
Other conditions we can test for include:
<    less than
<=  less than or equal to
>    greater than
>=  greater than or equal to
negation
The logical (Boolean) negation operator is ! and it can be applied to any T/F (Boolean) expression or variable. It means that if the value is true, make it false. If the value is false, make it true. Negation is a unary operator, as it applies to a single value. All of the others are binary operators.
comments
Comments are ignored by JS. You have seen lots of examples of the inline comment. This comment begins with // and ends at the end of the line (where you hit the Enter key). Longer, block comments are analogous to the comments in HTML (<!--   -->). In JS, they begin with /* and end at the first occurrence of */ anywhere in the JS code. They are very useful for debugging.

Empty (Null) Statement

Either trueStatement or falseStatement (or both) may be empty statements indicating that nothing should be done for that condition. An if without an else is essentially such a statement where the falseStatement is empty. There are two ways to express an empty statement. The first uses the semicolon, the second a compound statement. Making both empty is a semantic error.

Note that this explains the error caused by the errant semicolon described above!

if ( someTFexpression )
   ; // do nothing
else
   falseStatement  ;
   
if ( someTFexpression )
   trueStatement  ;
else
   ; // do nothing
if ( someTFexpression )
   { } // do nothing
else
   falseStatement  ;
   
if ( someTFexpression )
   trueStatement  ;
else
   { } // do nothing

Variables as a TF Expression

Some variables are of type Boolean and hold true/false values. They can be used in place of a logical expression in an if statement. But, what about numbers and strings? A number is considered to be false if the value is zero. A string is considered to be false if it is an empty string (""). Anything else is considered to be true.


Switch Statement Syntax

switch ( varUsedToDecide ) {
  case firstValue :
    // statements for varUsedToDecide == firstValue
    break;
  case secondValue :
    // statements for varUsedToDecide == secondValue
    break;
  // any number of cases...
  default:
    // statements for when none of the cases match
  }

Keywords are switch, case, break and default. The parenthesis and curly braces are required, as are the colons following each case.

You may have any number of cases.

The default is always last and covers "anything else".

If you leave out a break in (for example) the first case, code for the second case will also be executed. In fact, it will continue executing code until a break is encountered. The break means "I'm done, leave the switch statement now."


Examples & Discussion


It's Not Always About Equality


More Samples