Introduction
In our daily lives, we make several decisions, like whether to eat pizza or pasta or a burger. The decision which we opt for often decides our future course of action. Like choosing a car over a bike while going to office cities like Bangalore would most likely make one stuck in huge traffic.
Decision-making is not only limited to the human paradigm but entered into the technological world. Eg - we train robots on which action to perform and which to avoid. The most famous example is Chitti in the movie Robot where a little change in chip code resulted in a robot destroying the city and making its own robot army.
If we talk in the coding world, we use conditional statements like if, else, else-if, or switch statements for making an informed decision.
Prerequisite
- Basic understanding of C/C++
- Understanding of conditional statements
Switch Statement
In this, the condition provided is checked with multiple identifiers i.e with multiple cases. If any given case comes out to be true, then the code corresponding to that specific case gets executed. If none of the cases becomes true, then the block of code corresponding to the default case gets executed.
switch (expression){
case identifier1:
//code block
break;
case identifier2:
// code block
break;
....
case identifierN:
//code block
break;
default:
//code block
}
For a better understanding of the switch statement please refer to the below-given code.
Suppose you go to a concert and due to the huge crowd you are unable to see the staging area. The only thing you can hear is the song. On the basis of the song, you know the singer. Let's code the given situation with the help of a switch statement.
Here we are taking a string ‘song’ that represents the song played during the concert and printing the singer accordingly.
switch (song){
case “Attention”:
cout<<"singer is Charlie Puth"<<endl;
break;
case “Yaarr Ni Milyaa”:
cout<<"singer is Hardy Sandhu"<<endl;
break;
case "Tu hai ki nahi":
cout<< "singer is Ankit Tiwari"<<endl;
default:
cout<< "wrong input entered "<<endl;
}
Here you can see that after every case break statement is given in order to avoid a clash between 2 sets of code blocks.
If we do not provide a break statement then the control of the program will execute all the code blocks until it encounters a break statement, with the exception of the default case.
switch (song){
case “Attention”:
cout<<"singer is Charlie Puth"<<endl;
case “Yaarr Ni Milyaa”:
cout<<"singer is Hardy Sandhu"<<endl;
break;
case "Tu hai ki nahi":
cout<< "singer is Ankit Tiwari"<<endl;
default:
cout<< "wrong input entered "<<endl;
}
In the above case, if the song is “Attention” then the system should print “singer is Charlie Puth”. However as a break statement is given after that, thus the system would also print “singer is Hardy Sandhu” which is wrong as it has nothing to do with that song.
However, in some instances, avoiding the break statements is suitable. Suppose we have to print months' names having 31 days, then we have to multiple months thus in such a situation break statement can be avoided.
If -else statement
If else statement provides another means through which users can make informed choices. In this, a given condition is checked, and if the given condition comes to be true then control of the program goes to one part of a block and executes statements mentioned in the ‘if’ block. But if the condition comes to be false then control of the program execute the statement mentioned under else block
On understanding, the basics let us further hone our knowledge through an example.

Join the conversation! Your thoughts help the community grow.