我有两段代码:
做while循环:
do
{
errorflag=0;
...
if(cond1)
{
errorFlag=12;
break; // Error Conditions
}
.
. // Processing
.
if(cond2)
{
errorflag=56;
break;
}
.
.
} while (0);
Run Code Online (Sandbox Code Playgroud)
转到标签:
errorflag=0;
if(cond1)
{
errorflag=12;
goto xy;
.
.
.
.
if(Cond2)
{
errorflag=56;
goto xy;
}
.
.
.
xy:
Run Code Online (Sandbox Code Playgroud)
哪一个更好?请详细说明原因?或者有更好的方法吗?我们正在优化代码.我们最关注这些大循环.是装配级别,没有那么多的优化范围.请提供您的意见.
我不喜欢使用else - 如果从那以后,它再次成为检查一个条件的开销.所以当出现问题时直接退出.
我觉得在这次编辑之后我的问题是有道理的
提前致谢
好.所以我是Java的初学者.但是为什么这个循环永远不会结束,即使我输入0或1.
do {
//Ask user to enter 1 or 0
System.out.print("Enter 1 or 0: ");
upOrDown = keyboard.nextInt();
} while(upOrDown != 0 || upOrDown != 1);
//Here the loop should exit if user entered 1 or 0, but it does not.
Run Code Online (Sandbox Code Playgroud)
所以,我在这里错过了什么,可能非常明显,我很抱歉:(
import java.util.Scanner;
import java.util.*;
public class MultipicationTable{
public static void main(String[] args)
{
// Initialising selection variable to 0
int selection = 0;
int MultiValue = 0;
int UserValue = 0;
// Initializing the count for loop
int i;
// Initializing Random1 and Random2 to get random values
int Random1;
int Random2;
// Creating new Scanner
Scanner input = new Scanner(System.in);
do{
// Menu to select type of multipication
System.out.println("Please select your option");
System.out.println("1: Random Multipication table");
System.out.println("2: Give your …Run Code Online (Sandbox Code Playgroud) 我正在尝试设置一个do-while循环,它会重新提示用户输入,直到他们输入一个大于零的整数.我不能使用try-catch; 这是不允许的.这是我得到的:
Scanner scnr = new Scanner(System.in);
int num;
do{
System.out.println("Enter a number (int): ");
num = scnr.nextInt();
} while(num<0 || !scnr.hasNextLine());
Run Code Online (Sandbox Code Playgroud)
如果输入负数,它将循环,但如果是字母,则程序终止.
java exception-handling exception do-while java.util.scanner
背景
大家好。我有一个名为 it 的抽象类,BaseRecordFetcher<TEntity>它有一个方法,该方法从子类中获取 read/sort/translate/move-next 方法,并yield 将结果作为模型实体返回。
当我读取多个数据行时,它正确地yield return为每个实体执行 a 操作,然后在没有任何问题的情况下到达跟踪消息do...while。
问题
然而我注意到,当我IEnumerable.FirstOrDefault()在集合上使用时,系统假设不需要更多的收益返回,并且它不会完成此方法的执行!
除了返回多少记录的跟踪输出之外,我对这种行为没有太大问题,但它让我思考:“如果我确实需要一些......让我们称之为代码怎么办?finally”
问题
有没有办法始终确保系统在之后运行一些后处理代码yield return?
代码
/// <summary>
/// Retrieve translated entities from the database. The methods used to do
/// this are specified from the child class as parameters (i.e. Action or
/// Func delegates).
/// </summary>
/// <param name="loadSubsetFunc">
/// Specify how to load a set of database records. Return boolean
/// …Run Code Online (Sandbox Code Playgroud) 以下2个C#片段之间的执行存在差异吗?
do
{
Console.WriteLine(x.ToString());
++x;
}
while (x < 7);
Run Code Online (Sandbox Code Playgroud)
和
label:
{
Console.WriteLine(x.ToString());
++x;
}
if (x < 7) goto label;
Run Code Online (Sandbox Code Playgroud)
我想弄清楚为什么这么糟糕.谢谢.
编辑:如果我添加括号,片段非常相似.
EDIT2:在Visual Studio中,我点击了Go to Disassembly,我得到以下代码:
do
{
00000037 nop
Console.WriteLine(x.ToString());
00000038 lea ecx,[ebp-40h]
0000003b call 63129C98
00000040 mov dword ptr [ebp-48h],eax
00000043 mov ecx,dword ptr [ebp-48h]
00000046 call 63148168
0000004b nop
++x;
0000004c inc dword ptr [ebp-40h]
}
0000004f nop
while (x < 7);
00000050 cmp dword ptr [ebp-40h],7
00000054 setl al
00000057 movzx eax,al
0000005a mov …Run Code Online (Sandbox Code Playgroud) 我不确定我犯的是什么错误,但我只是改变ne了!=它并且它起作用了.
这是一个简单的程序,让用户猜出一个数字,直到他们达到目标数字.
#!/usr/bin/perl
my $guess = 1;
do {
$guess = <STDIN>;
} while ( $guess != 12 ) ; # it doesn't work if i replace != with ne
say "you guessed ", $guess;
Run Code Online (Sandbox Code Playgroud) 在编写程序的过程中,我经常遇到无限循环。
如何编写一个程序,以另一个程序作为输入并确定是否存在无限循环?
我有这个基本的 Arduino 代码,我想要 2 个选项来退出这个 Do-While 循环。我简化了我的原始代码以突出真正的问题:Do-While无法识别 OR || 退出循环的条件
在这段代码中,我从两个等于 0 的整数变量开始,一旦它们进入 Do-While,它们就会被设置为等于 2,这样它们就可以在第一个中立即退出 Do-While迭代。
这是我的代码:
int fin = 0;
int ending = 0;
int counter = 0;
void setup() {
Serial.begin(9600);
}; //void setup
void loop () {
do {
Serial.println("We are IN Do-While #1");
ending = 2;
//fin = 2;
} while ((ending < 1) || (fin < 1)); //I have just one condition for exit the Do-While ending = 1
Serial.println("We are OUT Do-While …Run Code Online (Sandbox Code Playgroud) 我有以下内容,output.txt它只包含 2 列来演示:
Test1 Test1-IS-OK
Test2 Test2-IS-NOT
Test3 Test3-IS-OK
Test4 Test4-IS-OK
Test5 Test5-IS-NOT
Run Code Online (Sandbox Code Playgroud)
然后我的 bash 脚本有以下代码:
#!/bin/bash
output="output.txt"
a=$(awk '{ print $1 }' $output)
b=$(awk '{ print $2 }' $output)
while IFS=" " read -r $a $b
do
echo "LOG: $a and $b"
done < "$output"
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
./test.sh: line 13: read: `Test1-IS-OK': not a valid identifier
Run Code Online (Sandbox Code Playgroud)
我需要有这样的输出
LOG: Test1 and Test1-IS-OK
LOG: Test2 and Test2-IS-NOT
LOG: Test3 and Test3-IS-OK
LOG: Test4 and Test4-IS-OK
LOG: Test5 and Test5-IS-NOT
Run Code Online (Sandbox Code Playgroud)
但是代码不起作用。从文件中循环这 …
do-while ×10
java ×3
loops ×3
c# ×2
goto ×2
while-loop ×2
arduino ×1
awk ×1
bash ×1
c ×1
c++ ×1
coding-style ×1
exception ×1
for-loop ×1
ienumerable ×1
perl ×1
yield-return ×1