Previous Page
Next Page

Chapter 4 Quick Reference

To

Do this

Example

Determine whether two values are equivalent

Use the == or != operator.

answer == 42

Compare the value of two expressions

Use the <, <=, >, or >= operator.

age >= 21

Declare a Boolean variable

Use the bool keyword as the type of the variable.

bool inRange;

Create a Boolean expression that is true only if two other conditions are true

Use the && operator.

inRange = (lo <= number) 
      &&  (number <= hi);

Create a Boolean expression that is true if either of two other conditions is true

Use the || operator.

outOfRange = (number < lo) 
        ||  (hi < number);

Run a statement if a condition is true

Use an if statement.

if (inRange) 
  process();

Run more than one statement if a condition is true

Use a block.

if (seconds == 59) 
{ 
    seconds = 0; 
    minutes++; 
}

Associate different statements with different values of a controlling expression

Use a switch statement.

switch (current) 
{ 
  case '<':  
          ... 
          break;  
  default : 
          ... 
          break; 
} 

Previous Page
Next Page