Python 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.
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i < 5 : 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.
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i >= 5 : 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.
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i < 5 : IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0) else : 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.
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i < 5 : IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0) elif i < 7 : IPoint(i*10, j*10, 10).clr(1, 1-j*0.1, 0) else : 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.
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i < 5 : IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0) elif i < 7 : IPoint(i*10, j*10, 10).clr(1, 1-j*0.1, 0) elif i < 9 : IPoint(i*10, j*10, -20).clr(1, j*0.1, 1) else : IPoint(i*10, j*10, -10).clr(0, j*0.1, i*0.1)
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i == 5 : IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0)
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i != 5 : IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0)
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i < 5 and j < 3 : IPoint(i*10, j*10, -20).clr(i*0.1, j*0.1, 1) else : 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.
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i < 5 or j < 3 : IPoint(i*10, j*10, -20).clr(i*0.1, j*0.1, 1) else : 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.
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if not(i < 5 or j < 3) : # same with i>=5 and i>=3 IPoint(i*10, j*10, -20).clr(i*0.1, j*0.1, 1) else : 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.
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i % 2 == 0 : IPoint(i*10, j*10, 20).clr(0, 1., 1.) else : IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0)
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if i % 4 == 0 : IPoint(i*10, j*10, 20).clr(0, 1., 1.) elif i % 4 == 1 : IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0)
add_library('igeo') size( 480, 360, IG.GL ) for i in range(10) : for j in range(10) : if ( i + j ) % 2 == 0 : IPoint(i*10, j*10, 0).clr(i*0.1, j*0.1, 0) # this condition is same with # if i%2==0 and j%2==0 or i%2==1 and j%2==1 :
This expression of ( i + j ) % 2 == 0 is useful for setting up diagonal grids.