4 Replies Latest reply on Dec 22, 2008 11:15 AM by Newsgroup_User

    Switch Question

    Level 7
      Dear All.,

      I was wondering if you can have more than one item in a switch element? For
      example:

      switch (_root.PressieRound) {
      case 1 :
      etc.

      ...but can you have
      switch (_root.PressieRound) {
      case 1 or 2 or 3 :

      ... or

      switch (_root.PressieRound) {
      case Is Between 1 and 5 :

      I have never seen examples. I am trying to avoid having massive elements in
      the switch statement. I am not sure what the components of a switch
      statement are so I have used what I think is maybe similar.

      Thanks again.

      Alasatair MacFarlane

        • 1. Re: Switch Question
          Level 7
          Alastair,

          > I was wondering if you can have more than one item in a switch element?
          > For example:
          > ...
          > switch (_root.PressieRound) {
          > case 1 or 2 or 3 :

          I've seen languages where this sort of thing is supported (Adobe
          Director's Lingo comes to mind), but I believe the best you'll get in
          ActionScript or JavaScript is to group your OR cases together and leave some
          of them blank:

          var temp:Number = 100;
          switch (temp) {
          case 99:
          case 100:
          case 101:
          trace("At or near 100");
          break;
          case 102:
          case 103:
          case 104:
          trace("Higher than, but near 100");
          break;
          // ...
          }

          In this sort of arrangement, the break statements are particularly
          important, as they mark the end of a group.

          > ... or
          >
          > switch (_root.PressieRound) {
          > case Is Between 1 and 5 :

          To assess a range, you could do this:

          var temp:Number = 100;
          switch (temp >= 99 && temp <= 101) {
          case true:
          trace("At or near 100");
          }

          ... but in that case, an if statement is more elegant.


          David Stiller
          Co-author, Foundation Flash CS4 for Designers
          http://tinyurl.com/5j55cv
          "Luck is the residue of good design."


          • 2. Re: Switch Question
            Level 7
            David,

            Thanks again for the reply. I am relatively new to Actionscript. I didn't
            realise you could use statements like:

            switch (temp >= 99 && temp <= 101)

            It seems so straightforward now that you have shown me an example. I should
            have thought of that one.

            Thanks for answering my question.

            Alastair



            "David Stiller" <stiller@quip-remove-.net> wrote in message
            news:gioe3e$fu$1@forums.macromedia.com...
            > Alastair,
            >
            >> I was wondering if you can have more than one item in a switch element?
            >> For example:
            >> ...
            >> switch (_root.PressieRound) {
            >> case 1 or 2 or 3 :
            >
            > I've seen languages where this sort of thing is supported (Adobe
            > Director's Lingo comes to mind), but I believe the best you'll get in
            > ActionScript or JavaScript is to group your OR cases together and leave
            > some of them blank:
            >
            > var temp:Number = 100;
            > switch (temp) {
            > case 99:
            > case 100:
            > case 101:
            > trace("At or near 100");
            > break;
            > case 102:
            > case 103:
            > case 104:
            > trace("Higher than, but near 100");
            > break;
            > // ...
            > }
            >
            > In this sort of arrangement, the break statements are particularly
            > important, as they mark the end of a group.
            >
            >> ... or
            >>
            >> switch (_root.PressieRound) {
            >> case Is Between 1 and 5 :
            >
            > To assess a range, you could do this:
            >
            > var temp:Number = 100;
            > switch (temp >= 99 && temp <= 101) {
            > case true:
            > trace("At or near 100");
            > }
            >
            > ... but in that case, an if statement is more elegant.
            >
            >
            > David Stiller
            > Co-author, Foundation Flash CS4 for Designers
            > http://tinyurl.com/5j55cv
            > "Luck is the residue of good design."
            >

            • 3. Re: Switch Question
              Level 7
              David,

              Thanks again for the reply. I am relatively new to Actionscript. I didn't
              realise you could use statements like:

              switch (temp >= 99 && temp <= 101)

              It seems so straightforward now that you have shown me an example. I should
              have thought of that one.

              Thanks for answering my question.

              Alastair



              "David Stiller" <stiller@quip-remove-.net> wrote in message
              news:gioe3e$fu$1@forums.macromedia.com...
              > Alastair,
              >
              >> I was wondering if you can have more than one item in a switch element?
              >> For example:
              >> ...
              >> switch (_root.PressieRound) {
              >> case 1 or 2 or 3 :
              >
              > I've seen languages where this sort of thing is supported (Adobe
              > Director's Lingo comes to mind), but I believe the best you'll get in
              > ActionScript or JavaScript is to group your OR cases together and leave
              > some of them blank:
              >
              > var temp:Number = 100;
              > switch (temp) {
              > case 99:
              > case 100:
              > case 101:
              > trace("At or near 100");
              > break;
              > case 102:
              > case 103:
              > case 104:
              > trace("Higher than, but near 100");
              > break;
              > // ...
              > }
              >
              > In this sort of arrangement, the break statements are particularly
              > important, as they mark the end of a group.
              >
              >> ... or
              >>
              >> switch (_root.PressieRound) {
              >> case Is Between 1 and 5 :
              >
              > To assess a range, you could do this:
              >
              > var temp:Number = 100;
              > switch (temp >= 99 && temp <= 101) {
              > case true:
              > trace("At or near 100");
              > }
              >
              > ... but in that case, an if statement is more elegant.
              >
              >
              > David Stiller
              > Co-author, Foundation Flash CS4 for Designers
              > http://tinyurl.com/5j55cv
              > "Luck is the residue of good design."
              >

              • 4. Re: Switch Question
                Level 7
                Alastair,

                > Thanks again for the reply. I am relatively new to Actionscript.
                > I didn't realise you could use statements like:
                >
                > switch (temp >= 99 && temp <= 101)

                Sure thing! Yeah, the switch statement itself does nothing more than
                evaluate expressions. In this case, the expression shown either evaulates
                to true or false, so you need at least one case case statement that checks
                for true. But again, for true/false, you may as well use an if statement.
                Bear in mind, you can next if statements inside your case statements. If
                you wanted to, you could even nest switch statements:

                var someValue = //whatever;
                switch (someValue) {
                case 5:
                case 10:
                if (someOtherValue) {
                // do this
                } else {
                // do that
                }
                break;
                case 15:
                case 20:
                switch (someOtherValue) {
                case ???:
                // do this
                break;
                case ???:
                // do that
                break;
                }
                break;
                }

                The trick is to balance out what's easiest to type (least number of
                keystrokes) with what's easiest to work with (too many nested levels becomes
                hard to read).


                David Stiller
                Co-author, ActionScript 3.0 Quick Reference Guide
                http://tinyurl.com/2s28a5
                "Luck is the residue of good design."