Skip to content

Session 10: Control Flows

Conditions

truthy falsy
true false
1 0
25 !true

Operators

a > b a больше b
a < b a меньше b
a == b a равно b
a != b a не равно b
a >= b a больше либо равно b
a <= b a меньше либо равно b
(a) || (b) либо a, либо b
(a) && (b) a и b
i++ i=i+1
i-- i=i-1

if clauses

if(condition1)
{
    // идем сюда, если условие (condition1) выполняется
}
else if(condition2)
{
    // идем сюда, если условие (condition1) не выполняется, условие (condition2) выполняется
}
else if(condition3)
{
    // идем сюда, если условия (condition1, condition2) не выполняются, условие (condition3) выполняется
}
else
{
    // и сюда, если никакие условия не выполняются
}

Exercise: Analyze Code

int temperature;
void loop(){
    temperature = measureTemperature();
    if((temperature<0) || (temperature>40))
    {
        set_led_color(LED_1, 0, 0, 0);
    }
    else if(temperature<15)
    {
        set_led_color(LED_1, 0, 0, 255);
    }
    else if(temperature<25)
    {
        set_led_color(LED_1, 0, 255, 0);
    }
    else
    {
        set_led_color(LED_1, 255, 0, 0);
    }
}

led control flow

for loops

Base Loop

// x = 7 -> 8 -> 9 -> 10 -> 11
for(int i=7; i<12; i++){
    // ...
}

// x = 0 -> 1 -> 2 -> 3 -> 4
for(int i=0; i<5; i++){
    // ...
}

Exercise: Invert Loop

// x = 4 -> 3 -> 2 -> 1 -> 0
for(int i=__; i__; ___){
        // ...
    } 
Solution
for(int i=4; i>=0; i--){
    // ...
} 

Exercise: Analyze Loop

for(int i=0; i<10; i=i+2){
    // ...
} 
Solution x = 0 -> 2 -> 4 -> 6 -> 8

Practical Part

Write a program that is able to play multiple animations. Switch between animations with your remote control.

Include:

  • an icon (smiley / drawing)
  • an animated icon
  • moving text

Inspiration