Week 3

Ch. 3 Expressions and Interactivity + Ch. 4 - Making decisions

- if statement

Syntax:

if ( expression ) {
  // block of code
}

if the expression is TRUE → execute the block of code, otherwise skip the block

- if, else statement

Syntax:
if ( expression ) {
  // block of code
}
else { 
  // block of code
}

if the expression is TRUE → execute the block of code with respect to if, otherwise → execute the block of code with respect to else.

- if, else if, else statement

Syntax:

if ( expression ) {
  // block of code
}
else if ( expression ) {
  // block of code
}
else if ( expression ) {
  // block of code
}
else {
  // block of code
}

if the expression is TRUE → execute the block of code with respect to if, otherwise → if the next expression is TRUE → execute the block of code with respect to the corresponding else if, otherwise → execute the block of code with respect to else.


- switch

Syntax:

switch( integer_expression* ) {
  case constant_expression_1:
    // line_statment;
    // line_statment;
    break;
  
  case constant_expression_2:
    // line_statment;
    // line_statment;
    break;
 
  default:
    // line_statment;
    // line_statment;
    break;
}

* can be a char_expression

Libraries

  • cmath - pow(b,e), sqrt(x), fabs(x)
  • iomanip - setprecision(n), showpoint, fixed

#defined constants