Studying Java-Part 11-switch statement

1.First of all

This time, it's about conditional branching similar to if statement!

Its name is "** switch statement **"! Personally, I often use if statements, but I sometimes see switches, so it's worth remembering!

2. switch statement

2.1. switch --basic

The switch statement is also a conditional expression. The basic switch statement looks like this: ..

Main.java


public class Main {
	public static void main(String[] args) {

		//↓ switch statement from here
		switch(formula){
case value_0:
			//Processing for value 0
			break;
case value_1:
			//Processing for value 1
			break;
case value_2:
			//Processing for value 2
			break;
		}
		//↑ switch statement up to here
	}
}

Basic primitive types can be used in expressions.

However, this is not enough w

Let's enter a specific value so that we can understand it a little more!

Main.java{


public class Main {
	public static void main(String[] args) {

		int number = 1;

		//↓ switch statement from here
		switch(number){
		case 0:
			//Processing for value 0
			System.out.println("It's a display of 0");
			break;
		case 1:
			//Processing for value 1
			System.out.println("1 display");
			break;
		case 2:
			//Processing for value 2
			System.out.println("Display of 2! !! Ugogogogogo");
			break;
		}
		//↑ switch statement up to here
	}
}

First, you're initializing to number = 1!

Then, I will pass the value to be judged by switch (number). It is evaluated whether the value of number passed at this time fits to the right of each case.

Since number = 1 this time, the processing in case 1: is executed and "" 1 is displayed "" is output.

It is also a good study to change the value of number from 0 to 2.

2.2. switch - break

The bottom line is that if you don't write a "break;" sentence, you'll be miserable. For example, the following example. ..

Main.java


public class Main {
	public static void main(String[] args) {

		int number = 1;

		//↓ switch statement from here
		switch(number){
		case 0:
			//Processing for value 0
			System.out.println("It's a display of 0");
		case 1:
			//Processing for value 1
			System.out.println("1 display");
		case 2:
			//Processing for value 2
			System.out.println("Display of 2! !! Ugogogogogo");
		}
		//↑ switch statement up to here
	}
}

I erased all the words of break. Then what about?

After executing the process after matching the condition of case 1: You can see that the processing of case 2: has also been executed.

What the hell! you know! ~~ By the way, you can see that the basic processing has flowed from top to bottom w ~~

If you do not write break, it will move to the next process and the process of the next case will be executed.

Don't forget to "break" if necessary! !!


As a rare technique

Main.java


public class Main {
    public static void main(String[] args) {

        int number = 1;

        //↓ switch statement from here
        switch(number){
        case 0:
        case 1:
        case 2:
            //Value 0, 1,Processing about 2
            System.out.println("It's a display of 0, 1 and 2.");
            break;
        }
        //↑ switch statement up to here
    }
}

You can also do it all together like this.

2.3. switch - default You could use else in the if statement.

Instead of this else? There is a default that can be used for. How to use ...

Main.java


public class Main {
	public static void main(String[] args) {

		int number = -1;

		//↓ switch statement from here
		switch(number){
		case 0:
			//Processing for value 0
			System.out.println("It's a display of 0");
			break;
		case 1:
			//Processing for value 1
			System.out.println("1 display");
			break;
		case 2:
			//Processing for value 2
			System.out.println("Display of 2! !! Ugogogogogo");
			break;
		default:
			System.out.println("Yaaaaaaa");
			break;
		}
		//↑ switch statement up to here
	}
}

The point is the last line.

default:
  System.out.println("Yaaaaaaa");

That place. This is like else in an if statement.

If none of the values apply, the process will run to default.

2.4. switch --String type

It ’s a little derailed, but Starting with Java SE 7, you can use the String type in the switch syntax. (As trivia Earlier I mentioned that you can use primitive types in switch expressions, but now you can treat String types a bit more specially!

Main.java


public class Main {
	public static void main(String[] args) {

		String number = "zero";

		//↓ switch statement from here
		switch(number){
		case "zero":
			//Processing for the value zero
			System.out.println("It's a display of 0");
			break;
		case "one":
			//Processing for the value first
			System.out.println("1 display");
			break;
		case "two":
			//Processing for value two
			System.out.println("Display of 2! !! Ugogogogogo");
			break;
		default:
			System.out.println("Yaaaaaaa");
			break;
		}
		//↑ switch statement up to here
	}
}

You can now use it in the same way as normal numerical values. ~~ Thank you ~~

However!

If the String type is empty, "null" (A state where there is a special existence garbage called null other than the state such as number = "", number = "null") In this case, it will not be picked up by default, so be careful! !! For example, the example below.

Main.java


public class Main {
	public static void main(String[] args) {

		String number = null;

		//↓ switch statement from here
		switch(number){
		case "zero":
			//Processing for the value zero
			System.out.println("It's a display of 0");
			break;
		case "one":
			//Processing for the value first
			System.out.println("1 display");
			break;
		case "two":
			//Processing for value two
			System.out.println("Display of 2! !! Ugogogogogo");
			break;
		default:
			System.out.println("Yaaaaaaa");
			break;
		}
		//↑ switch statement up to here
	}
}

You put null in the String type called number that you declared suddenly.

And when I run this program, I get a "NullPointerException" error on the switch (number) line. The so-called "nullpo".

Therefore, I think it is a good idea to check if it is null before passing it through the switch.

Main.java


public class Main {
	public static void main(String[] args) {

		String number = null;

		//null check
		if(number == null){
			//If number is null, put an empty string for the time being
			number = "";
		}

		//↓ switch statement from here
		switch(number){
		case "zero":
			//Processing for the value zero
			System.out.println("It's a display of 0");
			break;
		case "one":
			//Processing for the value first
			System.out.println("1 display");
			break;
		case "two":
			//Processing for value two
			System.out.println("Display of 2! !! Ugogogogogo");
			break;
		default:
			System.out.println("Yaaaaaaa");
			break;
		}
		//↑ switch statement up to here
	}
}

It may be extreme, but it may have been done like this.

3. Conclusion

As a summary of this time,

--switch can be used as a conditional expression --Break should not be overlooked if necessary --You can use else in the if statement called default --String type can be handled, but be careful about null

that's all.

~~ There is something that makes me feel like I forgot to write it, but ...

Next time, I would like to write an article about "for sentence"! !!

It seems that there will be a little update interval, so please wait.

Recommended Posts