Osion kuvaus

  • Flow control

    In numerous cases we need to do things conditionally, a number of times, or until something happens. For those sort of purposes we have different flow control statements:

    • if – do something if a condition is true
    • if-else – do something if a condition is true, otherwise do something else
    • for – repeat something a certain number of times. Consists of the initial counter value, condition and increment
    • while – repeat something as long as a condition is true. Quite rare in Processing because of the way interactive sketches work, but common in other languages and environments.

    Flow control

    Today we'll start with if and if-elseThese statements usually start a compound statement, which is a block of code enclosed in braces. Anything you put in there will be run or repeated, according to the statements above. You can also nest the compound statements, placing them inside each other.


    if(a>1000000)
    
    {
    println("It is a big number");
    }
    else
    {
    println("It is only a modest number"); }

    Comparison operators (vertailuoperaattorit)

    All of the above need conditions. Comparison operators are used for comparing two things, such as numbers or variables. Some of the most common ones we’ll use in the future are:

    • Equal: == (very easy to mix up with assignment, beware!)
    • Not equal: !=
    • Greater than: >
    • Greater than or equal: >=
    • Less than: <
    • Less than or equal: <=


    Homework

    Create a sketch that draws yellow bubbles on the left side of the screen and green on the right. On the upper half of the screen the bubbles should be small and on the lower large. Like here:


    Task

    Looks like a case for some if or if-else magic.

    Example solution: yellowandgreen.pde