在switch结构中嵌套if语句 [英] Nesting if statements in switch structures

查看:1548
本文介绍了在switch结构中嵌套if语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以将一个条件if结构嵌套在

条件开关结构中?


开关(freq)

{

案例1:CASENAME

如果(变量== 1)

{

做一些代码

}

其他

{

做一些其他代码

}

休息;

案例2:

Is it possible to have a conditional if structure nested inside a
conditional switch structure?

switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:

推荐答案

Technoid写道:
Technoid wrote:
是否可以将条件if结构嵌套在
条件开关结构中?


是的。

开关(频率)
{
案例1:CASENAME
if(variable == 1)
{
做一些代码
}

{
做一些其他代码
}
休息;
案例2:

编译器说if语句有语法错误。
Is it possible to have a conditional if structure nested inside a
conditional switch structure?
Yes.
switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:

The compiler says we have a syntax error on the if statement.




我不知道CASENAME是什么意思但它是一个语法错误,

其他一切看起来都没问题,从概念上讲。如果你提供了导致错误的确切代码和确切的

错误信息,那会更有帮助

,为什么你不这样做?


Robert Gamble



I don''t know what CASENAME is supposed to be but it is a syntax error,
everything else looks okay, conceptionally. It would much more helpful
if you provided the exact code that caused the error and the exact
error message, why wouldn''t you do this?

Robert Gamble





Technoid写于04/04/06 15 :19,:


Technoid wrote On 04/04/06 15:19,:
是否可以将一个条件if结构嵌套在
条件开关结构中?


是的。

开关(频率)
{
案例1:CASENAME
if(variable == 1)
{
做一些代码
}

{
做一些其他代码
}
休息;
案例2:




编译器说我们在if语句中有语法错误。
Is it possible to have a conditional if structure nested inside a
conditional switch structure?
Yes.
switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:
.
.
.

The compiler says we have a syntax error on the if statement.



为初学者摆脱`CASENAME''。然后

用有效代码替换'做一些代码'和`做一些其他代码''




-
Er*********@sun.com



Get rid of `CASENAME'', for starters. Then
replace `do some code'' and `do some other code''
with valid code.

--
Er*********@sun.com


Technoid schrieb:
Technoid schrieb:
是否可以将条件if结构嵌套在
条件开关结构中?


是的。

开关(频率)
{
案例1:CASENAME
if(variable == 1)
{
做一些代码
}

{
做一些其他代码
}
休息;
案例2:




编译器说我们在if语句中有语法错误。
Is it possible to have a conditional if structure nested inside a
conditional switch structure?
Yes.
switch(freq)
{
case 1: CASENAME
if (variable==1)
{
do some code
}
else
{
do some other code
}
break;
case 2:
.
.
.

The compiler says we have a syntax error on the if statement.



这很好地说明了为什么提供

a编译最小例子是有意义的。如果上面的代码是从

复制的,那么删除CASENAME。

除此之外,请考虑


#include< stdio .h>


enum A_VAL {SOME_VAL,SOME_OTHER_VAL,TEN_VAL = 10};


#define MAGIC 42

int main(无效)

{

int a;

double b;


a = TEN_VAL;

b = 7.5;


开关(a){

案例SOME_VAL:

案例SOME_OTHER_VAL:

put(AllowedVal);

休息;

案例TEN_VAL:

if(b * MAGIC> = a + MAGIC){

puts(TenMagic);

}否则{

puts (TenMundane);

}

休息;

默认值:

puts(" Default" );

休息;

}


返回0;

}


编译和链接。


干杯

Michael

-

电子邮件:我是一个/ at / gmx / dot / de address。



This nicely illustrates why it makes sense to provide
a compiling minimal example. If the above is copied from
your code, then remove CASENAME.
Apart from that consider

#include <stdio.h>

enum A_VAL { SOME_VAL, SOME_OTHER_VAL, TEN_VAL = 10 };

#define MAGIC 42

int main (void)
{
int a;
double b;

a = TEN_VAL;
b = 7.5;

switch (a) {
case SOME_VAL:
case SOME_OTHER_VAL:
puts("AllowedVal");
break;
case TEN_VAL:
if (b * MAGIC >= a + MAGIC) {
puts("TenMagic");
} else {
puts("TenMundane");
}
break;
default:
puts("Default");
break;
}

return 0;
}

Compiles and links.

Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


这篇关于在switch结构中嵌套if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆