在本文中,我们将详细介绍如何退出if子句的各个方面,并为您提供关于如何退出if语句的相关解答,同时,我们也将为您带来关于asp-classic–If子句未按预期工作、bash–有没有办法在退出时执行命
在本文中,我们将详细介绍如何退出if子句的各个方面,并为您提供关于如何退出if语句的相关解答,同时,我们也将为您带来关于asp-classic – If子句未按预期工作、bash – 有没有办法在退出时执行命令(无论脚本如何退出)?、c# – 当我在if子句中放置一个关闭div标签时,Razor抱怨、C模板实例化取决于if子句的有用知识。
本文目录一览:- 如何退出if子句(如何退出if语句)
- asp-classic – If子句未按预期工作
- bash – 有没有办法在退出时执行命令(无论脚本如何退出)?
- c# – 当我在if子句中放置一个关闭div标签时,Razor抱怨
- C模板实例化取决于if子句
如何退出if子句(如何退出if语句)
有哪些方法可以过早退出if
子句?
有时候,我在编写代码时希望将一条break
语句放在if
子句中,只是要记住,这些语句只能用于循环。
让我们以以下代码为例:
if some_condition: ... if condition_a: # do something # and then exit the outer if block ... if condition_b: # do something # and then exit the outer if block # more code here
我可以想到一种方法:假设退出情况发生在嵌套的if语句内,请将剩余的代码包装在一个大的else块中。例:
if some_condition: ... if condition_a: # do something # and then exit the outer if block else: ... if condition_b: # do something # and then exit the outer if block else: # more code here
问题是更多的退出位置意味着更多的嵌套/缩进代码。
另外,我可以编写我的代码以使if
子句尽可能小,并且不需要任何退出。
有人知道退出if
子句的好/更好的方法吗?
如果存在任何关联的else-if和else子句,我认为退出将跳过它们。
答案1
小编典典(此方法适用于if
s,多个嵌套循环和您break
不易使用的其他构造。)
将代码包装在自己的函数中。代替break
使用return
。
例:
def some_function(): if condition_a: # do something and return early ... return ... if condition_b: # do something else and return early ... return ... returnif outer_condition: ... some_function() ...
asp-classic – If子句未按预期工作
if ucase(displayanzeige) = "Y" \ or isnull(displayanzeige) \ and ucase(bstatus) = "2" \ or ucase(bstatus) = "1"
if子句的第一部分正在工作:
if ucase(displayanzeige) = "Y" \ or isnull(displayanzeige)
但第二部分不起作用:
and ucase(bstatus) = "2" \ or ucase(bstatus) = "1"
if子句有什么问题?请帮忙
解决方法
if (ucase(displayanzeige) = "Y" or isnull(displayanzeige)) and (ucase(bstatus) = "2" or ucase(bstatus) = "1")
你目前有:
1 or 2 and 3 or 4
系统可以将此解释为“1必须为真或2和3或4必须为真”
放置括号如下:
(1 or 2) and (3 or 4)
会告诉系统“1或2必须为真,3或4必须为真”
bash – 有没有办法在退出时执行命令(无论脚本如何退出)?
trap 'rm filename' EXIT
(参见§4.1 “Bourne Shell Builtins” of the Bash Reference Manual中对陷阱的描述.)
当然,在极端情况下命令将不会运行 – 例如,如果有人突然拔出机器,或者甚至只是将SIGKILL发送到运行脚本的Bash实例 – 但这几乎可以获得.
c# – 当我在if子句中放置一个关闭div标签时,Razor抱怨
@if(isNew) { <div> } ... @if(isNew) { </div> }
错误是:
cannot resolve the symbol 'div'
Razor不喜欢IF子句中的结尾div标签,我该如何让它工作?有没有逃脱我不得不使用?
解决方法
@if(isNew) { @:<div> } ... @if(isNew) { @:</div> }
更常规的方法是在div内重复标记(使用部分或其他方式) – 哪种方法更可取,我会说,取决于条件div之间的标记的性质:
@if(isNew) { <div> <!-- some markup or partial view --> </div> } else { <!-- some markup or partial view --> }
我喜欢这种方法:
>所包含的标记可以有用地包含在部分中,以便在其他地方可重用.
>条件包装标记超过几行,转义标记在这一点变得凌乱.
我还应该添加使用帮助方法的选项:
@helper MainMarkup() { <!-- some markup or partial view --> } @if(isNew) { <div> @MainMarkup() </div> } else { @MainMarkup() }
如果要使用上述第二个选项,但避免重复标记或嵌套太多部分(特别是如果此标记仅与此视图相关),则此功能非常有用.
C模板实例化取决于if子句
if(dimension == 2) { typedef itk::Image<short,2> ImageType; typedef itk::Image<unsigned int,2> IntegralImageType; m_pApp->train<2,ImageType,IntegralImageType>(); } else { typedef itk::Image<short,3> ImageType; typedef itk::Image<unsigned int,3> IntegralImageType; m_pApp->train<3,IntegralImageType>(); }
但我想这样做:
if (dimension == 2) DIMENSION = 2; else DIMENSION = 3; typedef itk::Image<short,DIMENSION> ImageType; typedef itk::Image<unsigned int,DIMENSION> IntegralImageType; m_pApp->train<DIMENSION,IntegralImageType>();
我无法做到这一点,因为c需要const变量用于模板实例化.
有没有这样的方法呢?
解决方法
template<unsigned N> void train(){ typedef itk::Image<short,N> ImageType; typedef itk::Image<unsigned int,N> IntegralImageType; m_pApp->train<N,IntegralImageType>(); }
然后:
if (dimension == 2) train<2>(); else train<3>();
请注意,此代码将实例化两个模板(将为它们生成代码),因为在编译时无法知道将使用哪个模板.
关于如何退出if子句和如何退出if语句的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于asp-classic – If子句未按预期工作、bash – 有没有办法在退出时执行命令(无论脚本如何退出)?、c# – 当我在if子句中放置一个关闭div标签时,Razor抱怨、C模板实例化取决于if子句的相关知识,请在本站寻找。
本文标签: