home processing download documents tutorial python tutorial gallery source about
 Python 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.

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.

  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.

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.

  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.

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.

  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.

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)


     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.

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)


     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.

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.


     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.

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.


(back to the list of tutorials)

HOME
FOR PROCESSING
DOWNLOAD
DOCUMENTS
TUTORIALS (Java / Python)
GALLERY
SOURCE CODE(GitHub)
PRIVACY POLICY
ABOUT/CONTACT