Switch

Learn the Basic Java Concepts Loops, arrays and methods
11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€65.44
List Price:  €93.49
You save:  €28.05
£56.04
List Price:  £80.06
You save:  £24.02
CA$95.74
List Price:  CA$136.79
You save:  CA$41.04
A$107.11
List Price:  A$153.03
You save:  A$45.91
S$95.36
List Price:  S$136.24
You save:  S$40.87
HK$547.90
List Price:  HK$782.75
You save:  HK$234.85
CHF 64.03
List Price:  CHF 91.47
You save:  CHF 27.44
NOK kr772.60
List Price:  NOK kr1,103.76
You save:  NOK kr331.16
DKK kr487.88
List Price:  DKK kr697.01
You save:  DKK kr209.12
NZ$117.84
List Price:  NZ$168.36
You save:  NZ$50.51
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,700.43
List Price:  ৳11,001.09
You save:  ৳3,300.65
₹5,837.33
List Price:  ₹8,339.41
You save:  ₹2,502.07
RM333.81
List Price:  RM476.90
You save:  RM143.08
₦92,489.99
List Price:  ₦132,134.22
You save:  ₦39,644.23
₨19,474.37
List Price:  ₨27,821.72
You save:  ₨8,347.35
฿2,588.61
List Price:  ฿3,698.17
You save:  ฿1,109.56
₺2,275.23
List Price:  ₺3,250.47
You save:  ₺975.23
B$358.08
List Price:  B$511.57
You save:  B$153.48
R1,312.98
List Price:  R1,875.77
You save:  R562.78
Лв127.91
List Price:  Лв182.73
You save:  Лв54.82
₩96,452.39
List Price:  ₩137,795.03
You save:  ₩41,342.64
₪267.73
List Price:  ₪382.49
You save:  ₪114.75
₱4,034.64
List Price:  ₱5,764.02
You save:  ₱1,729.38
¥11,065.06
List Price:  ¥15,807.91
You save:  ¥4,742.85
MX$1,201.03
List Price:  MX$1,715.83
You save:  MX$514.80
QR255.05
List Price:  QR364.38
You save:  QR109.32
P965.79
List Price:  P1,379.76
You save:  P413.97
KSh9,296.24
List Price:  KSh13,280.92
You save:  KSh3,984.67
E£3,349.82
List Price:  E£4,785.67
You save:  E£1,435.84
ብር3,985.10
List Price:  ብር5,693.24
You save:  ብር1,708.14
Kz58,375.85
List Price:  Kz83,397.65
You save:  Kz25,021.80
CLP$66,375.71
List Price:  CLP$94,826.51
You save:  CLP$28,450.80
CN¥507.11
List Price:  CN¥724.48
You save:  CN¥217.36
RD$4,113.13
List Price:  RD$5,876.15
You save:  RD$1,763.02
DA9,392.53
List Price:  DA13,418.47
You save:  DA4,025.94
FJ$158.35
List Price:  FJ$226.22
You save:  FJ$67.87
Q544.06
List Price:  Q777.27
You save:  Q233.20
GY$14,678.83
List Price:  GY$20,970.66
You save:  GY$6,291.82
ISK kr9,837.79
List Price:  ISK kr14,054.59
You save:  ISK kr4,216.80
DH708.72
List Price:  DH1,012.51
You save:  DH303.78
L1,246.75
List Price:  L1,781.16
You save:  L534.40
ден4,025.16
List Price:  ден5,750.48
You save:  ден1,725.31
MOP$565.76
List Price:  MOP$808.27
You save:  MOP$242.50
N$1,340.22
List Price:  N$1,914.68
You save:  N$574.46
C$2,582.07
List Price:  C$3,688.83
You save:  C$1,106.76
रु9,353.99
List Price:  रु13,363.42
You save:  रु4,009.42
S/263.57
List Price:  S/376.55
You save:  S/112.97
K270.82
List Price:  K386.91
You save:  K116.08
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,859.31
List Price:  ZK2,656.27
You save:  ZK796.96
L325.94
List Price:  L465.65
You save:  L139.71
Kč1,644.39
List Price:  Kč2,349.23
You save:  Kč704.84
Ft25,720.62
List Price:  Ft36,745.32
You save:  Ft11,024.70
SEK kr762.14
List Price:  SEK kr1,088.81
You save:  SEK kr326.67
ARS$61,174.36
List Price:  ARS$87,395.69
You save:  ARS$26,221.33
Bs486.59
List Price:  Bs695.16
You save:  Bs208.56
COP$277,528.94
List Price:  COP$396,486.91
You save:  COP$118,957.97
₡35,172.98
List Price:  ₡50,249.28
You save:  ₡15,076.29
L1,732.56
List Price:  L2,475.19
You save:  L742.63
₲520,857.74
List Price:  ₲744,114.38
You save:  ₲223,256.64
$U2,703.34
List Price:  $U3,862.08
You save:  $U1,158.74
zł282.38
List Price:  zł403.41
You save:  zł121.03
Already have an account? Log In

Transcript

Hello there, and welcome back to this Java development course. So, last time we went over else if statements in Java, and you had some homework to do, your job was to create a program which will take an integer from the console, in this case an age and print whether that person will be able to vote or not. So here is essentially what you should have done, secure and get into So first of all, you know, initially my scanner pass system.in as where I'm going to get the value from then I say integer age equals SC dot next answer just get the next value from the console. Then if age is more than or equal to 18, we print you can vote if it's not more than or equal to 18 we print you cannot vote and I also added here just for for the last I headed you will be able to vote in and then 18 minus age years so just tell the tell the person how long until they can vote It wasn't really required.

But you know, I just added it just, you know, for demonstration. Okay, so anyway, let's go ahead and run this application. Let's insert 12. And there we go, you cannot vote you will be able to vote in six years. But if we do 18 It's a you It says you can't vote. So if you're 18, if age is more than or equal to 18, which it is, we print, you can vote and then just ignores this.

So there we go. So that is essential. We should have done not very complex, pretty, pretty simple. Soa, yeah. Anyway, without further ado, let's get into today's lesson. Alright, so today, we're gonna be going over something, you know, that's actually kind of related to if statements, and l stiff statements, you know, all together just the whole thing, decision making in Java.

So that's actually what we're going to be going over today. More specifically, this is going to be called the switch statement. So let's go ahead and actually create a new class. This is going to be called switch. As always main method and finish. There we go.

Okay, so how does the switch statement work? So together we're going to write a program. scanner r equals new action. Oh, it's gonna be SC equals new scanner. system.in whoops, nope, not a string. system.in There we go.

All right. Let's import it. Yeah, there we go. Okay, so now we're gonna do int a equals SC dot next int. And so we're gonna write a very simple program. So we're going to do switch a, and there we go.

This is all be explained a second here. Case. One. System dot out dot print ln A is equal To one there we go. case two, System dot out dot print ln A is equal to two. Okay, and so we're just gonna do this exact same thing for all values up to 10.

Three, four, whoops. 5678 and I know this really isn't the most efficient way to do things. And of course, there are more, much better way of doing it we could just do system out print ln plus, then the value whatever value we actually went ahead and actually, you know, whatever value we inputted, so we can do system out println A is equal to plus a But in this case, we're just showing off the switch statement how it's used. So that way we'll just we'll just just take an idea of exactly what's going on here. So here change these values as well. 789 and 10 Okay, so there we go.

So what is this program going to do? Well, what's gonna happen is the switch statement always take some numbers input. So in this case, it's taking a is input, it can be any number and I believe even a string so recently in the eighth, I think version of Java strings in switch statements were allowed now. So is it the eighth or the seventh? I can't I can't remember for sure. But before you couldn't actually insert strings into switch statements, but now you can so you can answer any of the primitive values as well as the as well as the any of the primitive values with strings as well.

So you can also insert strings into switch statements. So essentially, here we have the entire you know, idea have sort of switch statements themselves. So okay, so we insert a, in this case it's a integer, which is two, let's go check case one, it's going to print this dot print line a is equal to one, if A is two, which it is, then it's going to print a is equal to two. So let's go ahead and I just realized I misspelled equal, but let's just keep it like this for now. So let's gonna run the application. Alright, so now we can insert four and there we go.

So as you can see, it's pretty it's behaving a little bit oddly though. So the whole idea of a switch statements pretty simple right? So if it is for which in this case it is, so case for it will just print this, but the problem is, how would we do if else so for that we have it so that if it actually prints case for it, we'll go ahead and print not only that is equal to four, but everything under it as well. So just to give you an idea of exactly what's happening here, so if I can do if I do one, it's going to print all of it so it doesn't stop. So just because we have something in a different case, doesn't necessarily mean that it's gonna not execute it. So if anything is under it, it's going to execute as well.

Of course, we can stop this though. So there's this handy keyword called the break keyword will which will just break so now if we do one, we just do a is equal to one and then a breaks and it exits the switch statement and goes to this line 35 and start executing from there. And since there's nothing there, it just ends the program. So through this break keyword, it allows us to perform or operations in switch statements. So let's say that if we wanted to print h if a was equal to four or five, so in this case, all we would do, so if a is equal to four. We print H, and then break.

Okay, there we go. So now if you go And do four, it'll print age. And if we do five, it'll also print age. So this allows us to, you know, create or statements inside switch statements. So that's where the break keyword comes in handy. So let me go and actually add that to all of these.

Let's go ahead and remove this back. So there's going to be a break, break, break, break. Break, break, break, break, break, break. All right, there we go. Okay, so, so now it just works like a, like an else if statement. So one, a is equal to one, five, a is equal to five and so on and so on.

Now, what if we wanted to do else? So if none of the values are true? Well, for that, we would just have the keyword default system dot out. Whoops, nope, not nano time system dot out dot print ln. A is more than 10. There we go.

All right. And so now if we do 11, a is more than 10. So this default will get executed if all of these are false, essentially. So the switch statement can actually be very confusing personally, when I was learning about it, it took me quite a bit of time before I actually understood where it can be useful and what even does. And generally speaking, if statements are more used that switch statements simply because they're just a little bit more robust, but nevertheless, switch is actually it's a pretty useful operation. Well, yeah, I guess operation in Java.

For working with values where you have to check a lot of different values, compare it with a lot of other different values. So they don't really really we really have switch is just to improve readability. So instead of doing if A is equal to one And then else if A is equal to two else if A is equal to three else, if A is equal to four, we just have this switch statement where we check case one, case two, case three, and so on and so on. So it just, it's just generally a lot easier to work with, and just looks better than doing a bunch of elsif statements and takes up less code. That's also you know, that was pretty good. Okay, so that is the switch statement.

Really nothing too difficult. Without further ado, let's find some work. All right. So for homework, we're going to be pretty simple. We're going to go ahead and first of all, create our switch homework class switch homework. There we go.

Alright, so what we're going to do is, first of all, we're gonna need our scanner class. And we're going to have four options. So we're going to do System dot out dot print ln. What is the correct way of defining a variable In Java, all right, there we go. And so what we're going to do now is we're going to have small print ln 234, we're going to have 123 and four. There we go.

One. int a equals to A to A equals 231. A equals two, and four, speak for something crazy. In two equals a. Let's see like that. All right, there we go.

All right, and so now we're going to get the next integer is going to be from one to four. And what you're gonna do is you're going to tell the person Whether or not they got it, right, so if they got it right, you're gonna say you got it right. If they got it wrong, you're gonna say you got it wrong. And if they are not in the, you know, they're not in from one to four, so if the answer is zero or five and so on, they're gonna say that isn't an option. So you're gonna do this without using the if statement. Obviously, just use the switch statement.

No if statements allowed, okay, so it's pretty simple, not too difficult. If you can understand the switch statement, you can get this. So yeah, I believe in you, and I'll see you next time.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.