Name

else

Description

Extends the if structure allowing the program to choose between two or more blocks of code. It specifies a block of code to execute when the expression in if is false.

Examples

  • size(400, 400);
    
    for (int i = 20; i < 380; i += 20) {
      if (i < 140) {
        line(120, i, 320, i);
      } else {
        line(80, i, 360, i);
      }
    }
    Image output for example 1
  • size(400, 400);
    
    for (int i = 20; i < 380; i += 20) {
      if (i < 140) {
        line(120, i, 320, i);
      } else if (i < 260) {
        line(80, i, 360, i);
      } else {
        line(0, i, 400, i);
      }
    }
    Image output for example 2

Syntax

  • if (expression) {
  • statements
  • } else {
  • statements
  • }
  • if (expression) {
  • statements
  • } else if (expression) {
  • statements
  • } else {
  • statements
  • }

Parameters

  • expressionany valid expression that evaluates to true or false
  • statementsone or more statements to be executed

Related