标签: control-flow

如何在Python中打破多个循环?

给出以下代码(不起作用):

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__

python break control-flow

440
推荐指数
15
解决办法
35万
查看次数

如何避免"如果"链?

假设我有这个伪代码:

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声明是否可以某种方式使用?

c c++ if-statement control-flow

262
推荐指数
25
解决办法
4万
查看次数

斯威夫特:守卫vs如果让

我一直在阅读关于Swift中的Optionals,我已经看过if let用于检查Optional是否包含值的示例,以及它是否存在 - 使用unwrapped值执行某些操作.

但是,我已经看到在Swift 2.0中,关键字guard主要用于.我想知道是否if let已从Swift 2.0中删除或是否仍然可以使用它.

我应该改变我的计划包含if letguard

conventions optional control-flow swift swift2

105
推荐指数
8
解决办法
4万
查看次数

如何退出if子句

过早退出if条款有哪些方法?

有时我正在编写代码并希望breakif子句中放置一个语句,只记住那些只能用于循环.

让我们以下面的代码为例:

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)

python control-flow

87
推荐指数
8
解决办法
26万
查看次数

为什么使用for循环而不是while循环?

可能重复:
使用for循环或while循环迭代?
C中的循环 - ()或while() - 哪个最好?

什么时候应该使用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)

loops coding-style control-flow

67
推荐指数
8
解决办法
5万
查看次数

编程风格:如果不满足保护条件,您应该提早返回吗?

我有时想知道的一件事是下面显示的两个中哪个更好(如果有的话)?它是更好立即返回,如果监护条件尚未满足,或者如果保护条件,你应该只能做其他的东西满意吗?

为了参数,请假设保护条件是一个返回布尔值的简单测试,例如检查元素是否在集合中,而不是通过抛出异常可能影响控制流的东西.还假设方法/函数足够短,不需要编辑器滚动.

// 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)

language-agnostic program-flow control-flow

30
推荐指数
6
解决办法
7877
查看次数

Scala中的非本地回报是新的吗?

一位同事刚刚向我展示了这一点,我很惊讶它汇总了:

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从内部封闭是不允许的.从什么时候开始?是否有这种非本地回报的警告?

scala return control-flow

24
推荐指数
2
解决办法
4475
查看次数

是否有适用于JavaScript的静态Call-Graph和/或Control-Flow-Graph API?

是否有适用于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

22
推荐指数
3
解决办法
9232
查看次数

node.js的最佳控制流模块是什么?

我已经使用了非常好的caolan异步模块,但是跟踪错误和通过控制流传递数据的方式变化导致开发有时非常困难.

我想知道是否有更好的选择,或者目前在生产环境中使用的是什么.

谢谢阅读.

asynchronous control-flow node.js

18
推荐指数
1
解决办法
1万
查看次数

Node.js控制流程:回调还是承诺?

我知道node.js 有很多控制流库.其中一些让一个链异步函数与回调(如async,asyncblock等),其他使用promise概念(Q,延期,期货等).鉴于长时间运行的脚本会一个接一个地执行一系列可能随时失败的操作,您更喜欢哪种控制流程?为什么?优缺点都有什么?

control-flow node.js

18
推荐指数
2
解决办法
4882
查看次数