'if / else'与'case'条件开关 [英] 'if/else' versus 'case' conditional switch
问题描述
您好。我一直在靠墙打我的头,因为
长。
设置变量''z''或''y' '对于不同的数字,以下
''if / else''代码段工作正常;然而,''case''代码片段
却没有。 (代码的功能是说明性的。)
/////////////////////////// ///////////////
////////工作''if / else''切换////////
////////////////////////////////////////// />
var z = 3 / *设置''z''到任意数字* /
if(z< 10)
{
document.write("''z''的值小于十。
< br>< br>" )
}
else if(z == 10)
{
document.write(" The输入的''z'值恰好等于
十。< br>< br>")
}
否则如果(z> 10)
{
document.write("''z''的值大于十。
< br>< br>")
}
//////////////// //////////////////////////
$ b $ // //////////////// //////////////////////////
$ b $ // //////////////// //////////////////////// //
//////////////////////////////////// //////
///////非工作''案例''开关////////
//// //////////////////////////////////////
var y = 15 / *设置''y''到任意数字* /
开关(y)
{
case(y< 10):
document.write("''y''的值小于10。
< br>< br> ;")
break
case(y == 10):
document.write("''y'的值'进入正好是十。 < br>< br>")
break
case(y> 10):
document.write(&输入的''y'值大于十。
< br>< br>")
////// ////////////////////////////////////
////// ////////////////////////////////////
////// ////////////////////////////////////
任何简单是什么改变会使''案例''切换工作?
tia,
c。
hello. i''ve been beating my head against a wall over this for too
long.
setting the variables ''z'' or ''y'' to differing numbers, the following
''if/else'' code snippet works fine; however, the ''case'' code snippet
does not. (the code''s function is illustrative.)
//////////////////////////////////////////
//////// working ''if/else'' switch ////////
//////////////////////////////////////////
var z=3 /* set ''z'' to any number */
if (z<10)
{
document.write("The value of ''z'' entered is less than ten.
<br><br>")
}
else if (z==10)
{
document.write("The value of ''z'' entered is exactly equal to
ten.<br><br>")
}
else if (z>10)
{
document.write("The value of ''z'' entered is greater than ten.
<br><br>")
}
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////
/////// non-working ''case'' switch ////////
//////////////////////////////////////////
var y=15 /* set ''y'' to any number */
switch (y)
{
case (y<10):
document.write("The value of ''y'' entered is less than ten.
<br><br>")
break
case (y==10):
document.write("The value of ''y'' entered is exactly ten. <br><br>")
break
case (y>10):
document.write("The value of ''y'' entered is greater than ten.
<br><br>")
//////////////////////////////////////////
//////////////////////////////////////////
//////////////////////////////////////////
any simple changes that will make the ''case'' switch work?
tia,
c.
推荐答案
cl ****** *******@hotmail.com 说:
///////////////////// /////////////////////
///////非工作''案例''开关////////
//////////////////////////////////////////
var y = 15 / *设置''y''到任意数字* /
开关(y)
{
情况(y< 10):
case( y == 10):
案例(y> 10):
任何简单的更改都会使''case''切换工作?
//////////////////////////////////////////
/////// non-working ''case'' switch ////////
//////////////////////////////////////////
var y=15 /* set ''y'' to any number */
switch (y)
{
case (y<10):
case (y==10):
case (y>10): any simple changes that will make the ''case'' switch work?
阅读本手册可能有所帮助。
http://docs.sun。 com / source / 816-6408-10 / stmt.htm#1018610
案例标签应该是表达式,其值为
与switch表达式的值相比,而不是
布尔表达式。
Reading the manual might help.
http://docs.sun.com/source/816-6408-10/stmt.htm#1018610
The case labels are supposed to be expressions whose values
are compared to the value of the switch expression, not
boolean expressions.
对于一个相对较新的javascript的人来说是一个合理的误解
(或编码)。
if / else条件检查不等同于开关检查。
不是。
检查开关的结构是什么:
开关(是)
你看,你已经通过一个变量,y。
因此,它在所有案例陈述中都被视为暗示,
隐含地将其与传递给案件的价值(所以:
在案例陈述中不要重复''y'):
switch(y){
案例202:
休息;
案例''dunno'':
break;
}
即,switch语句在变量y中得到,而CASE
语句进入只有它可能或假设的价值。
因此,限制是如果(y == something),switch语句不是一个
伪装。
压力落在==运算符上。
交换机只检查等价,而不是其他类型比较:==是
暗示。
你应该认为它与将函数传递给函数一样,
with也许你已经熟悉了:
function foo(arg){alert(arg)}
当你调用那个签名时,arg命名变量被替换为
其价值:
foo(''hallo'');
显然足够触发al alert并写入'hallo''。
你不要重复arg,事实上:
foo(arg =''hallo'');
这是错的。
>
请注意,在某些语言中,例如PHP,函数的SIGNATURE(不在
调用中)可以改为以该形式出现,
表示分配DEFAULT:
函数foo(
A reasonable misunderstanding for a person relatively new to javascript
(or coding).
The if/else conditional checks are NOT equivalent to a switch check.
Not.
Examine what the structure of a switch is:
switch(y)
you see, you passed already a variable, y.
As such, it is regarded as IMPLIED within all the case statements,
which IMPLICITLY compare it against the value passed to the case (so:
don''t repeat ''y'' within the case statements):
switch(y){
case 202:
break;
case ''dunno'':
break;
}
that is, a switch statements gets in the variable y, and the CASE
statements gets in its possible or presumed VALUES ONLY.
As such, the limitation is that a switch statement isn''t but an
if(y==something)
check in disguise.
The stress falls on the == operator.
A switch checks ONLY equivalences, NOT other type of comparisons: == is
IMPLIED.
You should consider it the same as passing arguments to a function,
with which perhaps you''re already familiar:
function foo(arg){alert(arg)}
when you invoke that signature, the arg named variable is replaced with
its VALUES:
foo(''hallo'');
that obviously enough triggers al alert with written in it ''hallo''.
You do NOT repeat arg, in fact:
foo(arg=''hallo'');
that''s wrong.
Please note that in some languages, like PHP, the SIGNATURE (NOT in the
invocation, that is) of a function can instead appear in that form,
which means assigning a DEFAULT:
function foo(
arg =''hallo''){print
arg=''hallo''){print
这篇关于'if / else'与'case'条件开关的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!