if-statement ----> switch-statement

change an if statement to a switch statement

Page 1 of 1

4 Replies - 3975 Views - Last Post: 13 October 2010 - 07:50 PM Rate Topic: -----

#1 paadsp  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-October 10

if-statement ----> switch-statement

Posted 12 October 2010 - 11:18 PM

Hello, I'm new to Java programming and having a little trouble with some coding. I'm trying to convert this segment of code:
import java.util.Scanner;
public class SwitchSample
{
  public static void main (String[] args)
  {      
    Scanner scan = new Scanner(System.in);
    System.out.print ("Enter Size: ");
     
    int size = scan.nextInt();

    if (size > 59)
      System.out.println("Big");
    else
      if (size > 29)
        System.out.println("Average");
      else
        System.out.println("Small");
  }
}



into a switch statement. This is what I have so far:
import java.util.Scanner;
public class SwitchSample
{
  public static void main (String[] args)
  {      
    Scanner scan = new Scanner(System.in);
    System.out.print ("Enter Size: ");
     
    int size = scan.nextInt(); 
     
    switch (size)
    {
      case 1:  System.out.println ("Small");
               break;
      case 0:  System.out.println ("Average");
               break;
      default:  System.out.println ("Big");
                break;
    }
  }
}


I want the switch statement to do produce the same output as the if-statement. I'm not sure what I'm doing wrong with the code segment for the switch statement.
For example...

<output>
Enter size: 28
Small

This post has been edited by paadsp: 12 October 2010 - 11:20 PM


Is This A Good Question/Topic? 0
  • +

Replies To: if-statement ----> switch-statement

#2 masijade  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 196
  • View blog
  • Posts: 580
  • Joined: 03-April 10

Re: if-statement ----> switch-statement

Posted 13 October 2010 - 12:22 AM

You cannot use ranges in a Java Switch, unfortuantely so you would probably have to do something like
case 0:
case 1:
...
case 28:
case 29:
  // small
  break;
case 30:
case 31:
..
case 58:
case 59:
  // average
  break;
default:
  // large
  break;


assuming that the number cannot be negative.

Not very advantageous, no?
Was This Post Helpful? 0
  • +
  • -

#3 Dean_Grobler  Icon User is offline

  • D.I.C Regular

Reputation: 39
  • View blog
  • Posts: 390
  • Joined: 15-January 10

Re: if-statement ----> switch-statement

Posted 13 October 2010 - 12:36 AM

Urgh that wouldn't be too fun..

You could also do this:


Scanner scan = new Scanner(System.in);  
System.out.print ("Enter Size: ");           
int inpt = scan.nextInt();  
int size = 0;

if(inpt>59){size = 1;}
else if(inpt>29){size = 2;}
else{size = 3;}

switch(size){
    case 1: System.out.println("Big");
            break;
    case 2: System.out.println("Average");
            break;
    case 3: System.out.println("Small");
            break;
    default:System.out.println("Unexpected error");
            break;
            }



But then you still using if() statements in there...
Was This Post Helpful? 0
  • +
  • -

#4 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5643
  • View blog
  • Posts: 12,359
  • Joined: 16-October 07

Re: if-statement ----> switch-statement

Posted 13 October 2010 - 04:56 AM

A switch is syntactic sugar. Unlike a standard if then construct, you never have to use them. They're nice if you have a simple list. If you have actual logic, no so much.

Another thing you might never have to use is a ternary operation. So, for the sake of silliness:
switch (size>59 ? 2 : size>29 ? 1 : 0) {
	case 2:  System.out.println ("Big"); break;
	case 1:  System.out.println ("Average"); break;
	default:  System.out.println ("Small"); break;
}



Of course, by the same token you could also do this:
System.out.println(size>59 ? "Big" : size>29 ? "Average" : "Small");


Was This Post Helpful? 0
  • +
  • -

#5 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8315
  • View blog
  • Posts: 31,836
  • Joined: 06-March 08

Re: if-statement ----> switch-statement

Posted 13 October 2010 - 07:50 PM

or
String[] sizeName = {"Big", "Average", "Small");
System.out.println(sizeName[size>59 ? 2 : size>29 ? 1 : 0]);


:^:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1