如果有这样的事情,我会想象语法是一样的
while(Integer item : group<Integer>; item > 5)
{
//do something
}
Run Code Online (Sandbox Code Playgroud)
只是想知道是否有这样的东西或模仿这种方式?
在R中意识到循环通常不理想,有时它们是必要的.
在写大循环时,没有
for (i in 1:large_number)
Run Code Online (Sandbox Code Playgroud)
浪费内存,因为必须创建大小为large_number的向量?
这会使while循环成为大型必要循环的最佳选择吗?
我看到了我读过的许多书中使用的不同约定,你可以在其中创建具有循环结构的无限循环,例如:
while()
foo();
for(;;)
foo();
Run Code Online (Sandbox Code Playgroud)
但实际上,我应该知道的差异是什么?哪一个更好?
在while循环中,Perl读取一行输入,将其放入变量,并运行循环体.然后,它返回找到另一行输入.但是在foreach循环中,行输入操作符正在列表上下文中使用(因为foreach需要一个列表来迭代).所以它必须在循环开始运行之前读取所有输入.当输入来自您的400 MB Web服务器日志文件时,这种差异将变得明显!通常最好使用像while循环的快捷方式这样的代码,它会尽可能地一次处理输入行.来源 - 骆驼书
我while每次都应该使用吗?foreach循环将占主导地位的可能情况是什么?
在下面的代码中,我有一个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的所有可能值都会立即列出.
这里有逻辑错误吗?
我目前正在学习java浮点数.我知道,浮动具有特定数量的显着差异.我也知道,浮点数在java中表示为-1或1*num*10 ^ x.其中num是数字,10 ^ x是小数"点".但在这里,我们没有一小部分.这里的无限循环怎么可能?
无限循环的代码:
float f = 123456789;
while (f-- > 0) {
System.out.println(f);
}
Run Code Online (Sandbox Code Playgroud) 我想在bash脚本中执行一个大约需要1分钟才能完成的命令.但是,有时这个命令会挂起,所以我想在循环中使用/ usr/bin/timeout直到它工作.
如果我使用timeout 300 mycommand myarg1它,它可以工作,但如果我在下面的循环中使用它在bash中,它不打印任何东西(甚至不是我的命令打印的典型输出)并且它挂起!:
until timeout 300 mycommand myarg
do
echo "The command timed out, trying again..."
done
Run Code Online (Sandbox Code Playgroud)
我的bash版本:
$ bash --version
GNU bash, version 4.3.48(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
Run Code Online (Sandbox Code Playgroud)
我的超时版本:
$ timeout --version
timeout (GNU coreutils) 8.25
Copyright (C) 2016 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change …Run Code Online (Sandbox Code Playgroud) while-loop ×10
loops ×5
c++ ×3
for-loop ×2
java ×2
bash ×1
curly-braces ×1
foreach ×1
halide ×1
javascript ×1
opencv ×1
perl ×1
python ×1
r ×1
settimeout ×1
timeout ×1