What is the difference between an if statement and a switch statement

The if statement is used to select among two alternatives. It uses a boolean expression todecide which alternative should be executed. The switch statement is used to select among multiple alternatives. It uses an int expression to determine which alternativeshould be executed.

Showing Answers 1 - 7 of 7 Answers

Amarendra Kumar

  • Aug 8th, 2007

IF Statement: Checks the value of data is less than or greater than. (in ranges).
example: can tell wether an input age is more than 18 and less than 60.

Switch Case: Checks the value of data that is prespecified. only equal to.

example: Can only generate output if the value matches. When the age is 18 or when the age is 60 . No comarison of data based on greater than or smaller than. Compares data based on equality.


  • Oct 9th, 2011

The if statement can be used to test conditions so that we can alter the flow of a program. In other words: if a specific statement is true, execute this instruction. If not true, execute this instruction.

The switch statement is almost the same as an if statement. The switch statement can have many conditions. You start the switch statement with a condition. If one of the variable equals the condition, the instructions are executed. It is also possible to add a default. If none of the variable equals the condition the default will be executed

  Was this answer useful?  Yes


  • Aug 8th, 2013

IF: it works under the Boolean literal whereas

SWITCH: it works under the integer/character literal

  Was this answer useful?  Yes


  • Oct 9th, 2015

IF always working when it is with else part...
if(n1>n2) {
Printf("TRUE! n1 Is Greater");
} else {
Printf("False! n2 Is Greater");

Switch statement:
It is the MultiConditional control statement where it checks the value of switch choice if it is matched then that case will be Executed directly.
Otherwise it goes to default condition case.

switch(grade) {
case 1:
case 2:
printf("First Class");
case 3:
printf("Second Class");
case 4:printf("Fail");
printf("Result Declared");

  Was this answer useful?  Yes


  • Dec 8th, 2015

a)If statement: we can check for multiple condition using "and" where as in switch statement there is no such option.
eg: if(age>10 and age<50) // checks for a range of value
where as swith(age)
case 1: 10;
case 2: 20;
case 3: 30;
default: // one has to match among set of values.

b) If and else checks to chose among two options
System.out.println("not eligible");
No such option in switch

c) Instead of using too many if else statements better go for switch statements

  Was this answer useful?  Yes


  • Dec 30th, 2015

In "if" statement we cannot use default statement
In "switch" statement we use default statement.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Answer Question

Click here to Login / Register your free account

Send   Reset


Related Answered Questions


Related Open Questions