No, you can't; in switch
es you can only implicitly use the ==
operator and only on integral and enumeration types (§6.4.2). You should rewrite that switch
as
switch (i){
case 'e':
case 'i':
case 'o':
case 'u':
case 'a':
cout<<"Vowel";
break;
case '+':
case '-':
case '/':
case '*':
case '%':
cout<<"Op";
break;
}
which exploits the fall-through feature of the switch
statement.
if not than how can we use comparison or logical operators in switch ?
Simply, you can't. If you want to do anything different than equality comparison with integral/enumeration types you have to write several if
/else
statements.
& why cant we declare and initialize variable in single case without using scope ?
It's not a problem of declaration, but of initialization; see the link in @awoodland's answer.