标签: do-while

是否有可能在c ++中有一个while循环,使得检查在循环的中间而不是开始或结束?

我想有一个while循环做类似下面的事情,但这可能在c ++?如果是这样,语法如何?


do {
    //some code
    while( expression to be evaluated );
    // some more code
}

我希望一旦while语句决定表达式不再为真,就会退出循环(即如果表达式为false,则表示不执行某些代码)

c++ loops while-loop do-while

4
推荐指数
2
解决办法
1693
查看次数

是"} while(0);" 总是等于"break;} while(1);"?

我比较了gcc汇编程序的输出

do{ 

//some code 

}while(0);
Run Code Online (Sandbox Code Playgroud)

do{

//some code

 break; 
}while(1);
Run Code Online (Sandbox Code Playgroud)

输出是相等的,有或没有优化,但..

一直都是这样吗?

没有实验可以证明理论,它们只能证明它们是错误的

c c++ break while-loop do-while

4
推荐指数
2
解决办法
2037
查看次数

Bash Shell Do While Loop Infinite循环?

基本上这是我的代码:

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命令还是只是重新执行其原始执行?或者我的问题在哪里?

linux bash shell while-loop do-while

4
推荐指数
1
解决办法
4519
查看次数

循环10次添加输入,在C#中使用for和do while循环?

我是一名基础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)

多谢你们!

c# for-loop do-while

4
推荐指数
1
解决办法
9763
查看次数

为什么`do {} while(0);`这么快?

我尝试了以下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)

objective-c do-while

4
推荐指数
1
解决办法
602
查看次数

使用多个CPU核心优化目录中的映像

我在目录中有几个PNG图像,我optipng用来优化和减少图像大小.问题是优化所有文件需要很长时间.

我有一个四核处理器,我注意到optipng在优化目录时只使用了一个核心.

这是我正在使用的代码:

ls -1 | while read line
do 
    optipng -o7 "$line"
done
Run Code Online (Sandbox Code Playgroud)

optipng在读取目录时是否可以并行执行四个不同的文件?

linux bash terminal image do-while

4
推荐指数
3
解决办法
1472
查看次数

Nodejs - 错误回调时重新调用函数 - 是否存在非阻塞方式?

我有一个函数向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)

asynchronous node.js do-while

4
推荐指数
1
解决办法
5431
查看次数

从List中删除多余的项目时,枚举器陷入无限循环

我有一个脚本,它接受一个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)

c# linq do-while

4
推荐指数
2
解决办法
292
查看次数

收到表情符号反应后的 Discord.js 消息

一旦先前的消息收到反应,是否可以让 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)

javascript do-while discord.js

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

循环条件中逻辑运算符的使用

在下面给出的代码中,为什么||逻辑不起作用,而是循环在&&使用时专门终止?

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)

c++ loops while-loop logical-operators do-while

4
推荐指数
1
解决办法
1022
查看次数