Computer Science Canada

convert switch case to if else

Author:  kris [ Sun May 30, 2004 12:20 am ]
Post subject:  convert switch case to if else

can any1 help me to change this code to if-else statements...

char ch = input.charAt(j);

switch(ch)
{
case '+': // it's + or -
case '-':
gotOper(ch, 1); // go pop operators
break; // (precedence 1)
case '*': // it's * or /
case '/':
gotOper(ch, 2); // go pop operators
break; // (precedence 2)
case '(': // it's a left paren
theStack.push(ch); // push it
break;
case ')': // it's a right paren
gotParen(ch); // go pop operators
break;
default: // must be an operand
output = output + ch; // write it to output
break;

Author:  Dan [ Sun May 30, 2004 12:48 am ]
Post subject: 

code:

if(ch == '+' || ch == '-')
{
          gotOper(ch, 1);
}
else if(ch == '*' || ch == '/')
{
           gotOper(ch, 2);
}
//and so on with else ifs till u get to the defult
else
{
            output = output + ch;
}


Notes:

have to use "||" in place of "or" in ifs

have to use "==" and not "=" in ifs for compaerson

boolean staments in else ifs will only be check if ifs b4 it are false

else runs if all other ifs b4 are false


: