IF ELSE VS Switch

Grade
====================
Below 50 Fail
50 - 64 Pass
65 - 74 Credit
75 - 84 Dist
85 & abov High Dist
====================

Can I show the abv grade using if else and also in switch?
Can teach me how to show?

--------------------------------------------
if (marks == 50 && marks > 50 && marks < 64)
cout << "Pass";
--------------------------------------------
Is the above code correct for a Pass grade?
--------------------------------------------
if (marks == 50 && marks > 50 && marks < 64)
cout << "Pass";
--------------------------------------------
Is the above code correct for a Pass grade?
No, marks can't be at the same time equal to 50 and greater than 50, the right condition would be marks >= 50 && marks <= 64 -this would include 64-
Tks Bazzy. Can I assume that the grade table cannot be implemented using switch?
If marks is an integer you could but it wouldn't be much good.
It would look like this:
1
2
3
4
5
6
7
8
switch ( marks )
{
    case 50: case 51: case 52: /*...*/ case 64:
        cout << "Pass";
        break;
    case 65: // ...
        //...
}
You wouldn't use a switch for that, no. And your example statement would always be false because marks cannot be 50, and > 50, at the same time. But yes, if/else blocks are the way to go.

All you can do is shorten the whole thing up a little, by using the correct logic:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (marks < 50) {
    cout << "Fail";
}
else if (marks <= 64) { //It is already implied that marks is > 50 now
    cout << "Pass";
}
else if (marks <= 74) { //Marks must also be > 64 now
    cout << "Credit";
}
else if (marks <= 84) { //And so on...
    cout << "Dist";
}
else {
    cout << "High Dist";
}


I also wish sometimes there were a way to 'switch' on a range of values, but unfortunately this cannot be done. You could do this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
switch (marks) {

    case 1: 
    case 2:
    case 3:
         //Do something
         break;

    case 4:
    case 5:
         //Do something else
         break;

}

but you can see how messy that could get, unless you only have a few numbers.
Last edited on
You cannot do it with a switch. A switch can only check against one condition or variable. You have several ranges to check and therefore it is impossible.
Here's some example code to demonstrate your problem. I will not give you all of it.
1
2
3
4
5
6
7
8
int grade;
cout << "Grade please!";
cin >> grade;
if(grade < 50)
    // code here
else if (grade < 65)
   // code here
etc....

You cannot emulate that behavior with a switch however (theoretically you could create a nested switch but that is just not as efficient or clean).
You also appear to be misinterpreting the logical operators. Read out this line:
if (marks == 50 && marks > 50 && marks < 64)

That reads as, "If marks equals fifty and marks is greater than fifty and marks is less than 64". How is it even possible for marks to both equal and be greater than fifty? Examine your conditions carefully and try reading them aloud.

EDIT: I started the post before lunch and submitted it after lunch and I missed four posts in between. Wow....
Last edited on
I also wish sometimes there were a way to 'switch' on a range of values, but unfortunately this cannot be done.

Actually gcc has an extension which allows ranges in switch, but that's not portable and generates the same code as with separate labels. (thus not so efficient).
In general, compiler extensions should be used carefully and portability should be kept high in mind during their use.
Topic archived. No new replies allowed.