by Dinesh Thakur Category: Control Structures

Both the switch and if-else-if statements enable us to select one of several alternative statements for execution. However, they differ in several aspects:

1. The switch statement restricts the selection of alternative statements for execution based on the value of a single control expression. The if-else-if statement, on the other hand, is very flexible and permits selection of alternative statements based on values of arbitrary expressions.

2. In the switch statement, each desired value of the control expression must be mentioned explicitly using a case keyword. However, in addition to the individual values, the if-else-if statement allows a range of values to be specified in test expressions.

3. The control expression in a switch statement must be an integral expression, whereas the if-else-if statement allows us to test integral and floating-point expressions as well as character strings (using the library functions provided in the <string. h>).

4. The syntax of the statement is much cleaner compared to the if-else-if statement. Thus, programs using switch statements are usually more readable.

5. The switch statement has the ability to execute more than one alternative statement by omitting break statements. This is not possible in if-else-if statement.

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Related Articles