Quantcast
Channel: switch-case statement without break - Stack Overflow
Viewing all articles
Browse latest Browse all 9

Answer by Thiago Navarro for switch-case statement without break

$
0
0

Try yourself - Run the code using ideone available here.

#include <stdio.h>void doA(int *i){    printf("doA i = %d\n", *i);    *i = 3;}void doB(int *i){    printf("doB i = %d\n", *i);    *i = 4;}void doC(int *i){    printf("doC i = %d\n", *i);    *i = 5;}int main(void) {    int i = 1;    switch(i){        case 1:            doA(&i);        case 2:            doB(&i);        default:            doC(&i);            break;    }    return 0;}

Output:

doA i = 1doB i = 3doC i = 4

Note:

  • It will execute all the options from the selected case until it sees a break or the switch statement ends. So it might be that only C is executed, or B and then C, or A and B and C, but never A and C
  • If you change the value of the variable analysed in switch inside the handle function (e.g doA), it does not affect the flow as describe above

Viewing all articles
Browse latest Browse all 9

Latest Images

Trending Articles





Latest Images