Thread: can you place a if/else statement in a switch case

  1. #1
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20

    can you place a if/else statement in a switch case

    can you place a if/else statement in a switch case?

    ex:

    case 4:
    answer=f+b
    cout<<sum<<answer<<endl;
    break;

    if (f > 5)
    else .......



    can i do this??

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Yes, try it. You can call functions, exit the program, write as much code as you want in a case block. Anything after the break and before the next case is ignored, tho.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    No, but you could do

    Code:
    switch(f)
    {
      case 4:
      break;
    
      default:
        if(f > 5)
        // something
        else
        // else
      break;
    }
    or

    Code:
    if(f > 5)
    // something
    else
    {
      switch(f)
      {
        case 4:
        break;
      }
    }
    or

    Code:
    if(f == 4)
    // something
    else if(f > 5)
    // else if
    else
    // else
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by MK27 View Post
    Yes, try it. You can call functions, exit the program, write as much code as you want in a case block. Anything after the break and before the next case is ignored, tho.
    Correct, so the answer is not "yes." He wants a case where the subject is bigger than 5. Meaning, he wanted something like this.. case f > 5: break; Which would be an interesting shortcut, but does not exist. Therefore the answer is "no."
    Last edited by Dae; 02-27-2010 at 08:46 PM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Feb 2010
    Location
    Seattle Washington
    Posts
    20
    so how could i write a statement that :

    asks for a letter ( i used D for division) when entered the program is going to ask the user for two numbers,but i need to divide the larger number by the smaller number.

    and i also need :

    when s ( subtraction is wanted) the program will calculate difference, subtracting the second number from the first number, but only when the first number is larger than the second number. and the program should swap both numbers before calculating the number

    its confusing me alot!!!

    i tried some stuff but it gave me errors

    ps: also when i am only using ( a-addition,s-subtraction,m-multiplication,d-division) but when i enter the letter J or any other it still asks for two number..i already placed the default in the switch case but it still asks ...HELP!!! lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM