给出以下代码(不起作用):
while True:
#snip: print out current state
while True:
ok = get_input("Is this ok? (y/n)")
if ok.lower() == "y": break 2 #this doesn't work :(
if ok.lower() == "n": break
#do more processing with menus and stuff
Run Code Online (Sandbox Code Playgroud)
有没有办法让这项工作?或者我是否已经做了一次检查以突破输入循环,然后另一个更有限的检查外部循环以在用户满意的情况下将所有内容分开?
Edit-FYI: get_input是我编写的一个简短函数,支持显示提示和默认值以及所有那些幻想和返回__CODE__
假设我有这个伪代码:
bool conditionA = executeStepA();
if (conditionA){
bool conditionB = executeStepB();
if (conditionB){
bool conditionC = executeStepC();
if (conditionC){
...
}
}
}
executeThisFunctionInAnyCase();
Run Code Online (Sandbox Code Playgroud)
executeStepX当且仅当前一个成功时才应执行函数.无论如何,executeThisFunctionInAnyCase应该在最后调用该函数.我是编程的新手,对于这个非常基本的问题感到抱歉:有没有办法(例如在C/C++中)避免长if链产生那种"代码金字塔",代价是代码易读性?
我知道如果我们可以跳过executeThisFunctionInAnyCase函数调用,代码可以简化为:
bool conditionA = executeStepA();
if (!conditionA) return;
bool conditionB = executeStepB();
if (!conditionB) return;
bool conditionC = executeStepC();
if (!conditionC) return;
Run Code Online (Sandbox Code Playgroud)
但约束是executeThisFunctionInAnyCase函数调用.该break声明是否可以某种方式使用?
我一直在阅读关于Swift中的Optionals,我已经看过if let用于检查Optional是否包含值的示例,以及它是否存在 - 使用unwrapped值执行某些操作.
但是,我已经看到在Swift 2.0中,关键字guard主要用于.我想知道是否if let已从Swift 2.0中删除或是否仍然可以使用它.
我应该改变我的计划包含if let到guard?
过早退出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
Run Code Online (Sandbox Code Playgroud)
我可以想到一种方法:假设退出情况发生在嵌套的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 …Run Code Online (Sandbox Code Playgroud) 什么时候应该使用for循环而不是while循环?
我认为以下循环是相同的,除了它们的语法.如果是这样,为什么选择一个而不是另一个呢?
int i;
for (i = 0; i < arr.length; i++) {
// do work
}
int i = 0;
while (i < arr.length) {
// do work
i++;
}
Run Code Online (Sandbox Code Playgroud) 我有时想知道的一件事是下面显示的两个中哪个更好(如果有的话)?它是更好立即返回,如果监护条件尚未满足,或者如果保护条件,你应该只能做其他的东西是满意吗?
为了参数,请假设保护条件是一个返回布尔值的简单测试,例如检查元素是否在集合中,而不是通过抛出异常可能影响控制流的东西.还假设方法/函数足够短,不需要编辑器滚动.
// Style 1
public SomeType aMethod() {
SomeType result = null;
if (!guardCondition()) {
return result;
}
doStuffToResult(result);
doMoreStuffToResult(result);
return result;
}
// Style 2
public SomeType aMethod() {
SomeType result = null;
if (guardCondition()) {
doStuffToResult(result);
doMoreStuffToResult(result);
}
return result;
}
Run Code Online (Sandbox Code Playgroud) 一位同事刚刚向我展示了这一点,我很惊讶它汇总了:
def toUpper(s: Option[String]): String = {
s.getOrElse(return "default").toUpperCase
// ^^^^^^ // a return here in the closure??
}
Run Code Online (Sandbox Code Playgroud)
这甚至有效:
println(toUpper(Some("text"))) // TEXT
println(toUpper(None)) // default
Run Code Online (Sandbox Code Playgroud)
我认为return从内部封闭是不允许的.从什么时候开始?是否有这种非本地回报的警告?
是否有适用于JavaScript的Call-Graph和/或Control-Flow-Graph生成器?
调用图 - http://en.wikipedia.org/wiki/Call_graph
控制流图 - http://en.wikipedia.org/wiki/Control_flow_graph
编辑:我正在寻找一个静态工具,让我使用一些API /代码访问图形
javascript call-graph control-flow serverside-javascript node.js
control-flow ×10
node.js ×3
python ×2
asynchronous ×1
break ×1
c ×1
c++ ×1
call-graph ×1
coding-style ×1
conventions ×1
if-statement ×1
javascript ×1
loops ×1
optional ×1
program-flow ×1
return ×1
scala ×1
swift ×1
swift2 ×1