Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

switch case logic #8

Open
uxrkhan opened this issue Jun 7, 2020 · 2 comments
Open

switch case logic #8

uxrkhan opened this issue Jun 7, 2020 · 2 comments

Comments

@uxrkhan
Copy link

uxrkhan commented Jun 7, 2020

The switch case explanation is a bit misleading . There should be a break statement after every case for it to function as an if-else statement, otherwise it will evaluate all the cases.

switch (x) {                  // x must be int
    case X1: a; break;        // If x == X1 (must be a const), jump here
    case X2: b; break;        // Else if x == X2, jump here
    default: c;               // Else jump here (optional)
}
@singhaln93
Copy link

The switch case explanation is a bit misleading . There should be a break statement after every case for it to function as an if-else statement, otherwise it will evaluate all the cases.

switch (x) {                  // x must be int
    case X1: a; break;        // If x == X1 (must be a const), jump here
    case X2: b; break;        // Else if x == X2, jump here
    default: c;               // Else jump here (optional)
}

Hi, @uxrkhan Switch cases i.e. (X1 and X2) mentioned above have break statements. But the fact is default case should also include a break statement. @mortennobel thanks for creating this cheat sheet. You can insert a break for default case and close this issue.

@nasir-shah-cloud
Copy link

nasir-shah-cloud commented Jul 19, 2021

The switch case also support char type , not only int type, though char is a kind of int.

switch (x) { // x must be int and char
case X1: a; break; // If x == X1 (must be a const), jump here
case X2: b; break; // Else if x == X2, jump here
default: c; // Else jump here (optional)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants