Why use switch ?

Well you are thinking correct that we could have used if else here. But sometimes when the program have too many conditions and many nested if else etc the problem becomes too much complex. And the brackets { } makes it even worse. So in that case to make it simple, we use switch. Also if there are not so much logic operating then instead of using == again and again, switch makes it no need. Its easy and better option for making decisions based on variable values only.

What do you like the most ?
Enter your choice from 1 to 3
(1) Video Games
(2) Music
(3) Manga
Enter Your Choice : 3
Dragon Ball is the Best .

second try

What do you like the most ?
Enter your choice from 1 to 3
(1) Video Games
(2) Music
(3) Manga
Enter Your Choice : 7
Bye because you entered wrong choice !

What is happening here is that I have used the variable like as a switch. With different values of like, different instructions are to be followed. Using scanf user will enter a choice which is saved in like variable. Then I applied the switch block as shown above. When the user enters something which is not one of the cases, like if I enter 5 instead of 1 or 2 or 3 here, the default case will be executed.

So that's it for today. In the next post, we are doing another interesting stuff. So stay connected.

Please share this as much as you can so that we can get connected with more and more people who want to learn programming.