When is a switch statement better than multiple if statements?

Posted by : Mitu Sinha
Posted On : 9/24/2013 10:13:09 PM
Posted As :Question Answers
Category :Software Development
View : 414
                 
A switch statement is generally best to use when you have more than two conditional expressions based on a single variable of numeric type. for Example :-- if (x == 1) printf("x is equal to one.\n"); else if (x == 2) printf("x is equal to two.\n"); else if (x == 3) printf("x is equal to three.\n"); else printf("x is not equal to one, two, or three.\n"); the following code is easier to read and maintain: switch (x) { case 1: printf("x is equal to one.\n"); break; case 2: printf("x is equal to two.\n"); break; case 3: printf("x is equal to three.\n"); break; default: printf("x is not equal to one, two, or three.\n"); break; }
 
when a Project really is Agile
Submitted in: Ask Question | Category: Project Management
what is Data Binding?
Submitted in: Ask Question | Category: Software Development
how to pass data from one activity to another activity?
Submitted in: Ask Question | Category: Software Development
What is Data Annotations ?
Submitted in: Ask Question | Category: Software Development
What is difference between $Scope and $rootScope ?
Submitted in: Ask Question | Category: Software Development
What is difference between TextBox and TextBoxFor?
Submitted in: Ask Question | Category: Software Development
how to send selected data to another page on button click
Submitted in: Ask Question | Category: Software Development
what is attribute routing in asp.net mvc
Submitted in: Ask Question | Category: Software Development
What’s an Object ?
Submitted in: Ask Question | Category: Software Development
What is the Android Architecture?
Submitted in: Ask Question | Category: Software Development
What is difference between account payable and bills payable?
Submitted in: Ask Question | Category: Accounting
What is the diff. between abstract class & interface
Submitted in: Ask Question | Category: Software Developement

All rights reserved to Interviewfunda.com. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. While using this site, you agree to have read and accepted our terms of use and privacy policy. Best viewed at 1024 x 768 resolution with Internet Explorer 5.0 or Mozila Firefox 3.5 or Google Crome and higher