If & If Else Statements

What are they? How do they work?

What do they do?

  • Use if to specify a block of code to be executed, if a specified condition is true
  • Use else to specify a block of code to be executed, if the same condition is false
  • Use else if to specify a new condition to test, if the first condition is false

The if Statement

  • The if statement specifies which block of code to be tested first, and what to output if it is true.
  • It is most typical to use a single if and an else to test if one condition is either true, or false.

Example

Big image

The if else Statement

  • The if else statement comes after the initial if statement, and specifies which block(s) of code to be tested in the event that the initial if statement returns a value of FALSE.
  • The if else Statement is most commonly used to test multiple conditions, rather than a true or false scenario.
  • Contrary to else statements, if else statements contain a condition.

Example

Big image