Interview Kickstart has enabled over 3500 engineers to uplevel.
Switch statements are pretty popular in software engineering technical interviews. If you are preparing for a software-engineering technical interview, you must know them and understand how to use them well. In this article, we will discuss switch statements in C or C++ language.
This article will cover the following concepts:
The switch statement in C/C++ takes the value of a particular variable and compares it with multiple cases. Once it finds the matching case, it executes the block of statements associated with that particular case. You can look at it as an alternative for long if-else statements.
In the switch statement, each case in a switch statement block has a different value, which acts as a unique identifier. The value provided to the switch statement is compared with all the cases inside it until it finds the case representing the same value.
A provision for a default case also exists that is similar to the else block in an if-else statement. If there is no matching case with provided value, then the default statement is executed.
The default case inside the switch statement is optional. If our value does not match with any case label and the switch statement does not include a default case, then no case will be executed.
// val - it is the value provided to the switch block
// this block gets executed if t1 == val.
// this block gets executed if t2 == val.
// this block gets executed if val doesn't match any of the above cases.
In the above syntax, the execution involves comparing the val with the values of each case label (i.e., with t1, t2).
If we do not use the loop control statement break at the end of the block for each case, then all statements after the case with the matching label are executed. This case is known as a fall-through switch case.
If an expression after evaluating gives a constant value, that expression is a valid expression for the switch statement.
// Constant expressions
switch(5+23) is equivalent to switch (28), and it is valid
switch(1*6+4/2) is equivalent to switch(8) and it is valid
// If variable expressions have assigned fixed values, then they are allowed, and hence, they make a valid expression.
Have a look at the following flowchart to understand switch statements better:
Now we’ll understand how switch statements work through a few examples:
// Implementation of switch statement in C/C++.
int val = 10;
printf("Case 1 matched");
printf("Case 2 matched");
printf("Case 3 matched");
printf("None of the cases matched");
Case 1 matched
// Implementation of a switch statement in C/C++.
// Here, because we have not used any break statement, the execution of all the cases after matched cases also occurs.
int val = 10;
printf("Case 1 matched\n");
printf("Case 2 matched\n");
printf("Case 3 matched\n");
printf("None of the cases matched\n");
Case 1 matched
Case 2 matched
Case 3 matched
None of the cases matched
O(1) because we are not running any loop, and there are constant operations only.
O(1) because we are not using any extra space.
End else if
Question 1: What is the advantage of switch statements over if-else?
The main advantage of using switch statements over if-else is that the switch statement is more efficient than the nested if-else statements.
A switch makes code cleaner and easy to understand when we have to combine cases. So, because of easier syntax and readability, switch statements are sometimes more preferred over if-else statements.
Question 2: What happens if we place the default case before any other case statement instead of placing it last?
Default case in switch statements executes when there is no matching case value found. In switch statements, we can place the default block anywhere. The position of the default block does not matter; it will still execute only when the program finds no case matching with the desired value.
So, if we place a default case before any other case, It will not affect the functionality of the default case. If we don't use the break statement at the end of the default block, then all the cases after it will get executed until we either reach the end of the switch statement or encounter a break statement.
Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!
If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview prep, we have trained thousands of Software Engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
Article contributed by Omkar Deshkmukh