01. Difference between if and switch

The if statement has a condition part that can:

  1. Take any integer or boolean value

  2. If integer is non zero or boolean is true

    1. Execute the true block

  3. else

    1. Execute the false block

The switch statement can take an expression that evaluates to an integer or enumeration.

  1. It matches the value of the expression with cases

  2. When match is found

  3. the lines after that case is executed.

The if statement can take any kind of boolean expression while the switch statement can only take integer expressions that maps for equality.

Last updated