Thread: Homework help! changing from a switch statement to if-else

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Homework help! changing from a switch statement to if-else

    Hi! This is my homework problem (for reference)
    Please change the switch statement in the following program into an if-else statement (else if or
    nested if-else statement may be necessary).
    startb.c
    Homework help! changing from a switch statement to if-else-startb-jpg
    and so far on my code i have gotten to here, but my book doesn't give any explanation on what the equivalent of break; default is in if-else so i don't really know what it means nor how to do that line. any suggestions?


    b.c
    Homework help! changing from a switch statement to if-else-current-b-jpg
    Last edited by Rachel Hyde; 03-07-2013 at 04:25 PM. Reason: easier to download picture than c file

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    break is not needed for if-else because it's a branching decision. Either the if-consequent is executed, or the else-alternate is executed. With switch, the behavior is to fall through (which can be useful) so you put breaks to avoid executing the successive statements.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    okay, so for my next line i would have } else if((number !=1)||(number!=-1)||(number !=0)); ?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Well, let's see if I can explain it in the most universal sense

    Code:
     if( predicate )
       consequent ;
    Here we execute the consequent, only conditionally. That is, only if the predicate is true. Otherwise, we move on to whatever code follows.
    Code:
      if( predicate )
         consequent ;
      else
         alternate ;
    In the above code, the predicate is tested for its boolean evaluation - true or false. If its true, the consequent is executed and the alternate is bypassed. If it is false, the consequent is bypassed and the alternate is executed. So, what part of this construct to you think is equivalent to 'default'?

    Code:
      if( predicate-1 )
         consequent-1 ;
      else if( predicate-2 )
         consequent-2 ;
    Where did the alternate statement go? The alternate is the following if statement! But note that the consequent-2 statement will only execute if the predicate-2 is true. If neither predicate is true, no consequent executes. I think it would be accurate to say that there is no 'default' in this snippet as default implies an 'if-all-else-fails' notion.

    Code:
      if( predicate-1 )
        consequent-1 ;
      else if( predicate-2 )
        consequent-2 ;
      else
        alternate ;
    Now we have an if with another if as its alternate, as in the previous snippet, but that second if now has its own alternate. So by now it should be clear that the unconditional else statement is the default. Don't forget though, we only branch to an else if the preceding predicate was false.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need homework help asap with if statement
    By automagp68 in forum C Programming
    Replies: 26
    Last Post: 01-27-2012, 02:19 PM
  2. Homework Help C Program Loop & Switch
    By 123456tacos in forum C Programming
    Replies: 7
    Last Post: 03-11-2011, 08:11 PM
  3. Replies: 3
    Last Post: 05-19-2010, 09:11 AM
  4. help with switch statement
    By agentsmith in forum C Programming
    Replies: 11
    Last Post: 08-26-2008, 04:02 PM
  5. help with switch statement?
    By Kristina in forum C++ Programming
    Replies: 10
    Last Post: 05-14-2002, 11:25 AM

Tags for this Thread