If else if Vs Switch
If else if switch
If else if statement uses multiple statements for
multiple choices.
switch statement uses single expression for
multiple choices.
if-else statement test for equality as well as for
logical expression.
switch statement test only for equality.
It evaluates all types of data, such as integer,
floating-point, character or Boolean.
It evaluates only on integer or character type
of data.
It has following general form
1. if(expression)
2. {
3. // statements;
4. }
5. else if
6. {
7. // statements;
8. }
9. else
{
// statements;
}
It has following general form
1. switch(expression)
2. {
3. case 1:
4. // statements;
5. break;
6. case 2:
7. // statements;
8. break;
9. .
.
.
case constant n:
// statements;
break;
default:
// statements;
}
If the all conditions are false then last else
block will be executed.
If the value in the switch does not match with
any case, default statement will be executed.

Differences between switch and case.docx

  • 1.
    If else ifVs Switch If else if switch If else if statement uses multiple statements for multiple choices. switch statement uses single expression for multiple choices. if-else statement test for equality as well as for logical expression. switch statement test only for equality. It evaluates all types of data, such as integer, floating-point, character or Boolean. It evaluates only on integer or character type of data. It has following general form 1. if(expression) 2. { 3. // statements; 4. } 5. else if 6. { 7. // statements; 8. } 9. else { // statements; } It has following general form 1. switch(expression) 2. { 3. case 1: 4. // statements; 5. break; 6. case 2: 7. // statements; 8. break; 9. . . . case constant n: // statements; break; default: // statements; } If the all conditions are false then last else block will be executed. If the value in the switch does not match with any case, default statement will be executed.