GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Processing
  • Download
  • Reference
  • Environment
  • Libraries
  • Tools
  • Wiki
  • Tutorials
  • Examples
  • Books
  • Forum
  • Overview
  • People
  • Privacy

This view

  • Original page
  • New address

GeezerVison is showing processing.org — ?: (conditional) / Reference

Reference+
Documentation→Reference→Control→Conditionals

Name

?: (conditional)

Description

A shortcut for writing an if and else structure. The conditional operator, ?: is sometimes called the ternary operator, an operator that takes three arguments. If the test evaluates to true, expression1 is evaluated and returned. If the condition evaluates to false, expression2 is evaluated and returned.

The following conditional expression:

result = test ? expression1 : expression2

is equivalent to this structure:
if (test) {
result = expression1
} else {
result = expression2
 }

Examples

  • int s = 0;
    for (int i = 5; i < 100; i += 5) {
      s = (i < 50) ? 0 : 255;
      stroke(s);
      line(30, i, 80, i);
    }
    

Syntax

  • test ? expression1 : expression2

Parameters

  • testany valid expression which evaluates to true or false
  • expression1any valid expression
  • expression2any valid expression

Return

  • Variable, dependent on the datatype of the expressions

Related

  • if
  • else
Creative Commons License

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.