if and Switch

1
2
if (choice == 'A' || choice == 'a')
break;



1
2
3
switch (choice)
case 'A': break;
case 'a': break;



is there a better way for switch to work? like combining the 'A' and 'a'.
once a switch enters a case, it will proceed until it reaches a break. Therefore you can do an OR kind of thing by stacking case labels:

1
2
3
4
5
6
7
switch(choice)
{
case 'a':
case 'A':
  // do whatever
  break;
}
tks again.
1
2
if (choice == 'A' || choice == 'a')
    break;

is not the same as
1
2
3
4
5
6
7
switch(choice)
{
case 'a':
case 'A':
  // do whatever
  break;
}


The former one will break out of the innermost loop the if is in. The latter will only get out of the switch.
He also forgot to put braces around his switch. But I figured that wasn't the point. =P
Topic archived. No new replies allowed.