from switch to if


this is a question from exam....and my answer is after the question
is it write or not?
please tell me if i go wrong.
question:Rewrite the following switch statement using if statements:
switch(grade)
{
case „A‟:
case „B‟: cout << “good”; break;
case „C‟: break;
case „D‟:
case „F‟: cout << “not good”; break;
default: cout << “invalid”; break;
}

1
2
3
4
5
6
7
8
9
  if(grade=='A') || (grade=='B')
cout << “good”;
else if(grade=='C')
break;
else if(grade=='D') || (grade=='F')
cout << “not good”;
else
cout << “invalid”;
You shouldn't have put the 'break' statement on line 4. break means nothing when not in a loop(do,for,while) or switch statement.
so what should i do for case 'C'????
if(grade=='A') || (grade=='B')
cout << “good”;
else if(grade=='C') ; // null statement
else if(grade=='D') || (grade=='F')
cout << “not good”;
else
cout << “invalid”;
Last edited on
Topic archived. No new replies allowed.