Better: switch-case or if-else?

Viewed 9489

Possible Duplicate:
If/Else vs. Switch

I have two codes here, i just wanted to ask which of the two is better in terms of writability(ease of writing the codes) and in terms of readability (ease of understanding the codes).

switch-case:

import java.io.*;

public class Quarter{
    public static void main(String[] args){
        int day;
        String input="";

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Input a number from 1 to 3: ");

        try{
            input=in.readLine();
        }catch(IOException e){
            System.out.println("Error!");
        }
        day=Integer.parseInt(input);

        switch(day){
            case 1:
            case 2:
            case 3:
                System.out.println("1st Quarter");
                break;
            case 4:
            case 5:
            case 6:
                System.out.println("2nd Quarter");
                break;
            case 7:
            case 8:
            case 9:
            System.out.println("3rd Quarter");
            break;
            case 10:
            case 11:
            case 12:
                System.out.println("4th Quarter");
                break;
            default: System.out.println("Error!");
        }

    }
}

if-else:

import java.io.*;

public class Days{
    public static void main(String[] args){
        int day;
        String input="";

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        System.out.print("Input a number from 1 to 12: ");

        try{
            input=in.readLine();
        }catch(IOException e){
            System.out.println("Error!");
        }
        day=Integer.parseInt(input);

        if(day>=1 && day<=3){
            System.out.println("1st Quarter");
        }else
        if(day>=4 && day<=6){
            System.out.println("2nd Quarter");
        }else
        if(day>=7 && day<=9){
            System.out.println("3rd Quarter");
        }else
        if(day>=10 && day<=12){
            System.out.println("4th Quarter");
        }else
            System.out.println("Error!");
    }
}
7 Answers
Related