GeekInterview.com
Series: Subject: Topic:
Question: 1914 of 2109

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.
Asked by: Interview Candidate | Asked on: Aug 25th, 2004
Showing Answers 1 - 4 of 4 Answers
Amarendra Kumar

Answered On : 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.

Yes  1 User has rated as useful.
  
Login to rate this answer.
wanttochange

Answered On : Jan 10th, 2008

View all answers by wanttochange

You should ask wat's the difference between if..else & switchwhen to use what

  
Login to rate this answer.
vijju

Answered On : 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

  
Login to rate this answer.
RAHUL

Answered On : Aug 8th, 2013

IF: it works under the Boolean literal whereas

SWITCH: it works under the integer/character literal

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.