Category
š
LearningTranscript
00:00Hi, today I'm going to explain switch statement.
00:07When in a program we have to take multiple times decision then we use if statement to
00:14create the program.
00:16When decision is to be taken number of times then number of if statement increases and
00:22because of this it become difficult to read the program.
00:27So what the developer did, they came up with the switch statement and it become simple
00:39to make the program of multiple decision, so developer came up with the switch statement
00:45to make multiple decision program simple.
00:50The switch statement has the value of a given variable against the list of case values and
00:56when match is found a block of statement associated with that case is executed.
01:04I'm going to make one of the program one of the demo program of use of integer value and
01:12I'm going to also make a demo program how to use the character value of switch case
01:19The things will be more clear to all of you.
01:23So I'm going to open a new file, empty file let us save it, so it's a simple program just
01:43to demonstrate that how the switch case is used.
01:51First thing what I will do I will use a variable a okay, now here I will give a message that
02:01enter any value okay, so I will input any value I will use the scanner for it plus and
02:15d comma and then what a, now here I will use the switch case, switch and inside it what
02:26a, first of all I will use what case one and here I will display what one of the message
02:37welcome inside it and at the end I will use the break statement then what case two, here
02:48I will display a message how are you okay and I will apply the break statement then
03:00what case three and what printf, one more message inside it, it's not mandatory only
03:13that you have to you can use messages inside the test cases or you can inside the switch
03:20cases but you can do the logical work by using the statement and you can apply the loop also
03:28inside it okay, so we'll see in our further videos, coming videos we are going to use
03:36the if statement for loop inside the switch cases okay.
03:41Now I will use a default case and inside it I will use what a message that is what
03:54by and here I will use rotate and zero for the exit status, so this is the switch case
04:04program I hope everything is fine, let me run the program and then I will explain how
04:12the value is going to the switch and then it will take you to a particular label to
04:19the case okay, let us click on the build it's showing me zero error and the zero warning
04:25I'm going to run it, suppose here I've used what input value three so scanf is going to
04:31read it, so what the switch case is going to do, what this switch case is going to do
04:36as it is having the value three, it will directly take me to this case three okay to this label
04:44and what it should display, it should display what come again, so let me press the enter
04:50so displayed what come again okay, now let us run one more time and if I input any other
04:57value suppose seven so there is not any case seven but there is a default case so it will
05:03take to the default case and what is there inside the default case that is by okay, so
05:14what it should display by when I press enter it displayed by, break statement is used to
05:22break from the particular case okay, the next program which I will going to explain that how
05:30to use the character value, thanks