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 - 4 of 4 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

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