我想有一个while循环做类似下面的事情,但这可能在c ++?如果是这样,语法如何?
do {
//some code
while( expression to be evaluated );
// some more code
}
我希望一旦while语句决定表达式不再为真,就会退出循环(即如果表达式为false,则表示不执行某些代码)
我比较了gcc汇编程序的输出
do{
//some code
}while(0);
Run Code Online (Sandbox Code Playgroud)
同
do{
//some code
break;
}while(1);
Run Code Online (Sandbox Code Playgroud)
输出是相等的,有或没有优化,但..
一直都是这样吗?
没有实验可以证明理论,它们只能证明它们是错误的
基本上这是我的代码:
bay=$(prog -some flags)
while [ $bay = "Another instance of this program is running, please exit it first" ]
do
echo "Awaiting Access to program"
do
.....
Run Code Online (Sandbox Code Playgroud)
我有一个程序,它只允许一个实例一次运行,因为它与我的硬件交互的方式,当另一个实例运行时它踢出以下消息"该程序的另一个实例正在运行,请先退出它" .
我需要能够运行多个脚本,这将使用相同的程序,所以我决定使用上面的代码.我的问题是,当我运行我的两个脚本时,一个人将获得对程序的访问并按照需要运行,但另一个将注意到错误,然后卡在一个无限循环中,回显"等待访问程序".
错过了什么?Statement是执行CLI命令还是只是重新执行其原始执行?或者我的问题在哪里?
我是一名基础C#课程的新手,我在完成工作任务时遇到了麻烦.我建了一个基本的计算器,它工作正常.现在我必须添加一个名为"Sum"的新按钮,它将从我的一个盒子(number1Txtbox)中获取输入,并通过循环将其添加到自身10次.
我倒了我的c#书的页面,无法想出这个.我想出了如何使用计数器等初始化循环,我只是无法让它在我的生活中工作.
有人告诉我使用for循环,然后切换到do while循环.这对我来说没有多大意义,我以为我可以用for循环来做到这一点.所以我的问题是:
1)我甚至需要切换到do while循环才能执行此操作吗?
2)我做错了什么?
这是我到目前为止所做的,当我在文本框中输入一个数字后尝试点击总和按钮时,它只会使我的程序冻结:
private void sumBtn_Click(object sender, EventArgs e)
{
int counter;
int loopAnswer;
int number1;
number1 = int.Parse(number1Txtbox.Text);
for (counter = 1; counter <= 10; counter++)
{
loopAnswer = number1 + number1;
do
{
loopAnswer = loopAnswer + number1;
} while (counter <= 10);
equalsBox.Text = loopAnswer.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
多谢你们!
我尝试了以下3个for
循环:
#define loop 1000000000
NSDate *start;
NSDate *end;
// 1: empty for loop
start = [NSDate date];
for (NSInteger i = 0; i < loop; i++) {
}
end = [NSDate date];
NSLog(@"time interval: %f", [end timeIntervalSince1970] - [start timeIntervalSince1970]);
// 2: do-while for loop
start = [NSDate date];
for (NSInteger i = 0; i < loop; i++) {
do {
} while (0);
}
end = [NSDate date];
NSLog(@"time interval: %f", [end timeIntervalSince1970] - [start timeIntervalSince1970]);
// …
Run Code Online (Sandbox Code Playgroud) 我在目录中有几个PNG图像,我optipng
用来优化和减少图像大小.问题是优化所有文件需要很长时间.
我有一个四核处理器,我注意到optipng
在优化目录时只使用了一个核心.
这是我正在使用的代码:
ls -1 | while read line
do
optipng -o7 "$line"
done
Run Code Online (Sandbox Code Playgroud)
optipng
在读取目录时是否可以并行执行四个不同的文件?
我有一个函数向API发出请求.有时API会出现一些小问题,并且不时有一两秒钟可用,导致出现错误,我想再次调用该函数.由于此回调后还有另外70~80行代码,我不想用一个分割流if(error) <do the same stuff> else <as here>
在尝试了一段时间后,我最终使用了do-while(错误)循环,它可以阻止.有这样的异步方式吗?
我的代码(简化为泛化):
//This is the request part
function bar(j, callback){
j++;
//simulated error
if(j<=10){
return( callback('dont', j) );
}
//simulated success
else{
return( callback(null, j) );
}
}
//This is the main function - part of a bigger piece in my code
function foo(){
var i = 0;
var err = 'yes'; //else we'd get an 'err is not defined'
do{
bar(i, function(error, j){
i = j
err = error;
if(error){ …
Run Code Online (Sandbox Code Playgroud) 我有一个脚本,它接受一个int[]
数组,将其转换为一个列表,并删除已经出现至少2次的所有进一步的整数.我遇到的问题是,当它进入我检查每个整数出现次数的循环时,我陷入了循环.
编辑:"我遗漏的是,列表必须保持其原始顺序,以便从上到下删除多余的数字.抱歉,如果那些已经回答的人感到困惑!
我认为改变的数量occursintegerOccurrence
将作为while循环的计数变化.
关于我在这里缺少什么的想法?除了任何可辨别的技能.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Remoting.Messaging;
public class Kata
{
public static void Main()
{
int[] arr = new int[] {1, 2, 1, 4, 5, 1, 2, 2, 2};
int occurrenceLimit = 2;
var intList = arr.ToList();
for (int i = 0; i < intList.Count; i++)
{
var occursintegerOccurrence = intList.Count(n => n == occurrenceLimit);
do
{
occursintegerOccurrence = intList.Count(n => n == occurrenceLimit);
foreach (var x in intList)
{ …
Run Code Online (Sandbox Code Playgroud) 一旦先前的消息收到反应,是否可以让 Discord 机器人发送消息?我在想这样的事情:
if(cmd === `${prefix}list`) {
var i = 0;
let embed = new Discord.RichEmbed()
.addField("List", "Content");
let anotherembed = new Discord.RichEmbed()
.addField("Message", "List has been completed!");
return message.channel.send(embed);
do {
message.channel.send(anotherembed + 1);
}
while (i !== 0) && (reaction.emoji.name === "?");
}
Run Code Online (Sandbox Code Playgroud) 在下面给出的代码中,为什么||
逻辑不起作用,而是循环在&&
使用时专门终止?
int main() {
char select {};
do {
cout<<"Continue the loop or else quit ? (Y/Q): ";
cin>>select;
} while (select != 'q' && select != 'Q'); // <--- why || (or) doesn't work here ??
return 0;
}
Run Code Online (Sandbox Code Playgroud) do-while ×10
while-loop ×4
c++ ×3
bash ×2
c# ×2
linux ×2
loops ×2
asynchronous ×1
break ×1
c ×1
discord.js ×1
for-loop ×1
image ×1
javascript ×1
linq ×1
node.js ×1
objective-c ×1
shell ×1
terminal ×1