When is a “switch” statement preferable over an “if” statement?



When is a “switch” statement preferable over an “if” statement?..

Answer / Pramod Kumar Pandey

A switch statement is preferred when you have multiple cases to test against a single variable and the conditions are exhaustive (i.e., all possible values of the variable are covered). It can make your code more readable and efficient because it avoids nesting if-else statements.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates

3 Answers  


write an interactive C program that will encode or decode a line of text.To encode a line of text,proceed as follows. 1.convert each character,including blank spaces,to its ASCII equivalent. 2.Generate a positive random integer.add this integer to the ASCII equivalent of each character.The same random integer will be used for the entire line of text. 3.Suppose that N1 represents the lowest permissible value in the ASCII code,and N2 represents the highest permissible value.If the number obtained in step 2 above(i.e.,the original ASCII equivalent plus the random integer)exceeds N2,then subtract the largest possible multiple of N2 from this number,and add the remainder to N1.Hence the encoded number will always fall between N1 and N2,and will therefore always represent some ASCII character. 4.Dislay the characters that correspond to the encoded ASCII values.  The procedure is reversed when decoding a line of text.Be certain,however,that the same random number is used in decodingas was used in encoding.

1 Answers  


When is an interface "good"?

1 Answers  


Explain how many levels deep can include files be nested?

1 Answers  


Combinations of fibanocci prime series

1 Answers  


helllo sir give me some information of the basic information the c as printf ,scanf , %d ,%f and why is the main use of these.

3 Answers  


How is = symbol different from == symbol in c programming?

1 Answers  


How can a program be made to print the line number where an error occurs?

1 Answers  


Is anything faster than c?

1 Answers  


Can math operations be performed on a void pointer?

1 Answers  


What are bitwise shift operators in c programming?

1 Answers  


Write a program to print distinct words in an input along with their count in input in decreasing order of their count

0 Answers  


Categories