标签: while-loop

是否有可能在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循环读取文件在C++中工作?

为什么可以使用while循环读取文件,例如

while (file >> variable)
Run Code Online (Sandbox Code Playgroud)

要么

while (getline(xx, yy))
Run Code Online (Sandbox Code Playgroud)

执行>>和getline函数返回布尔值?

c++ file while-loop

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

从无限循环保存到文本文件

我目前正在将无限循环中的数据写入覆盆子pi上的SD卡.

file = open("file.txt", "w")
while True:
    file.write( DATA )
Run Code Online (Sandbox Code Playgroud)

似乎有时如果程序没有通过命令或键盘中断关闭,file.txt并不总是保存.是否有定期的方法来保存并确保数据被保存?我在考虑使用

open("file.txt", "a")
Run Code Online (Sandbox Code Playgroud)

附加到文件并定期关闭txt文件并再次打开它.在运行无限循环时,有更好的方法可以安全地存储数据吗?

python while-loop raspberry-pi

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

这个while循环如何知道何时失败?

我正在阅读有关如何编程网络套接字并运行这段代码的内容:

try {     
   while (true) {   // This is the line in question
     int i = in.read(  );
     if (i == -1) break;
     System.out.write(i);
   }
 }
 catch (SocketException e) {
   // output thread closed the socket
 }
 catch (IOException e) {
   System.err.println(e);
 }
Run Code Online (Sandbox Code Playgroud)

第二行如何知道何时失败?换句话说,while(true)循环如何工作?我想我不明白'虽然这是真的吗?'

java while-loop

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

在while循环中使用scanf函数

我正在尝试为编程分配格式化以空格分隔的用户输入.

本质上,输入由任意数量的表达式组成

L integer integer integer integer和C integer integer integer.

例如:L 1 1 5 7 C 4 5 3.

到目前为止,我已经设法根据初始字符提取整数,并可以使用scanf函数迭代字符串:

char a;
while(scanf("%c", &a) == 1){
    if(a == 'C'){
        int inputX, inputY, inputR;
        scanf("%d %d %d", &inputX, &inputY, &inputR);
        printf("%d %d %d\n", inputX, inputY, inputR);
    }
    else if(a == 'L'){
        int x1, y1, x2, y2;
        scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
        printf("%d %d %d %d\n", x1, y1, x2, y2);
    }
}
Run Code Online (Sandbox Code Playgroud)

不幸的是,虽然这会输出所需的整数,但循环(和用户输入提示)不会终止.

有人可以告诉我为什么会这样吗?

c scanf while-loop c89

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

如何使用`while read -r line`同时检查BASH中的另一个文件是否为空?

我有一个while循环,简化如下:

while read -r line
do
    (delete some lines from file2.txt)
done < file.txt
Run Code Online (Sandbox Code Playgroud)

如果file2.txt为空,则此while循环不再需要运行.

换句话说,我需要这个:

while read -r line AND file2.txt IS NOT EMPTY
do
    (delete some lines from file2.txt
done < file.txt
Run Code Online (Sandbox Code Playgroud)

我试着结合while read -r line使用-s file2.txt,但结果不工作:

while [ read -r line ] || [ -s file2.txt ]
do
    (delete some lines from file2.txt)
done < file.txt
Run Code Online (Sandbox Code Playgroud)

如何使用while循环读取文件中的行,同时还检查另一个文件是否为空?

bash while-loop

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

C while循环只在if语句为true时运行?

我正在看一些C代码,我找到了这一行.

if (temp==NULL)
    while (1) ;
Run Code Online (Sandbox Code Playgroud)

根据我的理解,当你进入一个无限循环时,除非你打破,否则没有出局,这是如何工作的?如果if语句不为NULL,它是否会中断,如果是这样,是什么让它一遍又一遍地检查if语句?

有关更多信息,请查找realboy源代码,该文件为gboy_lcd.c

第304行
https://github.com/guilleiguaran/realboy/blob/ed30dee751c3f78964e71930a8f87d2074362b9b/gboy_lcd.c

对于linux来说,它是一个非常稳定和优秀的gameboy模拟器

c loops if-statement while-loop

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

c#中的两个语句

我正在编写一个程序,它从键盘输入并以下面的螺旋方式打印方形矩阵

1 2 3 
8 9 4 
7 6 5 
Run Code Online (Sandbox Code Playgroud)

我设法编写程序,但我遇到了一个奇怪的错误.在第26行,它给了我一个超出范围的索引异常

while (matrix[row, col] == 0 && col < matrix.GetLength(0) )
Run Code Online (Sandbox Code Playgroud)

但是,如果我在循环中切换两个语句的顺序,异常就消失了?这是否意味着while循环中两个语句的顺序很重要?如果是,为什么?如果两个语句都是真的执行循环,并且如果其中一个是假的,无论是哪一个,都应该停止执行它.

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SpiralMatrixN
{
    class Program
    {
        static void Main(string[] args)
        {
            //prompt the user to enter n
            Console.WriteLine("Enter the value of n");
            int n = int.Parse(Console.ReadLine());
            int[,] matrix = new int[n,n];
            Console.Clear();
            System.Console.SetWindowSize(100, 30);
            int value = 1;
            int col = 0;
            int row = 0;
            if …
Run Code Online (Sandbox Code Playgroud)

c# while-loop

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

while循环中的post增量操作(java)

最近我遇到了这个问题

int i = 10;
while (i++ <= 10) {
    i++;
}
System.out.print(i);
Run Code Online (Sandbox Code Playgroud)

答案是13,有些人可以解释一下它是13吗?

java loops while-loop

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

为什么要改变&&到|| 导致变量无法看到

我正在做一些阅读文件的练习,我的任务是为不平衡文件编写File Interleave代码.所以如果我有这样设置的东西它工作正常:

while ((inputLine1 = LineSequential.read(inFileStreamName1)) != null **&&** (inputLine2 = LineSequential.read(inFileStreamName2)) != null) 
Run Code Online (Sandbox Code Playgroud)

注意&& ...但是如果我将它改为|| netbeans声明inputLine2尚未初始化(尽管它以这种方式看到inputLine1非常精细):

while ((inputLine1 = LineSequential.read(inFileStreamName1)) != null **||** (inputLine2 = LineSequential.read(inFileStreamName2)) != null)
Run Code Online (Sandbox Code Playgroud)

现在,如果inputLine1(这是来自文件A的数据)首先变为空,我只得到inputLine2中的每一行(来自文件B).在每次循环迭代中,它跳过一条线.我很确定它与我初始化inputLine2的位置有关..:

while ((inputLine1 = LineSequential.read(inFileStreamName1)) != null || LineSequential.read(inFileStreamName2) != null) {           
    inputLine2 = LineSequential.read(inFileStreamName2);
    System.out.println(inputLine2);
    if(inputLine1 != null && inputLine2 == null){
    LineSequential.write(outFileStreamName, inputLine1);
    }else if(inputLine1 == null && inputLine2 != null){
        LineSequential.write(outFileStreamName, inputLine2);
    }
    else if(inputLine1 != null && inputLine2 != null){
       LineSequential.write(outFileStreamName, inputLine1);
       LineSequential.write(outFileStreamName, inputLine2); 
    }
}
Run Code Online (Sandbox Code Playgroud)

java loops file while-loop

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

标签 统计

while-loop ×10

loops ×4

java ×3

c ×2

c++ ×2

file ×2

bash ×1

c# ×1

c89 ×1

do-while ×1

if-statement ×1

python ×1

raspberry-pi ×1

scanf ×1