[英]skipping iteration in while loop java
I am just beginner at programming and I just started taking java at our school last week. 我只是编程的初学者,上周刚开始在我们学校学习Java。 What I am trying to do is skip an iteration using continue statement in while loop in java and unfortunately the output is not what I expected it to be...
我正在尝试做的是在java中的while循环中使用continue语句跳过迭代,但是不幸的是输出不是我期望的...
This is my code: 这是我的代码:
// Using while Loop
int counter = 1;
while(counter <= 5){
if(counter == 3){
continue;
}
System.out.println(counter);
counter++;
}
The output is: 1 2 and it doesn't print the 4 and 5 but I've noticed that the program is still not terminated. 输出为:1 2,它不显示4和5,但是我注意到程序仍然没有终止。
I've even tried coding it like this: 我什至尝试编码如下:
int counter = 1;
while(counter <= 5){
System.out.println(counter);
if(counter == 3){
continue;
}
counter++;
}
It just prints 3 nonstop 它只连续打印3个
int counter= 1;
while(counter <= 5){
counter++;
if(counter == 3){
continue;
}
System.out.println(counter);
}
this one prints 2 4 5 6 instead of 1 2 4 5 这个打印2 4 5 6而不是1 2 4 5
I have used for loop to do this and it work well 我已经使用了循环来做到这一点,它工作良好
this is my code: 这是我的代码:
//using for loop
for(int counter = 1; counter <= 5; counter++){
if(counter == 3){
continue;
}
System.out.println(counter);
}
this prints the right output... 这将输出正确的输出...
Now, can anyone please tell me what is my mistake in using while loop in doing this exercise? 现在,有人可以告诉我在进行此练习时使用while循环有什么错误吗? Thanks...
谢谢...
if(counter == 3){
continue;
}
System.out.println(counter);
counter++;
Here the continue
statement skips the ctr++;
这里的
continue
语句跳过了ctr++;
statement, so it is always 3
and the while
loop never terminates 语句,因此它始终为
3
, while
循环永不终止
int counter = 1;
while(counter <= 5){
System.out.println(counter);
if(counter == 3){
continue;
}
counter++;
}
Here the print statement will be reached, as it is before the continue
statment, but the counter++;
在这里,将到达print语句,就像在
continue
声明之前一样,但是要到达counter++;
will still be by passed, resulting in an infinite loop of printing 3. 仍会被跳过,从而导致打印3的无限循环。
int counter= 1;
while(counter <= 5){
counter++;
if(counter == 3){
continue;
}
System.out.println(counter);
}
Here counter++
is reached, but it will be incremented before the println()
so it prints out one plus the values you want 在这里到达了
counter++
,但是它会在println()
之前递增,因此它会打印出一个加上所需的值
By the way, in first answer given by @GBlodgett, you know why your program is not showing the result you were expecting. 顺便说一句,在@GBlodgett给出的第一个答案中,您知道为什么您的程序没有显示您期望的结果。 This is how you can achieve your result.
这就是您可以实现结果的方式。
// Using while Loop //使用while循环
int counter = 0;
while(counter < 5){
counter++;
if(counter == 3){
continue;
}
System.out.println(counter);
}
The issue is that once counter == 3, it will always hit the if statement as true and never increment counter again. 问题是一旦counter == 3,它将始终击中if语句为true,并且永远不会再递增counter。 So your while loop will print 1 2 and then execute infinitely.
因此,您的while循环将打印1 2,然后无限执行。
In order to solve the issue, code it like this: 为了解决此问题,请像下面这样编码:
// Using while Loop
int counter = 1;
while(counter <= 5){
if(counter == 3){
counter++;
continue;
}
System.out.println(counter);
counter++;
}
Just add counter++ before your continue statement. 只需在继续语句之前添加counter ++。 Hope this helps.
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.