Tutorials (back to the list of tutorials)

## Simple Conditions

### If Condition

If condition creates a switch to change a path of control flow. It examines a given condition and if the condition is true, it executes the body of the if statement.

See the example below where an if condition is applied to limit the creation of points when the counter i is under 5.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i < 5 ){
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
}
}
```

Compare with the result of this previous code.
Points were created only when i was smaller than 5.
"<" is one of Boolean operations to examine if the number on the left is less than the number on the right of the operation.
In the same way, ">" examines if the left number is greater than the right.
"<=" examines if the left number is less than or equals to the right.
">=" examines if the left number is greater than or equals to the right.
Please be careful about the order of the letter with "=" in "<=" and ">=". "=" comes after "<" or ">".

To create an opposite condition of the code above, you use ">=" as bellow.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i >= 5 ){
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
}
}
```

This diagram on the right shows the conditional flow of the execution of if statements.

1. The condition i<5 is checked if it's true or false.
2. If the condition is true, it goes to step 3. If it's false, the body is skipped and it exits the if statement.
3. The body inside the parentheses { ... } is executed and it exits the if statement.

### If and Else

If and else statement switches the control flow to execute one body or anoter body.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i < 5 ){
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
else{
new IPoint(i*10, j*10, -10).clr(0, j*0.1, i*0.1);
}
}
}
```

The diagram on the right shows the conditional flow of if-else statement.

1. The condition i<5 is checked if it's true or false.
2. If the condition is true, it goes to step 3. If it's false, it goes to step 4.
3. The body inside the parentheses { ... } is executed and it exits the if-else statement.
4. The else body inside the parentheses { ... } is executed and it exits the if-else statement.

### If, Else If and Else

If, else if and else statement switches multiple paths of control flow.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i < 5 ){
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
else if( i < 7 ){
new IPoint(i*10, j*10, 10).clr(1, 1-j*0.1, 0);
}
else{
new IPoint(i*10, j*10, -10).clr(0, j*0.1, i*0.1);
}
}
}
```

This diagram shows the conditional flow of if-else if-else statement.

1. The condition i<5 is checked if it's true or false.
2. If the condition is true, it goes to step 3. If it's false, it goes to step 4.
3. The body inside the parentheses { ... } is executed and it exits the if-else if-else statement.
4. The condition i<7 is checked if it's true or false.
5. If the condition is true, it goes to step 6. If it's false, it goes to step 7.
6. The else if body inside the parentheses { ... } is executed and it exits the if-else if-else statement.
7. The else body inside the parentheses { ... } is executed and it exits the if-else statement.

You can put more than one else if in this control flow.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i < 5 ){
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
else if( i < 7 ){
new IPoint(i*10, j*10, 10).clr(1, 1-j*0.1, 0);
}
else if( i < 9 ){
new IPoint(i*10, j*10, -20).clr(1, j*0.1, 1);
}
else{
new IPoint(i*10, j*10, -10).clr(0, j*0.1, i*0.1);
}
}
}
```

### Boolean Operators

Other than "<", ">", "<=" and ">=", there are two more operator "==" and "!=". Those operators used for conditional examination are called Boolean operators.
"==" gets true when the left and the right values are equal.
"!=" gets true when the left and the right values are not equal.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i == 5 ){
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
}
}
```

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i != 5 ){
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
}
}
```

### And, Or, Not

There are another set of Boolean operator which combines other Boolean operators.
&& is "and" operator. And operator combines two Boolean expressions on the left and right and gets true only when both expressions are true.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i < 5 && j < 3 ){
new IPoint(i*10, j*10, -20).clr(i*0.1, j*0.1, 1);
}
else{
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
}
}
```

|| (double vertical bar) is "or" operator. Or operator combines two Boolean expressions on the left and right and gets true when either expressions is true.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i < 5 || j < 3 ){
new IPoint(i*10, j*10, -20).clr(i*0.1, j*0.1, 1);
}
else{
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
}
}
```

! is "not" operator. Not operator is put in front (left) of another Boolean expression and invert true/false of the expression.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( !(i < 5 || j < 3) ){ // same with i>=5 && i>=3
new IPoint(i*10, j*10, -20).clr(i*0.1, j*0.1, 1);
}
else{
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
}
}
```

Note that parentheses "(", ")" are used to set priority of expressions.

### Use of %

% is an operator to calculate remainder, which is the amount of left over when you divide two integers.
For example,
3 % 2 = 1
4 % 2 = 0
4 % 3 = 1
5 % 3 = 2
6 % 3 = 0

This operator is useful to make a periodic condition.

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i % 2 == 0 ){
new IPoint(i*10, j*10, 20).clr(0, 1., 1.);
}
else{
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
}
}
```

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( i % 4 == 0 ){
new IPoint(i*10, j*10, 20).clr(0, 1., 1.);
}
else if( i % 4 == 1 ){
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
}
}
```

```import processing.opengl.*;
import igeo.*;

size( 480, 360, IG.GL );

for(int i=0; i < 10; i++){
for(int j=0; j < 10; j++){
if( ( i + j ) % 2 == 0 ){
new IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0);
}
// this condition is same with
// if( i%2==0 && j%2==0 || i%2==1 && j%2==1 )
}
}
```

This expression of ( i + j ) % 2 == 0 is useful for setting up diagonal grids.

(back to the list of tutorials)