All language subtitles for 012 Switch Statements_en

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Which demons can also give you control over your code, you learn to control which parts of your code

run using, if elusive, and now statements, each statement has a condition and Java looks at the first

statement with a true condition and runs the code inside.

You can also use switch statements to control how your code runs.

So in this lesson, you're going to learn how do you switch as well as when do you switch versus if

else?

First thing I'll need you to do is create a new class by yourself inside the Section three project,

create a new file name, switch statements Java, and make sure the class has a main method.

So how do you switch a switch statement compares one value against a list of values.

In other words, it compares an argument against a list of cases.

The argument is the value that you're comparing and if the argument matches value one, the first case

runs, if the argument matches value to the second case runs, if the argument matches value three,

the third case runs and if the argument matches none of the values the default case runs.

And so back in our code, we're going to see this in action, we're going to make it into variable named

and month and it's going to story value that represents a month five, which corresponds to the month

of May.

And so now we're going to make a switch statement and it's going to compare your month value against

the list of month values in the form of cases.

So we're going to rate case one.

And there are 12 months, so we'll need 12 cases, so I'm going to copy this 11 more times.

And I'm going to number each case in ascending order.

Followed by default, now, if the argument month matches one of the cases, that case is going to run

and we're going to need to print that month in text.

So if the month ends up being equal to one, we're going to print January.

If it matches, case two will print.

And you know what, I'm just going to copy this Prince theme throughout.

And now I'm going to fill in the appropriate month values.

And if I were a month value doesn't match any of our cases, a default case should execute.

And if it does, we're going to print please enter a valid month.

They think we're ready to run our code.

Whoa, that's weird.

The month argument matches Case five, Jalaa clearly ran Case five, but it also runs every case that

follows.

Well, that's because by nature, which runs every case that follows the case much, it's like the switch

turns on when there's a match.

Let's have a look.

You saw the value five in the month.

Variable switch accepts your month value as an argument, and it compares it to a list of month values.

It starts comparing your value against each case.

Case one, two, three, four, five.

Aha.

We have a match.

So the switch turns on and Javor runs case five, but it also runs every case that follows the case

match.

This is pretty frustrating.

How do we fix it?

That is exactly why we have the brake keyboard, the brake keyboard can break the switch statements.

And so every case inside your switch them it needs to end with the keyword that way after your case

runs, Java knows to break out of the switch statement.

So back in our code, we're going to fix the switch statements.

We're going to add the break keyword at the end of each case.

And now we can rerun our code.

And that's much better.

So which compares your value against each case?

Uh huh.

We found a match after Jova runs the code inside case five, it then sees the break keyword and that

breaks out of the switch statements.

So why didn't we at break next to default?

Well, default is the last thing that can run.

Switch already breaks after default, but for the sake of example.

We'll set month equal to some absurd number like 30 to.

We'll rerun our code.

And the month value doesn't match any of the cases, so the default case Rut's.

All right, so it's time for the million dollar question.

When do we use a Felsen?

When do we use switch?

I want to preface this by saying I use a false 95 percent of the time.

And if statement can test any condition, a switch statement can only compare a value against the list

of others.

So we'll think back to the OP we just built, the one that grants diploma if a student has over 40 credits

and a minimum GPA of 2.0.

Can you think of a way to do this using such absolutely impossible GPA and credits can be any number

from an infinite range of values.

Are you going to make infinite cases?

I don't think so.

The way it fails is designed around conditions and ranges.

It's way more flexible than comparing a value against the list of cases.

So if you fast forward to the AP book just now, you can easily use it false.

So why didn't we use them?

We'll have a look at this code looks pretty ugly, doesn't it?

Once again, the screaming cat is there to tell us what not to do if you're comparing one value against

the list of other values, don't use a false uSwitch because it's honestly prettier.

It's easier to read.

And they also run a tiny bit faster than anything else in general.

So if you don't want to get lost, just follow this rule of thumb and you should be golden.

You can only use switch to compare one value against the list of others in any other scenario.

And 95 percent of the time you're going to need efforts to control how your code runs.

In this section, you learn to fully control how your code runs using NFLs and switch statements, if

false runs a piece of code.

If the condition is true, conditional statements, officials are way more flexible than switch statements.

Ninety five percent of the time you're going to use efforts to control how your code runs.

Switch runs a piece of code.

If the argument matches a case on the off chance that you need to compare a value against a list of

other values, then you can use switch.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.