Easy if-else OR switch-case

This is a discussion on Easy if-else OR switch-case within the Tech Board forums, part of the Community Boards category; I wonder... Which is easier if-else statement or switch-case statement to be learned for first?...

  1. #1
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489

    Question Easy if-else OR switch-case

    I wonder... Which is easier if-else statement or switch-case statement to be learned for first?
    Just GET it OFF out my mind!!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,397
    I think that if-else statements may be easier to learn since you would not also need to explain the concept of fall through (assuming that the programming language's switch construct has such a feature) while explaining the concept of control flow branching.
    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

  3. #3
    Ugly C Lover audinue's Avatar
    Join Date
    Jun 2008
    Location
    Indonesia
    Posts
    489
    However, I just confused with if (nested statement) which is a part of if statement.

    For instance, I'll give this easy-to-understand (I think) for my friends...
    Code:
    bool hungry = true;
    if( hungry ) {
      print( "Eating some food..." );
    }
    print( "Go to campus!" );
    That's mean whenever you hungry you'll eat some food first, even if you're not you will still go to the campus.

    But, I still don't get the *right* example (analogy) for the nested one.

    Code:
    bool hungry = true;
    if( hungry ) {
      if( momCooking ) {
        print( "Eating some food..." );
      } else {
        print( "Go to restaurant to eat." );
      }
    }
    print( "Go to campus!" );
    Obviously the code above still can be changed to:

    Code:
    bool hungry = true;
    if( hungry && momCooking ) {
        print( "Eating some food..." );
    } else if( hungry && !momCooking ) {
        print( "Go to restaurant to eat." );
    }
    print( "Go to campus!" );
    Ohhh yeah... I don't get it.
    Just GET it OFF out my mind!!

  4. #4
    Reverse Engineer maxorator's Avatar
    Join Date
    Aug 2005
    Location
    Estonia
    Posts
    2,318
    Quote Originally Posted by audinue View Post
    Code:
    bool hungry = true;
    if( hungry && momCooking ) {
        print( "Eating some food..." );
    } else if( hungry && !momCooking ) {
        print( "Go to restaurant to eat." );
    }
    print( "Go to campus!" );
    Ohhh yeah... I don't get it.
    That bold part is not necessary anymore, since mom obviously isn't cooking.
    "The Internet treats censorship as damage and routes around it." - John Gilmore

  5. #5
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    Actually, mom may be cooking and we just aren't hungry.
    Code:
    if ( hungry && momCooking )
    can be false if hungry is false and momCooking is true, if hungry is true and momCooking is false, or if hungry is false and momCooking is false.

    In this case, I would say it is irrelevant whether or not momCooking until we determine hungry, so the "outter" if statement should only check whether hungry is true or not. If it isn't, we don't even need to bother checking for momCooking.

    Code:
    bool hungry = true;
    if( hungry ) {
      if( momCooking ) {
        print( "Eating some food..." );
      } else {
        print( "Go to restaurant to eat." );
      }
    }
    print( "Go to campus!" );
    is much clearer.

    I would only combine expressions in an if statement (or a [do-]while/for condition) if each expression must be resolved before we determine a course of action.
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

  6. #6
    verbose cat
    Join Date
    Jun 2003
    Posts
    209
    [edit] oops, I just noticed the bool hungry = true, so you're right in this case. However then I would say it is not useful in this case to have hungry even in the if statement since we are really only checking to see whether momCooking or not.

    However, if we remove the initialization to true and instead assume that hungry might be true or false (determined earlier in the code), then my statements are valid.[/edit]

    [edit2]gah, sorry for the double post, hit the wrong button.[/edit2]
    abachler: "A great programmer never stops optimizing a piece of code until it consists of nothing but preprocessor directives and comments "

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Replies: 5
    Last Post: 03-05-2009, 10:32 AM
  3. function switch help
    By etbjr182 in forum C Programming
    Replies: 9
    Last Post: 02-20-2009, 02:51 PM
  4. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 04:50 PM
  5. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM

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