Tutorials | (back to the list of tutorials) |
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.
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.
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.
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); } } }
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); } } }
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.
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.