Switch/case vs if/else

This is a discussion on Switch/case vs if/else within the C Programming forums, part of the General Programming Boards category; Originally Posted by Babkockdood You can put as many statements as you want on one line. That is a self-defeating ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    ...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.

Page 2 of 2 FirstFirst 12
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, 07: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, 07:26 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21