change if-else-tree to case statement

Optimize code.