将if语句切换 [英] Put if statements to switch

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

问题描述

这是我的一段代码。正如您所看到的,有一些if语句与我的结构一起使用。我正在考虑将它们转换为switch语句,但我不太清楚如何。也许有人可以给我一个提示?



我尝试过:



This is a piece of my code. As you can see there are some if statements that are working with my structure. I was thinking about converting them onto a switch statement but I´m not quite sure how. Maybe someone could give me a hint?

What I have tried:

if(structure[X].number== -1)
{
    structure[X].number= number;
    structure[X].first= def;
    structure[X].second= def;
    break;
  }
  if(structure[X].number== number)
  {
    if(structure[X].first== -1)
      structure[X].first= def;
      structure[X].second= def;
      break;
}

推荐答案

你不能:切换语句 case 值不能左值 [ ^ ]:它们必须是编译时常量。因此,您与数字的比较根本不能用于案例
You can't: switch statement case values cannot be lvalues[^]: them must be compile time constants. So your comparison to number cannot be used in a case at all.


Quote:

我正在考虑将它们转换为switch语句,但我不太确定如何。也许有人可以给我一个提示?

I was thinking about converting them onto a switch statement but I´m not quite sure how. Maybe someone could give me a hint?



阅读文档总是一个好的开始:

在C中切换语句 [ ^ ]



以下是语言作者参考C和C ++书籍的链接。注意,C是C ++的祖先,所以知道C对C ++总是有用。

C编程语言 - 维基百科,免费的百科全书 [ ^ ]

https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2。 pdf [ ^ ]

http://www.ime.usp。 br / ~pf / Kernighan-Ritchie / C-Programming-Ebook.pdf [ ^ ]



C ++编程语言 [ ^ ]


Reading documentation is always a good start:
switch statement in C[^]

Here is links to references books on C and C++ by the authors of the languages. Note than C is the ancestor of C++, so knowing C is always useful with C++.
The C Programming Language - Wikipedia, the free encyclopedia[^]
https://hassanolity.files.wordpress.com/2013/11/the_c_programming_language_2.pdf[^]
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf[^]

C++ Programing Language[^]


这篇关于将if语句切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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