Thread: Switch/case vs if/else

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Babkockdood
    You can put as many statements as you want on one line.
    That is a self-defeating argument. This notion of "more compact" boils down to the specifics of subjective style. As such, it is a matter of you finding it more compact for your style. So be it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    Quote Originally Posted by Babkockdood View Post
    Yeah, I realized that. Look at the edit.
    This will work

    Code:
    if (!argv[1]) {printf("There are no arguments.\n"); return 1;}
    else {printf("There is at least one argument.\n"); return 0;}
    even this will work
    Code:
    if (!argv[1]) {printf("There are no arguments.\n"); return 1;} else {printf("There is at least one argument.\n"); return 0;}
    if you print whole program in one line it will work depending upon the compiler you are using.
    Last edited by Dr.Xperience; 09-19-2010 at 01:00 AM.

  3. #18
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ...Which is the same as:
    Code:
    switch (y)
    {
        case 1: printf("There are no arguments.\n"); return 1; break;
        case 2: printf("There is at least one argument.\n"); return 0; break;
    }
    And the more you do it, the less pretty it becomes. So eventually you have to put it on multiple rows.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #19
    Registered User
    Join Date
    Sep 2010
    Posts
    32
    yup
    I will use switch case when i have to compare single value with multiple value. For comparing one value or to compare multiple values with multiple values i will use if else. Though both have same use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with cin.fail() in if/else clause
    By clearcom in forum C++ Programming
    Replies: 1
    Last Post: 04-17-2010, 10:11 PM
  2. can you place a if/else statement in a switch case
    By swansea in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2010, 08:59 PM
  3. Need help with if/else and switch commands please
    By masterofwar14 in forum C++ Programming
    Replies: 4
    Last Post: 09-29-2009, 12:28 AM
  4. Issues with using CHAR with IF/ELSE conditions.
    By TheBlackVeil in forum C++ Programming
    Replies: 5
    Last Post: 07-31-2007, 10:58 AM
  5. If/else statement not working
    By zenovy in forum C++ Programming
    Replies: 1
    Last Post: 01-18-2006, 08:26 PM

Tags for this Thread