#Switch Statement Help

18 messages · Page 1 of 1 (latest)

worn wind
#

How does this code output "Well done" even though there is no statement underneath the case B?

   char grade = 'B';

   switch(grade) {
      case 'A' :
         printf("Excellent!\n" );
         break;
      case 'B' :
      case 'C' :
         printf("Well done\n" );
         break;
      case 'D' :
         printf("You passed\n" );
         break;
      case 'F' :
         printf("Better try again\n" );
         break;
      default :
         printf("Invalid grade\n" );
   }
   
   printf("Your grade is  %c\n", grade );```
thick wharfBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

trim zodiac
#

because there is no break

covert mountain
# worn wind How does this code output "Well done" even though there is no statement underne...

switch statements have the so called "fall-through" behaviour, meaning if the code enters the case 'B', then it executes all code responsible for this case (which in your case is no code as it's an empty body) and then it falls to the next case, should there be no break.
However you could've also written that 'B' and 'C' case as:

switch (grade) {
    // case A here
    case 'B' ... 'C':
        printf("Well done\n");
        break;
    // rest of cases
}
#

Example to demonstrate the fall through behaviour:

#

;compile

#include <stdio.h>

int x = 4;
switch (x) {
    case 1:
        puts("1");
    case 2:
        puts("2");
        break;
    case 4: puts("4");
    case 3: puts("3");
    case 5: puts("5");
    default: puts("default");
}
leaden gullBOT
#
Program Output
4
3
5
default
covert mountain
#

;compile

#include <stdio.h>

int x = 1;
switch (x) {
    case 1:
        puts("1");
    case 2:
        puts("2");
        break;
    case 4: puts("4");
    case 3: puts("3");
    case 5: puts("5");
    default: puts("default");
}
leaden gullBOT
#
Program Output
1
2
covert mountain
#

In case you wonder why it is that way: You can imagine switch statements as 2 separate parts: The case conditionals and the case bodies. The entire code of the case bodies is written in sequence, so it'd look something like this:

case1:
    puts("1");
case2:
    puts("2");
    goto end
case4:
    puts("4");
case3:
    puts("3");
case5:
    puts("5");
casedefault:
    puts("default");
    // goto end; could be put here, but doesn't actually do anything as 'end' comes immediately after this anyways
end:
    // whatever comes after the switch statement
```and the case conditionals you can imagine as just a big lookup table which then jumps into the code.
```c
if (x == 1)
    goto case1;
else if (x == 2)
    goto case2;
else if (x == 3)
    goto case3;
else if (x == 4)
    goto case4;
else if (x == 5)
    goto case5;
else
    goto casedefault;

The actual lookup is way more efficient than such a long if-elif-else chain (actual lookup happens in O(1), the if-elif-else chain has O(n) runtime complexity).

worn wind
#

@covert mountain cheers that was really helpful thank you 🙏🏼

#

imma keep the channel for tmr so I can revisit

thick wharfBOT
#

@worn wind Has your question been resolved? If so, run !solved :)

ruby minnow
worn wind
#

!solved

thick wharfBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

worn wind
#

!open

fiery solar
#

on case B you are not breaking the statement with break; and because of that, on case b its executing the line under it which is the case to printf("well done");