Controllo dell’esecuzione


Operatori di relazione


Controllano se sussiste una certa relazione tra il valore a sinistra e il valore a destra

minore di

>= maggiore di o uguale a

== uguaglianza

<= minore di oppure uguale a

maggiore di

!= non uguaglianza


Operatori logici


|| OR logico

&& AND logico

Confrontano due espressioni logiche e restituiscono true oppure false

NOT logico

Opera su un’espressione logica e e restituisce true oppure false


Iterazioni


for

Controls a sequence of repetitions.
A for structure has three parts: init, test, and update. Each part must be separated by a semi-colon “;”.
The loop continues until the test evaluates to false.
When a for structure is executed, the following sequence of events occurs:

  1. The init statement is executed
  2. The test is evaluated to be true or false
  3. If the test is true, jump to step 4. If the test is False, jump to step 6
  4. Execute the statements within the block
  5. Execute the update statement and jump to step 2
  6. Exit the loop.

Sintassi

for (init; test; update) { statements }

Parametri

init, statement executed once when beginning loop
test, if the test evaluates to true, the statements execute
update, executes at the end of each iteration
statements, collection of statements executed each time through the loop

while

Controls a sequence of repetitions.
The while structure executes a series of statements continuously while the expression is true.
The expression must be updated during the repetitions or the program will never “break out” of while.
This function can be dangerous because the code inside the while() loop will not finish until the expression inside while() becomes true.
It will lock out all other code from running (mouse events will not be updated, etc.)
So be careful because this can lock up your code (and sometimes even the Processing environment itself) if used incorrectly.

Sintassi

while (expression) { statements }

Parametri

expression, a valid expression
statements, one or more statements


Selezioni


if

Allows the program to make a decision about which code to execute.
If the expression evaluates to true, the statements enclosed within the block are executed and if the expression evaluates to false the statements are not executed.

Sintassi

if(expression) { statements }

Parametri

expression, any valid expression that evaluates to true or false
statements, one or more statements to be executed

else

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

Sintassi

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

Parametri

expression, any valid expression that evaluates to true or false
statements, one or more statements to be executed

switch

Works like an if else structure, but switch() is more convenient when you need to select between three or more alternatives.
Program controls jumps to the case with the same value as the expression.
All remaining statements in the switch are executed unless redirected by a break.
Only primitive datatypes which can convert to an integer (byte, char, and int) may be used as the expression parameter.
The default is optional.

Sintassi

switch(expression)
{
case label: statements
case label: statements
default: statements
}

Parametri

expression, byte, char, or int
label, byte, char, or int
statements, one or more statements to be executed