0
0
Embedded Cprogramming~10 mins

Simple state machine with switch-case in Embedded C - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare the state variable.

Embedded C
int state = [1];
Drag options to blanks, or click blank then click option'
A3
B1
C2
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting the state from 1 instead of 0.
Using an uninitialized variable.
2fill in blank
medium

Complete the switch statement to check the current state.

Embedded C
switch([1]) {
    case 0:
        // do something
        break;
    case 1:
        // do something else
        break;
}
Drag options to blanks, or click blank then click option'
Ainput
Bstate
Ccounter
Dflag
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable unrelated to the state machine in the switch.
Forgetting to put the variable inside the switch parentheses.
3fill in blank
hard

Fix the error in the state update line.

Embedded C
case 0:
    // transition to state 1
    state [1] 1;
    break;
Drag options to blanks, or click blank then click option'
A=
B=>
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=' for assignment.
Using invalid operators like '=>'.
4fill in blank
hard

Fill both blanks to complete the state machine loop and switch.

Embedded C
while([1]) {
    switch([2]) {
        case 0:
            // do something
            state = 1;
            break;
        case 1:
            // do something else
            state = 0;
            break;
    }
}
Drag options to blanks, or click blank then click option'
A1
Bstate
C0
Drunning
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 in the while condition which stops the loop immediately.
Using a variable other than 'state' in the switch.
5fill in blank
hard

Fill all three blanks to add a default case and update the state machine.

Embedded C
switch(state) {
    case 0:
        // action for state 0
        state = [1];
        break;
    case 1:
        // action for state 1
        state = [2];
        break;
    default:
        // reset state
        state = [3];
        break;
}
Drag options to blanks, or click blank then click option'
A1
B0
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Setting wrong state transitions causing infinite loops.
Not handling the default case properly.