Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    May 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Is nesting switch statements possible?

    Hi,

    Hope someone can help.

    Is it possible to nest a switch statement inside another, using an if ..else statement?

    E.G :


    var A;
    varB;
    str_Grade;

    switch (var A){

    case: 1,2,3,4,5
    str_Grade = 'X';
    break;

    if (str_Grade == 'X') {
    case: 0
    var B = 1;
    break;

    case: 1
    var B = 2;
    break;

    case: 2
    var B = 3;
    break;

    default:
    varB =3;
    break;

    }

    else
    }

    case: 6,7,8,9
    str_Grade = 'Y';
    break;

    if (str_Grade == 'Y') {
    case: 0
    var B = 2;
    break;

    case: 1
    var B = 3;
    break;

    case: 2
    var B = 4;
    break;


    case: 3
    var B = 5;
    break;


    default:
    varB =5;
    break;

    }
    else

    }


    case: 10,11,12,13
    str_Grade = 'Z';
    break;

    if (str_Grade == 'Y') {


    case: 0
    var B = 3;
    break;

    etc. Hope you see where I am coming from.

    Can this be done, and if so, can someone help me out with the proper syntax?

    Also have a sneaking suspicion about case: 1,2,3,4,5 - is this possible, as in ASP, or would it have to be case:1, case:2, case:3, etc.?

    Any answers/ideas or links to examples greatly received!

  • #2
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Statements can be nested, but not that way.

    A switch statement can be condensed somewhat through using a more complex expression, but labels cannot be grouped.

    no clue of what your example is meant to accomplish though; what's the idea there?
    hmm... ?

  • #3
    New to the CF scene
    Join Date
    May 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question

    Hi, thanks for replying.

    The code is basically for a form which deals with pension contributions. If varA is age, as the pension contributions work in a table like this:

    Band A Age 18-23 Employeee contribution = 0-15 Employer cont. 2-3
    Band B Age 25-29 Employeee contribution = 0-15 Employer contr. 2-4
    Band C Age 30-39 Employeee contribution = 0-15 Employer contr. 2-6
    Band D Age 40-49 Employeee contribution = 0-15 Employer contr. 2-9
    Band E Age 50+ Employeee contribution = 0-15 Employer cont. 2-12

    Hence the bands.

    I have 2 separate switch statements working, one in asp and the other in Javascript, for a page which pulls the person's age out of the database, but this one is a general screen, so age is variable, and so trying to combine the 2 statements in Javascript.

    If a person is in a particular band, then they can get a company contribution of a differing amount (varB) based on their band value, upto a maxiimum, set as the default.

    In summary, the code gets value of age, checks what band they are in and then checks what their contribution, and corresponding company contribution is. Hope this is all clear!

    As I cannot group labels, (unlike in asp), can I use (>18 && <25), or should I try an array?

    Any help or an example of a "switch statement which has been condensed by using a more complex expression" would be greatly appreciated!

    Thanks again!

    A.

  • #4
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Between that description & the first example, I'm still unclear about the details... but here's a start, perhaps:
    Code:
    <html>
    <head>
    <title>-</title>
    
    <script type="text/JavaScript">
    function getBand(n){
    var Band;
    switch(+(n >= 18) +""+ +(n >= 25) +""+ +(n >= 30) +""+ +(n >= 40) +""+ +(n >= 50)){
    case "10000" : 
    Band = "A";
    break;
    case "11000" : 
    Band = "B";
    break;
    case "11100" : 
    Band = "C";
    break;
    case "11110" : 
    Band = "D";
    break;
    case "11111" : 
    Band = "E";
    break;}
    alert("Band: " + Band);
    }
    </script>
    
    </head>
    <body>
    
    <label>Age: </label>
    <select onchange="getBand(new Number(this.options[this.options.selectedIndex].text))">
    <optgroup label="Band A">
    <option>18</option>
    <option>19</option>
    <option>20</option>
    <option>21</option>
    <option>22</option>
    <option>23</option>
    <option>24</option>
    </optgroup>
    <optgroup label="Band B">
    <option>25</option>
    <option>26</option>
    <option>27</option>
    <option>28</option>
    <option>29</option>
    </optgroup>
    <optgroup label="Band C">
    <option>30</option>
    <option>31</option>
    <option>32</option>
    <option>33</option>
    <option>34</option>
    <option>35</option>
    <option>36</option>
    <option>37</option>
    <option>38</option>
    <option>39</option>
    </optgroup>
    <optgroup label="Band D">
    <option>40</option>
    <option>41</option>
    <option>42</option>
    <option>43</option>
    <option>44</option>
    <option>45</option>
    <option>46</option>
    <option>47</option>
    <option>48</option>
    <option>49</option>
    </optgroup>
    <optgroup label="Band E">
    <option>50</option>
    <option>51</option>
    <option>52</option>
    <option>53</option>
    <option>54</option>
    <option>55</option>
    </select>
    
    </body>
    </html>
    hmm... ?

  • #5
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, it may seem ridiculous to go from string to number to boolean to number to string, but its fun...

    same thing, but shorter:

    switch((n >= 18) + (n >= 25) + (n >= 30) + (n >= 40) + (n >= 50)){
    case 1 :
    Band = "A";
    break;
    case 2 :
    Band = "B";
    break;
    case 3 :
    Band = "C";
    break;
    case 4 :
    Band = "D";
    break;
    case 5 :
    Band = "E";
    break;}
    hmm... ?

  • #6
    New to the CF scene
    Join Date
    May 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Now that looks a lot simpler!

    Many thanks for your advice, much appreciated.

    I'll give it a try, and let you know how I get on.

    A.

  • #7
    New to the CF scene
    Join Date
    May 2004
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    All working now, thanks very much for your help!

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Posts
    672
    Thanks
    1
    Thanked 1 Time in 1 Post
    That's really creative. I never knew you could have a "macro" in the parenthesis of a switch(). Functions can't do such a thing since the variable cannot become a result (not even with window[]). Anyway, I have one question. If one case proves true and you don't 'break' it, does the next case have to be true for it's effect to fire?

    Code:
    //The case isn't true
    switch(4)
    {
    case 8: alert(8)
    }
    Code:
    /* The first case is true, it fires, it's not broken. The next case fires even though it's not true*/
    switch(4)
    {
    case 4: alert(4)
    case 8: alert(8)
    }
    Is there any logic behind this? I thought the use of break was to allow multiple IFs to fire statements, and continue falling. In the above example, it seems if one fires, they all fire until broken. How should this work?

  • #9
    Regular Coder
    Join Date
    Feb 2003
    Posts
    638
    Thanks
    0
    Thanked 0 Times in 0 Posts
    From the JScript Reference:

    If a label value equals expression, execute its accompanying statementlist.
    Continue execution until a break statement is encountered, or the switch statement ends. This means that multiple label blocks are executed if a break statement is not used.
    If "label block" refers to "statementlist", it seems that subsequent labels would not be compared.

    How this is useful, I'm not sure...
    hmm... ?

  • #10
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    It continues on the next statementlist, without doing a comparison of the next case clause.

    For an example of a practical use of this fallthrough, have a look at http://www.codingforums.com/showthread.php?p=149971
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •