如果条件怎么写。? [英] How to write if condition.?

查看:82
本文介绍了如果条件怎么写。?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我想写一个开关案例陈述,这样,



for每个案例我都会检查条件。例如



开关(expr)



案例(如果要写在这里的陈述):



如何实现这一功能。



我的尝试:



开关(expr)

case if if(i%3 == 0&& i%5 == 0)//抛出编译时间错误。

Hello All,

I want to write the switch case statement such that ,

for each case i will be checking condition. e.g.

switch(expr)

case (if statement to be written here):

how to achieve this functionality.

What I have tried:

switch(expr)
case if(i%3==0 && i%5==0) // throwing compile time error.

推荐答案

有可能,你不需要一个switch语句,但是 if(){} else if(){}而不是(){} 声明。



这里, expr 似乎没有彼此直接相关,所以开关语句不是一个选项。
Chances are, you do not need a switch statement there, but a if () { } else if () { } else () { } statement instead.

Here, expr and i do not seem to be directly related to each other, so a switch statement is not an option.


尝试类似:

Try something like:
if (condition 1)
    {}
else if (condition 2)
    {}
else
    {}





建议!花些时间来正确学习语言。

以下是语言作者对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 ++编程语言 [ ^ ]



[UpDate]



Advice! Take time to learn properly the language.
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[^]

[UpDate]

引用:

我已经知道你提供的东西但我想要写下这个案例陈述

或者更多代替案例1我想要案例如果(条件表达式)

i already know the thing you provided but i want to write the case statement as below
or moreover in place of case 1 i want case if(conditional expression)

它在语言中不存在!



[UpDate]

It does not exist in the language !

[UpDate]

引用:

我想用switch case优化下面的代码声明。



i已编写此代码。

I want to optimize the below code using switch case statement.

i have already written this code.

if (i % 3 == 0 && i % 5 == 0)
                {
                    Console.WriteLine("Both True");
                }
                if (i % 3 == 0)
                {
                    Console.WriteLine("Three True");
                }
                if (i % 5 == 0)
                {
                    Console.WriteLine("Five True");
                }

你做不到!它已经优化了!

You can't ! it is already optimized !


这没有意义(参见切换(C#参考) [ ^ ] )



你可以写

Well that's meaningless (see switch (C# Reference)[^])

You could write
switch (expr)
{
case 0:
  if(i%3==0 && i%5==0)
  {
    //...
  }
  break;
case 1:
  //...  
}


这篇关于如果条件怎么写。?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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