Thread: What is possible with a switch?

  1. #1
    -Oscuro-
    Guest

    What is possible with a switch?

    Can you have boolean expressions,if statements...in a switch for each case? and what are legal names for the different cases like case 1, case 2... can u use variables instead of numbers?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Can you have boolean expressions
    Yes, but most compilers will give a warning about this because the switch expression must be an integral value. This can be fixed by assigning the return value of an expression to a variable and using that as the switch variable.

    >if statements...in a switch for each case?
    No, an if statement is not an expression. You can use the ternary operator though, provided it returns an integral value.

    >and what are legal names for the different cases like case 1,
    >case 2... can u use variables instead of numbers?
    Anything that is a valid constant such as '1', 'b', 45, 12. You cannot use variables since they aren't constant values.

    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM