hey guys what's going on is clever sticky and in this Lecture, we're going to
learn about this piece which statement [NOISE] so this is the basics in
texts of the switch statement to have a cure switch followed by the expression
inside the around records and between the curly brackets we have here it's
the case followed by the valley to enter Collins and break for by the semicolon
so what this means is if the expression of valleys to zero so then we want to
following cold dessicated and then we end the execution with a cure break if
we don't use the cured break after code (Dallas Website Development) then all the following cases
where also be evaluating it and the code will also be executed and I'll give
you an example of how that works a little bit later now the default cured it
will be executed if none of the other this matches so if our expression
will value to for example five then that code goal be executed so it is i
wanted to use break if you don't want all the following cases to be as luck
and the switch statement can be written and an alternative form like so so
instead of early brackets, we can use a column and instead of the ending
currently bracket we can also use and switch followed by the semicolon so
the switch statements index for you and now let's go ahead and I create a
little script to see how warps and practice [NOISE] so I'm just going to
create and our folk animals and I'm just going to put three animals and it
can a dog and duck and then I'm going to use the switch statement and for
the expression I'm going to put in our [a COUGH] and then I'm going to use
area and the or a trend function to pick out a random key from there and so
at this point king randomly take out cad Dugard duck and first the value
which we're going to get so we're going to write case I had colony coal now
so I'm just going to put in this sounds with the animals will make in for
cases which are a dog and dog ok for the documenting put echo with and final
we can forget to break that and place duck from enough with a is the break
and that's a basic program and I'm going to go ahead and running no and i
refreshed the page keeps refreshing and so as you can see randomly puts out
different animals sounds based on the can a kind of random art and value
which has been text out from the rain so that's a basic as an example of the
switch statement now let me show you a for example if we added the default
you're here and we get to say something like any model why did around for your
life so the good idea to run for your life we haven't an animal and here i
would put something like wants [UM] or and now if-if not of the other cases
had the magic so if it's not a and it's not a dog duck it's not a dog then
the default cases executed which are monster, in that case, I know animal
detected around for your life his executed and if we keep a hidden random is
you can see it keeps selecting at once in a while randomly so that's how the
default works and so now (Dallas Website Development)I want to show you an example if we simply don't
use breaks and it's going to give you I'm going to pick create a very boy
named speed I'm going to save sixty-five and so this is going to be like a
car example where we're going to be speeding so I'm going to see the switch
speeding and then I am I going to type all of that I have all these taped up
already but basically is going to give you a different message based on what
kind of same speed you have here so in a case in sixty-five and the age which
is the valley right now we're going to say you're going the speed limit
response.

No comments:
Post a Comment