5.1 Switch
Hello everyone, in this course, we will see what the Switch is and how to use it Are you ready? Let’s go! So we’ll open intellij, you can create new project, if you want, and then we’ll come here. For example, type "int" value = 1 If value == 1. Then we display "The value is 1". Otherwise if value == 2 Then we display "The value is 2". Otherwise "The value is not 1 or 2" Now we execute the code And we see it, the phrase "if". And if I change the value to 2. We see, the phrase "else if". Now it can get a little messy.. If you have for example 10 values to test. There is an alternative. It is the use of the "switch" statement. So we are going to do something equivalent to what we did above. But using a "switch" instruction. So int switchvalue =1. switch switchvalue case 1, equals "if value == 1". Then we display it The value is 1. Then type the word "break". The break statement helps the program to exit the switch statement. We will do the same for box 2. And default, It’s like the "else" We display it The value is not 1 or 2. So we comment, Upstairs, we don’t need any more. Then we post it. And as you can see. The value is well 1. And if we change the value to two. We also see that the value is 2, listen was all In the next video we will do a little exercise on the "switch". I hope you’re ready. Go to the next one!