Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the state variable.
Embedded C
int state = [1]; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Starting the state from 1 instead of 0.
Using an uninitialized variable.
✗ Incorrect
The initial state is usually set to 0 to start the state machine.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
The switch statement should check the current state variable to decide the action.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '=' for assignment.
Using invalid operators like '=>'.
✗ Incorrect
Use the assignment operator '=' to update the state variable.
4fill in blank
hardFill 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'
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.
✗ Incorrect
The loop runs while true (1), and the switch uses the state variable.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting wrong state transitions causing infinite loops.
Not handling the default case properly.
✗ Incorrect
State 0 transitions to 1, state 1 transitions back to 0, and default resets to 0.