hi welcome to an Arduino step-by-step course by Robojax
now let's have a look at the switch and case
so what is a switch or a case which is a simpler version of f and f statement
and executes block of code based on a specific case an example of a f statement
and we can have f id as a variable we are checking it if id is equal 555 so this two equal sign is
for comparison purpose and we are comparing id with 555 if this is true then we print this text
serial dot print ln normal so this is some kind of id and we are printing normal we could do
more but for simplicity I’m just putting this one line if this is true this will be printed but you
can have multiple lines and multiple complicated code this then then we say else f if this is not
true then we have another condition id is equal 1 to 9 again we put two equal signs remember if
you put one equal sign this will be always true one equal sign means assignment 129 is assigned
to id and if we put one equal sign and if we say f id is equal which with one equal sign equal
129 then it checks if it can put 129 into id as soon as it can put it then the value will be true
for that reason we have to put two equal sign now we check if id is equal 129 with this two equal
sign we print this text and say abnormal else mean if this is not true and this is not true and
in case these two are not true then just say the id not found here is how we define it the switch
is defined with the word switch with s nothing is capital and then we have open and closed practice
and inside this we have some kind of variable and it must be a variable you cannot put a
string or numerical value opening curly brace and closing curly brace this is exactly like
other statements that we have in Arduino so this is opening and closing and then we have cases
for example this a cases value one so we put case and then value which we compare it with this and
then we have this column and if this is equal that then we execute this block of code whatever we put
in here it could be a piece one line or it could be hundreds of line or calling another
function whatever and then if we have case two another case we have another value value 2
then we do that and then we execute another piece of code if this is equal to that
this is an if statement that I have just explained and now let's implement it using a switch this can
be written like this if we say fid we just put id and then we put case 555 that's the first one and
then case 129 that's the second one and then we put default, default is equivalent to else
and here we check if ids equal 555 then we say serial print normal
and if that is the case then we also put break. break means do not go and compare to this
if you do not put the break it will also go and check this but in an if statement it just checks
it once if this is true it exits everything but here when this is true if you do not put break
it executes this and also it goes to the next line and check and prints it so this breaks
mean do not compare or do anything if just do this and break which means exit from this
switch if this is not true if the id is not equal to 555 then compare this one 129
compare it with id like this line if that is true print this line and if that is not true then
default means this is not true this is not true so just do this id not found so this is how we use
the switch and now let's go for another example if you found this tutorial helpful please thumb
up and also click on the subscribe on the right side in the lower corner I really appreciate it
and here is another example we have an integer variable called code the value is 241
and inside the switch we just check the code against these values and we see for example in
here if code is equal seven to one then this case means if that code is equal 721 then
execute this code and this code is calling a function there's a function called
send warning so this will be executed and right now we don't know what is inside this function
but this is just an example you can write your own function and call it based on the condition
or if the code is equal 241 then open main door
default if this is not true this is not true you can have hundreds of these cases
not true then say wrong code wrong code means another function send SMS to someone
inform them or take a picture or shut the door or do something else so this is just a function
and here is the equivalent of the switch with this f code is equal seven one zero
send warning else if the code is equal to four one gen then run this function open main door
and default wrong code else mean wrong code so the code must be these two of course we can have else
f more else f so f is important at the beginning and else is important at the end to make this work
please make sure to subscribe by clicking at the subscribe button in here thank you
thank you for watching tutorial from Robojax if you learned something and found this useful
please thumb up as this will help my video in the search algorithm of YouTube
if you have comment or question please post it at the comment section below
I try to answer and reply and don't forget subscribe to get updates on my upcoming videos