Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
Confluence
For you

Programming
Results will update as you type.
  • Tiling and Repetition
  • Reactors
  • Programming Basics: Parametric and Generative Graphic Design 2016
  • Archive
  • High Scores
  • Artificial Neural Network
  • Alternatives to the Processing IDE
  • p5.js Programming
  • Programming in Processing (java)
    • Starting with Processing (en)
    • Variables (en)
    • Classes and Objects (en)
    • Events und Functions (en)
    • Writing our own Functions (en)
    • Random Numbers (en)
    • Conditionals (en)
      • Bedingungen (de)
    • Loops (en)
    • Nested Loops (en)
    • Coordinates (en)
    • Arrays and Lists (en)
    • SVG + Images (en)
    • Motion and Temporality
    • Gesture Interactions
    • Using bitmaps as modifiers
    • Vectors
    • Animation
    • Animation 2
    • Simple Collision Detection
    • Sound
    • Typography

    Two hearts overlapped with each other
    Welcome back
    Catch up on the top discussions and highlights from your team.
    /
    Conditionals (en)

    Conditionals (en)

    Oct 28, 2020

    Analytics

    Loading data...

    deutsche Version

    Conditions in a programming language are instructions that direct the flow of a program. This works a bit like rail traffic: a train travels on a rail until it hits a switch, then the train changes its direction of travel. Conditions, therefore, change the course of a program. But how is the direction actually decided? The answer to this is logical operations based on the Boolean logic (remember the logic gates exercise?) . We use logical operations in everyday life, here are some examples:

    • When I'm hungry or thirsty, I go to the kitchen and get something
    • If I have enough money and I have time, then I'll make a trip to India

    In a programming language the spelling is different but the logic behind it remains the same.

    • equal to ==
    • not equal !=
    • greater than >
    • less than <
    • greater than or equal to >=
    • less than or equal to <=
    • logical And &&
    • logical Or ||


    In code it looks like this:

    int x = 10;
    if(x == 10)
    {
       println("x is the same as 10");
    }
    else
    {
       println("x is not the same as 10");
    }


    The code can almost be read out as intelligible language: If x is 10, write "x is equal to 10", if not then write "x is not equal to 10".

    The syntax of a condition is always:

    if([boolean expression])
     {
     // code that will be executed if the answer is true
     }
     else
     {
     // code that will be executed if the answer is false
     }


    Of course, there are also some variations of writing style:

    int x=10;
     
    // Conditional without brackets 
    // the conditional will run until the next semicolon
    if(x == 10)
    println("x ist gleich 10");
     

    If the first condition is false, we can test new condition with else if. We can keep doing this indefinitely.

    int x=10;
      
    // Here is a conditional with multiple cases
    // pay attention to the use of 'else if'
    if(x == 10) {
    	println("x is the same as 10");
    } else if(x == 9) {
    	println("x is the same as 9");
    } else if(x == 8) {
    	println("x is the same as 8");
    } else {
    	println("x is not 10,9 or 8");
    }


    For the multiple alternative cases, there is also the switch statement:

    int x=10;
    switch(x)
    {
    case 10:
    println("x is the same as 10");
    break;
    case 9:
    println("x is the same as 9");
    break;
    case 8:
    println("x is the same as 8");
    break;
    default:
    println("x is the same as 10,9 or 8");
    break;

    Exercise 5

    Program a drawing app. The colour can be changed with the buttons '1' - '5'. The left mouse button draws and the right mouse button erases the drawing.


    A possible solution to exercise 5: 

    color myColor = color(255, 0, 0);// red by default
    
    void setup()
    {
      size(600, 600);      // def. window size
      noStroke();
      background(255);
    }
    
    void draw()
    {
      if (mousePressed && mouseButton == LEFT) {
        fill(myColor);
        ellipse(mouseX, mouseY, 15, 15);
      }
        if (mousePressed && mouseButton == RIGHT) {
        fill(255); // white
        ellipse(mouseX, mouseY, 15, 15);
      }
      
    }
    
    void keyPressed()
    {
      switch(key)
      {
      case '1':
        myColor = color(255, 0, 0);// red 
        break;
      case '2':
        myColor = color(0, 255, 0);// green 
        break;
      case '3':
        myColor = color(0, 0, 255);// blue 
        break;
      case '4':
        myColor = color(0, 0, 0);// black 
        break;
      case '5':
        myColor = color(255, 255, 0);// yellow 
        break;
      default:
        println("wrong key");
        break;
      }
    }
    


    , multiple selections available,

    Related content

    Loops (en)
    Loops (en)
    Programming
    More like this
    Classes and Objects (en)
    Classes and Objects (en)
    Programming
    More like this
    Arduino Basics (en)
    Arduino Basics (en)
    Electrical Engineering
    More like this
    Programming Basics 2020
    Programming Basics 2020
    Creative Coding
    More like this
    Programming
    Programming
    Programming
    More like this
    p5.js Variables
    p5.js Variables
    Programming
    More like this
    {"serverDuration": 52, "requestCorrelationId": "130eece9e9e345dab4d8621e87d36210"}