Tip:
Highlight text to annotate it
X
Hello, I'm Eli for edu4java
and this is tutorial number 6
of Basic Java;Instruction or sentence "Switch"
Let's start with a small program I've made
This program uses a scanner which we had already seen in past tutorials
Here is the import of the class Scanner. When we want to use a class we usually have to import it first
It has a lot of System.out. It prints on the console a Menu
which gives two options; add two numbers or
substract one number to another
This sentence leaves a space in between
This prints "Ingresar opción" (insert an option)
Notice it doesn't have an "ln" so the next line will be writtten in the same line
This means that the option is going to be written in the same line
Till we write an option and press enter the execution will be stopped. The number will be kept in the variable "option"
It's a type int variable
Once we know the option we are going to
do another "print" asking for the first number
Another scanner asking for the number
the same with a second number
Another scanner to ask for the second number
We declare a variable "resultado" which is empty. In this variable we are going to keep the result. We ask which option did we choose
This is the one we chose
If option equals one...-this double = means equals. If you remember one = means assignment.
So if option equals one it will come this way and if will add the two numbers
we asked for
If it's not one (let's say it's two)
It will substract both numbers
Finally we are going to print on the console
the result
Let's run and see what happens
Here we can see the menu we had written and the space we had left
"Ingresar opción" ("Insert an option") Let's write option 1
2+3=5. Ok, it works correctly
Another execution
Option 2
4-1=3 Ok, it works!
Let's do it a bit more complicated
Let's add two
more options. I've just added the option "multiply" and "divide"
Now in "insert option" you can choose between 1,2,3 and 4
The two numbers we are asking for don't change either because there are also two numbers
This is what changes. If it's one this is correct
In the case it's not one
there are more options. We are going to do an if-else
and we are going to ask if option
option==2
If it equals 2
this is what we are going to do
else-
it could be three or four. We should write another if
if option
option==3
we are going to multiply and if not we are going to divide
Apparently everything is ok
Let's try it
option 1
2+2=4
Let's try the third option
3*4=12
Lastly we are going to try option 4
7/2=3
It will be 3 something but because we are working with integers it will be 3
Nice program with if
But this tutorial is about switch so we are going to do this but in another way
Let's choose the instruction switch
Switch has this format here
This "key" is the variable which will
establish the selection. Switch is just like an if-
It also works over the flow control of the program
but instead of using boolean conditions and true or false
we write here an expression
In our case we are going to put a variable ="option"
but it could be any expression which had as a result
a countable type. In this case it is numeric
In the case option=1
In the case option=2 we would do another thing
in the case option=3
We need another case
In the case option=4
1,2,3,4
In the case option=1 everything inside here will be executed
In between case 1 and break
If option=2 what will be executed
is everything inside here, between case 2 and break
The same with 3 and with 4
If it is not any of these
It will go automatically to default
Anything written here will be executed
and it will finish
To do this, let's
copy
what we want to do from the if of before
Option 1; to add, option 2; to substract
Option 3; multiply
In case of option 4; divide
In none of the others are executed then this will be executed
and we could write "error"
"error in option"
This will be shown on the console
as I told you this switch
would replace this
Here I have an error.
Java tells me that "resultado"(result) is not initialized
Java has taken a look at the flow control
If we come here and here there is no problem. But what happens if we come here?
"resultado" is not initialized. We could potentially get to a variable which was never initialized
int "resultado" has nothing inside
The easy way is to initialize it to 0
and it would already have a result
Let's run the program
Let's see if everything is ok
Again. Sum two numbers. Option1;2+2=4
Option 2
5-2=3
option 3
Multiplication
2*4=8
And lastly
5
no, 15
Ups, here I commited an error. I wrote 15
which is not an valid option
It is asking for the numbers, this is not very nice... but it says "error in the option"
It has come out this way, by default
It is ok as an example!!
and lastly option 4. 9/3
=3
With this we can finish the tutorial about switch
I personally don't use "switch" a lot because everything you can do with a "switch" you can do it with an "if". There are some cases
in which "switch" can come in handy
In object orientated programming there is another methodology to do this type of instructions
to resolve this situation
but for today, this was more than enough!
Thank you for listening! See you in the next tutorial!!