Custom Search

Sunday, May 22, 2011

Intro to the switch statement

In this post we will be covering the switch statement.  A switch statement is a more efficient way to write large amounts of  if statements.  This is useful when you have a program that asks the user for age, weight, height or any other variable that differs for every person and then does something different depending on that variable.  Without switch statements you would have to write if statements for any possible weight which would take forever. That’s where the power of switch statements comes in.  Let’s begin!

import java.util.Scanner;

public class Advice_Generator {



public static void main(String[] args){

Scanner a = new Scanner(System.in);

      int weight;

      weight = a.nextInt();

     

     

      if(weight == 60){System.out.print(weight);}

      if(weight == 61){System.out.print(weight);}

      if(weight == 62){System.out.print(weight);}





}





}

As seen above, we would have to write an if statement for every possible weight.  The switch statement makes this a little easier.

import java.util.Scanner;

public class Advice_Generator {



public static void main(String[] args){

Scanner a = new Scanner(System.in);

      int weight;

      System.out.println("Enter weight");

      weight = a.nextInt();

     

     

switch(weight){

case 60:System.out.println("Eat more, you are to light!");

case 61:System.out.println("Eat more, you are to light!");

case 62:System.out.println("Eat more, you are to light!");

case 63:System.out.println("Eat more, you are to light!");

case 64:System.out.println("Eat more, you are to light!");

case 65:System.out.println("Eat more, you are to light!");

case 66:System.out.println("Eat more, you are to light!");

case 67:System.out.println("Eat more, you are to light!");

case 68:System.out.println("Eat more, you are to light!");

case 69:System.out.println("Eat more, you are to light!");

case 70:System.out.println("Eat more, you are to light!");

case 71:System.out.println("Eat more, you are to light!");

case 72:System.out.println("Eat more, you are to light!");

case 73:System.out.println("Eat more, you are to light!");

case 74:System.out.println("Eat more, you are to light!");

case 75:System.out.println("If you are a child, you are ok.");

case 76:System.out.println("If you are a child, you are ok.");

case 77:System.out.println("If you are a child, you are ok.");

case 78:System.out.println("If you are a child, you are ok.");

case 79:System.out.println("If you are a child, you are ok.");

case 80:System.out.println("If you are a child, you are ok.");

     



}





}





}

We are creating a switch statement for the variable weight and then creating different cases for whatever the variable might be.  So if you are 74 pounds or under it would say Eat more.  This could be done more efficiently with an if statement(if(weight >74){

      System.out.println("If you are a child, you are ok.");

}  )

If statements are better if you have a range you want to do something for, but if you have an application that needs to do something different each time a switch statement is better.  For example a child likes and does different things at each age so a switch statement would be better, but if you were doing a application deciding if someone had to go to the gym, and if statement would be better because at a certain range you should go to the gym.  You would go to the gym if you 5000 pounds and anywhere above so an if statement is better.  Deciding whether to use an if statement or a switch statement simply depends on the goal of the application you are trying to build.

If you have any questions on switch statements or any other topic feel free to leave a comment.  Also if you have any ideas for future posts or things that you feel I can improve on, feel free to leave a comment.  Well, that wraps it up for this post, see you in the next!

No comments:

Post a Comment