我看到了我读过的许多书中使用的不同约定,你可以在其中创建具有循环结构的无限循环,例如:
while()
foo();
for(;;)
foo();
Run Code Online (Sandbox Code Playgroud)
但实际上,我应该知道的差异是什么?哪一个更好?
我正在学习java以及android.几乎所有我们可以执行的操作都会循环我们可以在while循环中执行的操作.
我找到了一个简单的条件,使用while循环比循环更好
如果我必须在我的程序中使用计数器的值,那么我认为while循环比循环更好
使用while循环
int counter = 0;
while (counter < 10) {
//do some task
if(some condition){
break;
}
}
useTheCounter(counter); // method which use that value of counter do some other task
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我发现while循环比for循环更好,因为如果我想在for循环中实现相同,我必须将counter的值赋给另一个变量.
但是当while循环优于for循环时,是否存在任何特定情况
我在MacOSX上运行python版本2.7.3.
考虑这段代码:
from __future__ import print_function
import time
x = 0
while x < 5:
print(x)
x += 1
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
如果我运行这个脚本,我会观察到我期望的输出:数字0通过附加到每个数字4的\n字符.此外,每暂停一秒后显示每个数字.
0
1
2
3
4
Run Code Online (Sandbox Code Playgroud)
现在考虑这个代码块:
from __future__ import print_function
import time
x = 0
while x < 5:
print(x, end='')
x += 1
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
输出是我所期望的,01234没有\n's,但时间是出乎意料的.该过程不是在一秒钟暂停后显示每个数字,而是等待四秒钟,然后显示所有五个数字.
为什么print('string')表现与print('string', end='')while循环不同?有没有办法显示没有换行符的字符,一次一秒?我尝试过sys.stdout.write(str(x)),但它的行为方式与之相同print(end='').
有人可以解释这两个while循环之间的区别:
while read test; do
echo $test
done <<< "$(seq 5)"
Run Code Online (Sandbox Code Playgroud)
-
while read test; do
echo $test
done < <(seq 5)
Run Code Online (Sandbox Code Playgroud) 我试图控制我的程序中的while循环停止和启动基于用户输入.我用一个按钮尝试了这个,它的"开始"部分工作,但然后代码进入无限循环,我不能手动终止它.以下是我的所有代码: Header Class
package test;
import javax.swing.JFrame;
public class headerClass {
public static void main (String[] args){
frameClass frame = new frameClass();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(150,75);
frame.setVisible(true);
}
}
Run Code Online (Sandbox Code Playgroud)
框架类
package test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class frameClass extends JFrame {
private JButton click;
public frameClass(){
setLayout(new FlowLayout());
click = new JButton("Stop Loop");
add(click);
thehandler handler = new thehandler();
click.addActionListener(handler);
}
private class thehandler implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==click){
looper loop = new looper();
looper.buttonSet = !looper.buttonSet; …Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我有一个while语句用于确保输入字符串少于10个字符.我已经声明了一个bool被叫cont,我用它来告诉while循环在我的条件满足后停止.
#include "stdafx.h"
#include <iostream>
#include <string>
int main()
{
using namespace std;
cout << "Enter a string less than 10 characters long: ";
string teststring;
{
bool cont(false);
//if input is 10 or more characters, ask for input again until it is less
while (!cont)
{
getline(cin, teststring);
if (teststring.length() >= 10)
{
cout << "Too long, try again: ";
}
else
{
cout << "Thank you.\n\n";
cont = true;
}
}
}
return 0; …Run Code Online (Sandbox Code Playgroud) 我正在尝试在Halide中实现Meijster距离变换算法.我已经将此代码重写为C++(使用openCV)并且它工作正常.关于这个算法的论文就在这里.现在我的卤化物代码已完成50% - 第一阶段工作正常,现在我遇到了阶段2(链接代码中的扫描3)的问题,其中(简化)看起来像这样:
//g is 2 dimensional cv::Mat (something like array) - result of previous stage
// m is g.width and n is g.height
int(*functionF)(int x, int i, int g_i) = EDT_f;
int(*functionSep)(int i, int u, int g_i, int g_u, int max_value) = EDT_Sep;
cv::Mat dt = cv::Mat(n, m, CV_32SC1);
int* s = new int[m];
int* t = new int[m];
int q = 0, w;
for (int y = 0; y<n; y++)
{
q = 0; …Run Code Online (Sandbox Code Playgroud) 变量a可以采用任意数量的值.值a是为while循环提供的额外预定义条件的数量.
这可以通过多个elif语句来完成,但有更简洁的方法吗?
if a == 0:
while condition_1:
...
elif a == 1:
while condition_1 or condition_2:
...
elif a == 2:
while condition_1 or condition_2 or condition_3:
...
Run Code Online (Sandbox Code Playgroud) 我在这里阅读了w3schools和其他类似问题的相关页面,但似乎无法理解下面的内容有什么不对:
var myfunc03 = function (i) {
document.getElementById('d01').innerHTML += 100-i+"<br>";
};
var myFunc01 = function() {
i=0;
while (i<100) {
setTimeout(myfunc03(i), 1000)
i++;
}
};
Run Code Online (Sandbox Code Playgroud)
何时myFunc01();运行.
没有任何暂停,i的所有可能值都会立即列出.
这里有逻辑错误吗?
我有一个while let循环遍历迭代器Result并使用模式匹配; 它遍历迭代器,直到它命中一个Err或者它Ok的值是一个空字符串:
while let Some(Ok(a)) = some_iterator.next() {
if a == "" {
break;
}
// ...
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常.但是,我认为这个if陈述看起来很难看,可能不是惯用的Rust.在match语句中,保护可以用于模式匹配,如下所示:
match foo {
Some(Ok(a)) if a != "" => bar(a)
// ...
}
Run Code Online (Sandbox Code Playgroud)
这对我的while let循环来说是理想的,虽然那里使用的模式匹配似乎不支持它,导致语法错误:
while let Some(Ok(a)) = some_iterator.next() if a != "" { // <-- Syntax error
// ...
}
Run Code Online (Sandbox Code Playgroud)
在这样的情况下,有没有办法使用这样的守卫while let?如果没有,如果发现空字符串,是否有更好的方法可以打破循环?
while-loop ×10
c++ ×3
java ×2
loops ×2
python ×2
bash ×1
button ×1
curly-braces ×1
for-loop ×1
halide ×1
javascript ×1
opencv ×1
rust ×1
settimeout ×1
swing ×1