## Conditional Logic and Control

In a way, computational processes model (or in some cases, mimic) decision-making processes. Mathematical decision-making is fundamentally based on conditional logic, on the comparison of data’s values and types, then responding to the results of those comparisons. It’s easy for a computer to test for things like the equality of two values.

### Booleans

Conditional logic is based on “boolean” logic, named after George Boole, who defined a logical algebraic system in the mid 19th Centry. In Processing, we use boolean values (having the value *true* or *false*).

```
boolean johnnyIsADullBoy = true;
boolean johnnyBeGood = false;
```

Boolean variables are the simplest and most light-weight data type. They are really binary variables, that contain a single bit, either 0 or 1. Most languages have keywords that make dealing with booleans more English-like. In Processing, they are false (for 0) and true (for 1).

There is an entire system of binary arithmetic familiar to all computer scientists and electrical engineers. In binary, 1+1=10, 10+1=11, and 11+1=100. At the lowest level, computers only work in binary, and their internal mechanisms are combinations of transistors that perform addition, multiplication, basic logic operations, etc.

### Expressions That Evaluate to Booleans

Like the boolean addition above, there are situations where a statement in Processing can evaluate to true or false, instead of an integer, like 2 + 2 does. These form the basis for conditional statements.

But to us at this point, booleans are important because we can test whether one is true or false.

### Conditional Syntax

Conditional statements are those that depend on a boolean value, whether an expression is true or false.

The syntax for every conditional follows one of two templates:

```
if ( expressionIsTrue ) {
// Do something here.
}
if ( expressionIsTrue ) {
// This code is executed if the expression is true.
} else {
// Otherwise do this code here.
}
```

Conditionals are more commonly called “if-statements.” They always start with “if”, have a set of parentheses for the expression that evaluates to a boolean, and at least one code block. The ‘else’ block is optional and is executed if the expression in parens is false, or not true.

There is also a shorthand for the first template:

```
if ( expressionIsTrue ) executeJustThisFunction();
if ( expressionIsTrue )
executeJustThisFunction();
if ( expressionIsTrue )
doThis();
else
doThat();
```

You can omit the braces { } if the statement that depends on the conditional is just one line (one semicolon).

### Comparisons

The easiest boolean expressions are the comparisons.

Comparisons follow the same pattern as in mathematics. We can compare two numbers to check if one is greater than, less than, or equal to the other.

#### Comparison Operators

Assume x and y are integers:

x == y | equal to | evaluates to true if x and y have the same value |

x != y | not equal to | evalues to true if x and y have a different value |

x > y | greater than | evaluates to true if x is greater than y |

x < y | less than | evaluates to true if x is less than y |

x >= y | greater than or equal to | evaluates to true if x is greater than or equal to y |

x <= y | less than or equal to | evaluates to true if x is less than or equal to y |

x != y | does not equal | evaluates to true if x and y do not contain the same value |

The ones that differ most from mathematical use are == and !=, which are comparison operators. A single equal sign, =, is an assignment operator, that assigns a value to a variable.

#### Sample Code for Comparison Operators

```
int x = 1;
int y = 2;
println( "Is x greater than y?");
println( x > y );
println( "Is x less than y?" );
println( x < y );
println( "Is x equal to y?" );
println( x == y );
println( "Does x not equal y?" );
println( x != y );
println( "Is the sum of x and y more than 3?" );
println( x+y > 3 );
// Addition happens first.
```

### Conditionals + Comparisons

Finally, we can encode conditional simple rules.

The comparison expressions on the previous page are placed inside the parentheses of the “if-statements” like so:

```
int x = 5;
int y = 6;
if( x > y ) {
println( "ack!" );
} else {
println( "whatever." );
}
```

Note: The first part of this conditional is read aloud: “If x is greater than y then print line …”

#### The Classic Bouncing Ball

This example is rather mundane though.

```
float ballX;
float ballY;
float speedX;
float speedY;
float ballR = 20;
void setup(){
size( 700, 400 );
smooth();
fill(0);
ballX = width/2;
ballY = height/2;
speedX = random(-10,10);
speedY = random(-10,10);
}
void draw(){
background(255);
ellipse( ballX, ballY, ballR, ballR );
ballX += speedX;
ballY += speedY;
// Uncomment to add pseudo-gravity.
//speedY += 0.1;
// If the ball's edge reaches a boundary,
// make it bounce by reversing the speed.
if( ballX-ballR < 0 ) speedX = -speedX;
if( ballX+ballR > width ) speedX = -speedX;
if( ballY-ballR < 0 ) speedY = -speedY;
if( ballY+ballR > height ) speedY = -speedY;
}
```

### More Boolean Logic

Sometimes conditions are conflated and more complex than just a single true or false.

The study of logic gives us a way of making more complex conditions. These mechanisms are what we need to start applying constraints.

#### AND

If two comparisons are required for a condition, we can use an AND operator to make sure that both conditions have to be true for the whole condition to be true:

“If x is less than y AND x is greater than Z … "

In Processing-speak:

```
if ( x < y && x > z ) {
// Do something here.
}
```

If we have two conditions A and B (that evaluate to either true or false), here is the “truth table” for AND:

| A | B | A and B (A && B) |

| false | false | false |

| false | true | false |

| true | false | false |

| true | true | true |

#### OR

If either of two comparisons causes a condition, we can use an OR operator to check for the true of one or the other:

“If x is less than -5 OR x is greater than 5 … "

In Processing-speak:

```
if ( x < -5 || x > 5 ) {
// Do something here.
}
```

If we have two conditions A and B (that evaluate to either true or false), here is the “truth table” for AND:

| A | B | A or B (A || B) |

| false | false | false |

| false | true | true |

| true | false | true |

| true | true | true |

#### NOT

This simply changes a condition from true to false, or false to true.

boolean theSkyIsGreen = false;

if ( !theSkyIsGreen ) {

println( “Whew. That was close.” );

}

### Bouncing Ball With Paddle

```
float MAX_SPEED = 5;
float ballX;
float ballY;
float speedX;
float speedY;
float ballR = 20;
float paddleWidth = 100;
float paddleTop;
void setup(){
size( 700, 400 );
smooth();
ballX = width/2;
ballY = ballR + 10;
speedX = random(-MAX_SPEED,MAX_SPEED);
speedY = random(-MAX_SPEED,MAX_SPEED);
paddleTop = height-20;
fill(0);
}
void draw(){
background(255);
ellipse( ballX, ballY, ballR, ballR );
rect( mouseX-paddleWidth/2,
paddleTop, paddleWidth, 10 );
ballX += speedX;
ballY += speedY;
// Uncomment to add pseudo-gravity.
speedY += 0.1;
if( ballX-ballR < 0 ) speedX = -speedX;
if( ballX+ballR > width ) speedX = -speedX;
if( ballY-ballR < 0 ) speedY = -speedY;
if( ballY+ballR > paddleTop &&
( ballX > mouseX-paddleWidth/2 &&
ballX < mouseX+paddleWidth/2 ) )
speedY = -speedY;
}
```