Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 convert switch case to if else
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
kris




PostPosted: 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;
Sponsor
Sponsor
Sponsor
sponsor
Dan




PostPosted: Sun May 30, 2004 12:48 am   Post subject: (No 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
Computer Science Canada Help with programming in C, C++, Java, PHP, Ruby, Turing, VB and more!
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: