0
0
Cprogramming~10 mins

Switch statement - Interactive Code Practice

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

Complete the code to start a switch statement on the variable choice.

C
switch([1]) {
    case 1:
        printf("One\n");
        break;
    default:
        printf("Other\n");
}
Drag options to blanks, or click blank then click option'
Avalue
Boption
Cinput
Dchoice
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name that is not declared.
Forgetting the parentheses after switch.
2fill in blank
medium

Complete the code to print "Two" when the variable num equals 2.

C
switch(num) {
    case [1]:
        printf("Two\n");
        break;
    default:
        printf("Other\n");
}
Drag options to blanks, or click blank then click option'
A1
B4
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong number in the case label.
Forgetting the colon after the case value.
3fill in blank
hard

Fix the error in the switch statement to correctly handle the default case.

C
switch(option) {
    case 1:
        printf("One\n");
        break;
    default[1]:
        printf("Unknown\n");
}
Drag options to blanks, or click blank then click option'
A:
B;
C,
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using a semicolon after default instead of a colon.
Omitting the colon after default.
4fill in blank
hard

Fill both blanks to complete the switch statement that prints "Zero" or "Positive" based on num.

C
switch(num) {
    case [1]:
        printf("Zero\n");
        break;
    case [2]:
        printf("Positive\n");
        break;
    default:
        printf("Negative\n");
}
Drag options to blanks, or click blank then click option'
A0
B1
C2
D-1
Attempts:
3 left
💡 Hint
Common Mistakes
Using negative numbers for positive cases.
Forgetting the break statements.
5fill in blank
hard

Fill all three blanks to create a switch that prints the day name for day values 1, 2, or 3.

C
switch([1]) {
    case [2]:
        printf("Monday\n");
        break;
    case [3]:
        printf("Tuesday\n");
        break;
    default:
        printf("Other day\n");
}
Drag options to blanks, or click blank then click option'
Aday
B1
C2
D3
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in switch.
Mixing up case numbers.