有时当我编程时,我发现某些特定的控制结构对我来说非常有用,但在我的编程语言中却无法直接使用.我认为我最常见的愿望是"分裂时"(我不知道该怎么称呼它):
{
foo();
} split_while( condition ) {
bar();
}
Run Code Online (Sandbox Code Playgroud)
此代码的语义将foo()始终运行,然后检查条件.如果为true,则bar()运行,然后返回第一个块(因此foo()再次运行等).感谢reddit用户zxqdms的评论,我了解到Donald E. Knuth在他的论文"使用go to语句进行结构化编程"(参见第279页)中写到了这种结构.
您认为哪些替代控制结构是组织计算的有用方法?
我的目标是为自己和他人提供构建代码的新方法,以改进分块和推理.
注意:我不是在询问如何概括所有可能的控制结构,无论是使用jne,if/ goto,Lisp宏,continuation,monads,组合器,夸克还是其他任何东西.我问的是什么专业在描述代码时很有用.
我需要if用Python 编写这四个.注意它的作用,是在循环中的四种可能状态之间切换:1,0 -> 0,1 -> -1,0 -> 0,-1然后回到第一种状态.
if [dx, dy] == [1,0]:
dx, dy = 0, 1
if [dx, dy] == 0, 1:
dx, dy = -1, 0
if [dx, dy] == [-1, 0]
dx, dy = 0, -1
if [dx, dy] == [0, -1]:
dx, dy = 1, 0
Run Code Online (Sandbox Code Playgroud)
有谁能建议我更好/更好的方式来写这个?
我已经看过很多了:$fp = fopen($filepath, "w") or die();
但我似乎无法找到关于这种"或"语法的任何真实文档.它的功能显而易见,但我可以在任何地方使用它吗?它一定要跟着die()吗?or当你可以使用类似的东西时,是否有任何需要注意的事项
if (file_exists($filepath))
$fp = fopen($filepath,"r");
else
myErrMessage();
Run Code Online (Sandbox Code Playgroud)
我知道这似乎是一个愚蠢的问题,但我找不到任何严格的规则.谢谢.
今天我正在浏览php手册并偶然发现一个控制结构声明.
declare构造用于为代码块设置执行指令
这是declare应该做的.说实话,我不明白.在再次阅读时,它发现了一件新事物Ticks
tick是由声明块内的解析器执行的每N个低级别可勾选语句发生的事件.在声明块的指令部分中使用ticks = N指定N的值.
我也不明白.这是什么意思N low-level tickable statements如果有一个很好的示例代码,那么它将很容易理解.但手册中没有找到任何内容.我在SO Q1上找到了一些,这实际上增加了我的好奇心和困惑.任何人都可以使用这个,我们可以在哪里使用它.
我的实际困惑在于这个陈述(来自链接的帖子)you can declare a tick-function which checks each n executions of your script whether the connection is still alive or not.因此,当我在php文件上注册tick = 20的tick函数并执行它时,该文件将处于活动状态,直到20执行完成(当它被错误地视为多线程时得到这个想法).这是我的想法,我不认为它是正确的..
或者它是一个简单的替代品 while($x = 20)
[编辑1]
我也看到了declare()php手册函数参数的另一部分的实现
[编辑2]
也在过程控制中
您可以使用declare()语句指定程序中允许回调的位置.这允许您最小化处理异步事件的开销
我总是使用和看到只有"休息"的例子.这是什么意思:
<?php
while ($flavor = "chocolate") {
switch ($flavor) {
case "strawberry";
echo "Strawberry is stock!";
break 2; // Exits the switch and the while
case "vanilla";
echo "Vanilla is in stock!";
break 2; // Exits the switch and the while
case "chocolate";
echo "Chocolate is in stock!";
break 2; // Exits the switch and the while
default;
echo "Sorry $flavor is not in stock";
break 2; // Exits the switch and the while
}
}
?>
Run Code Online (Sandbox Code Playgroud)
"break"语句是否有更多可用选项?
在审阅时,我有时会遇到这种循环:
i = begin
while ( i != end ) {
// ... do stuff
if ( i == end-1 (the one-but-last element) ) {
... do other stuff
}
increment i
}
Run Code Online (Sandbox Code Playgroud)
然后我问这个问题:你会写这个吗?
i = begin
mid = ( end - begin ) / 2 // (the middle element)
while ( i != end ) {
// ... do stuff
if ( i > mid ) {
... do other stuff
}
increment i
}
Run Code Online (Sandbox Code Playgroud)
在我看来,这超过了编写循环的意图:你循环,因为每个元素都有一些共同点.使用此构造,对于某些元素,您可以执行不同的操作.因此,我总结说,您需要为这些元素单独的循环:
i = begin …Run Code Online (Sandbox Code Playgroud) language-agnostic loops for-loop control-structure while-loop
第一关,我是PHP的新手.我一直在使用for循环,while循环,foreach循环脚本.我想知道
我坚持的代码想知道要使用哪个循环.
for($i=0;$i<count($all);$i++)
{
//do some tasks here
for($j=0;$j<count($rows);$j++)
{
//do some other tasks here
}
}
Run Code Online (Sandbox Code Playgroud)
很明显我可以用while编写上面的代码.希望有人能帮助我找出哪个循环应该更好用.
我有一个关于if not声明的问题Python 2.7.
我写了一些代码和使用过的if not语句.在我编写的代码的一部分中,我引用了一个函数,其中包含一个if not语句来确定是否已输入可选关键字.
它工作正常,除非0.0是关键字的值.我理解这是因为0其中一个被认为是'不'.我的代码可能太长而无法发布,但这是一个类似的(尽管是简化的)示例:
def square(x=None):
if not x:
print "you have not entered x"
else:
y=x**2
return y
list=[1, 3, 0 ,9]
output=[]
for item in list:
y=square(item)
output.append(y)
print output
Run Code Online (Sandbox Code Playgroud)
但是,在这种情况下,我留下了:
you have not entered x
[1, 9, None, 81]
Run Code Online (Sandbox Code Playgroud)
在哪里我想得到:
[1, 9, 0, 81]
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,我可以使用列表推导,但假设我想使用该函数并获得所需的输出,我该怎么做?
我有一个想法是:
def square(x=None):
if not x and not str(x).isdigit():
print "you have not entered x"
else:
y=x**2 …Run Code Online (Sandbox Code Playgroud) 我需要根据一组相当大的8个相关依赖条件做出决定.
| A | B | C | D | E | F | G | H
-----------+---+---+---+---+---+---+---+---
Decision01 | 0 | 1 | - | 1 | 0 | 1 | - | 1
Decision02 | 1 | 0 | - | 0 | 0 | - | 1 | -
...
Decision11 | 1 | 0 | 1 | 1 | 1 | - | 1 | 1
Run Code Online (Sandbox Code Playgroud)
从A到H的每个条件对于决定可以是真(1),假(0)或不相关( - ).
所以用给定的输入
A B C D E F …Run Code Online (Sandbox Code Playgroud) 我已经工作PHP了一段时间,但今天当我看到它时,它对我来说是新的:
if(preg_match('/foo.*bar/','foo is a bar')):
echo 'success ';
echo 'foo comes before bar';
endif;
Run Code Online (Sandbox Code Playgroud)
令我惊讶的是它也运行没有错误.任何人都可以开导我吗?
谢谢大家 :)
php ×5
loops ×3
if-statement ×2
python ×2
syntax ×2
alternate ×1
break ×1
c# ×1
for-loop ×1
logic ×1
matrix ×1
nested-loops ×1
or-operator ×1
performance ×1
program-flow ×1
python-2.7 ×1
while-loop ×1