a Differentiate between if and switch statements with example
Answers
Differences b/w if-else and switch statement. Based on the result of the expression in the 'if-else' statement, the block of statements will be executed. If the condition is true, then the 'if' block will be executed otherwise 'else' block will execute. The switch statement contains multiple cases or choices.
Hope it helps....... ❤️
Explanation:
The main difference between them is, if-else statement evaluates a condition and if the result turns out to be true then only the if block would be executed otherwise else block.
SWITCH STATEMENT
A switch statement cannot have two or more cases with identical value .
Switch statement in C tests the value of a variable and compares it with multiple cases. Once the case match is found, a block of statements associated with that particular case is executed. Each case in a block of a switch has a different name/number which is referred to as an identifier.
IF-ELSE STATEMENT
Such situation doesn't occur in If
the 'if-else' statement, the block of statements will be executed. If the condition is true, then the 'if' block will be executed otherwise 'else' block will execute.