Flag This Hub

Java Programming guide : 8 -- Case Switcher

By


Welcome

Hello, and welcome to your eighth java programming tutorial! In this segment we will look at the case switcher statement!

Whats a switch statement?

A switch statement is basicly a bunch of if and else statements, but its on one intager. See if you had an int called num, and you set it to 4. Then later in your code you would put a bunch of if statments to check what number it is. Like if its 1 do this; If its 2 do this; ETC.

Lets Begin

Ok so you want to clear your code, so all there is is your main method. In the main method please create the following variables, and set them as below:

  • int num = 4
  • string text = "zero"

Now we need to add an if statement to see what number NUM is. So lets write the code below:

public static void main(String[] args)
{
	int num = 4;
	String text = "zero";

	if(num == 0)
	{
		text = "zero";
	}
}

But now we need to continue all the way to at least 4, so lets go all the way to 5:

public static void main(String[] args)
{
	int num = 4;
	String text = "zero";

	if(num == 0)
	{
		text = "zero";
	}

	if(num == 1)
	{
		text = "one";
	}

	if(num == 2)
	{
		text = "two";
	}

	if(num == 3)
	{
		text = "three";
	}

	if(num == 4)
	{
		text = "four";
	}

	if(num == 5)
	{
		text = "five";
	}
}

Now obviously if we ran the code it would change the string TEXT to four, because the value was four, but we didn't print it, but thats ok because now we are going to do the same thing, but with a switch statement.

public static void main(String[] args)
{
	int num = 4;
	String text = "zero";

	switch (num)
	{
            case 1:  monthString = "one";
                     break;
            case 2:  monthString = "two";
                     break;
            case 3:  monthString = "three";
                     break;
            case 4:  monthString = "four";
                     break;
            case 5:  monthString = "five";
                     break;
	}
}

Now as you can see, that took a lot less typing, and it does the same exact thing!

Explained

Ok so to start the case statement we add a keyword called "case", then put the number we will switch in parenthesis like this:

switch(num)
{

}

Then you need to add a few cases, like this:

case 1:  monthString = "one";
                     break;
case 2:  monthString = "two";
                     break;

Ok so when it says case 1, that means if the NUM is equal to 1, do the following. And it sets the string to one.

The break command means "break" out of this case, because we already set the statement, so its impossible to ask more cases, so we dont need to waste javas time

The Final Code

public static void main(String[] args)
{
	int num = 4;
	String text = "zero";

	switch (num)
	{
            case 1:  monthString = "one";
                     break;
            case 2:  monthString = "two";
                     break;
            case 3:  monthString = "three";
                     break;
            case 4:  monthString = "four";
                     break;
            case 5:  monthString = "five";
                     break;
	}
}

Thanks

Thank you so much for reading up on this segment in java tutorials. i hope it helped, and i hope you learned something! If i helped you in any way please go to http://cyberpwn.com for more tutorials!

Up next

  • Comments
  • Arrays

Comments

cyberpwn 4 months ago

back to work!

mikeydcarroll67 4 months ago

Interesting hub! Could you offer a little more of a tutorial of how to use this on a webpage?

cyberpwn 4 months ago

what do you mean? this is java. Do you mean JavaScript? they are two different things.

Submit a Comment
Members and Guests

Sign in or sign up and post using a hubpages account.



    Like this Hub?
    Please wait working