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 - 42 of 42 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.

vijju

  • 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

RAHUL

  • Aug 8th, 2013
 

IF: it works under the Boolean literal whereas

SWITCH: it works under the integer/character literal

  Was this answer useful?  Yes

LOGAN

  • Oct 9th, 2015
 

IF always working when it is with else part...
ex:
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.

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

  Was this answer useful?  Yes

Reshma

  • 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;
break;
case 2: 20;
break;
case 3: 30;
break;
default: // one has to match among set of values.

b) If and else checks to chose among two options
if(age>18)
System.out.println("eligible);
else
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

aiman

  • Dec 30th, 2015
 

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

  Was this answer useful?  Yes

komalnk

  • Mar 1st, 2016
 

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 alternative should be executed

  Was this answer useful?  Yes

Ashraf

  • May 10th, 2017
 

we can use " else" alone inf if-else as we use "default" in switch.

  Was this answer useful?  Yes

crow

  • May 18th, 2017
 

The main difference is that switch despatches immediately to the case concerned, typically via an indexed jump, rather than having to evaluate all the conditions that would be required in an if-else chain, which means that code at the end of the chain is reached more slowly than code at the beginning.

net-informations com / java / cjava

Crow

  Was this answer useful?  Yes

Shahzad

  • May 29th, 2017
 

No. There is also a default statement in the if-else structure. That is, else statement. Conceptually, else statement in if-else structure do the same work as default statement do in switch structure. i.e. both statements will be executed in the case of all the remaining possibilities or conditions.

  Was this answer useful?  Yes

Aska

  • Dec 27th, 2017
 

If statement is bidirectional flow of control

  Was this answer useful?  Yes

Moamed Rafeek

  • Apr 12th, 2018
 

"If-else" and "switch" both are selection statements. The selection statements, transfer the flow of the program to the particular block of statements based upon whether the condition is "true" or "false". The fundamental difference between if-else and switch statements is that the if-else statement "selects the execution of the statements based upon the evaluation of the expression in if statements". The switch statements "selects the execution of the statement often based on a keyboard command".

  Was this answer useful?  Yes

Poorvi Pathak

  • May 8th, 2018
 

"If" is a selection statement. If the condition evaluates to true course of action/statement is executed otherwise it is ignored.
"Switch" is also a selection statement. The switch statement test the value of an expression against a list of integer or character constant.

  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.

 

Related Answered Questions

 

Related Open Questions