...

View Full Version : Is nesting switch statements possible?



azrael
05-01-2004, 11:39 PM
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!

swmr
05-02-2004, 01:12 AM
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?

azrael
05-02-2004, 10:31 AM
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.

swmr
05-02-2004, 04:24 PM
Between that description & the first example, I'm still unclear about the details... but here's a start, perhaps:


<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>

swmr
05-02-2004, 06:18 PM
Ok, it may seem ridiculous to go from string to number to boolean to number to string, but its fun... :D

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;}

azrael
05-02-2004, 06:59 PM
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.

azrael
05-11-2004, 08:39 AM
All working now, thanks very much for your help!

Tails
05-11-2004, 06:43 PM
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?



//The case isn't true
switch(4)
{
case 8: alert(8)
}




/* 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?

swmr
05-11-2004, 08:37 PM
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...

liorean
05-11-2004, 09:28 PM
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 149971



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum