skipping iteration in while loop java

continue in while loop java
break statement in java
continue in nested for loop java
how to skip a number in a for loop java
how to continue for loop after exception in java
java skip to next iteration of for loop
continue within for loop java
go to next iteration of while loop java

I am just beginner at programming and I just started taking java at our school last week. 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...

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.

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

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

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? Thanks...

if(counter == 3){
    continue;
}
System.out.println(counter);
counter++;

Here the continue statement skips the ctr++; statement, so it is always 3 and the while loop never terminates

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++; will still be by passed, resulting in an infinite loop of printing 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

How to skip a iteration/loop in while-loop, while(rs.next()) { if(f.exists() && !f.isDirectory()) continue; //then skip the iteration else { //proceed } }. So the computer doesn’t enter the loop until the user gives some input. Then when the computer is inside the loop, the computer asks for more input to feed the loop’s next iteration. The trick seems strange, but it works. Developers use this technique, priming a loop, all the time, so it can’t be that bad.

The issue is that once counter == 3, it will always hit the if statement as true and never increment counter again. So your while loop will print 1 2 and then execute infinitely.

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. Hope this helps.

skipping iteration in while loop java, if(counter == 3){ continue; } System.out.println(counter); counter++;. Here the continue statement skips the ctr++; statement, so it is always 3 and  Iterator HoldingsTableRows = HoldingsTableRows.iterator(); while (HoldingsTableRows.hasNext()) { } I could get the contents of a variable and if it matches I can break out of the loop but I'm trying to avoid hard coding anything specific to the header names because if the names of the headers change it would break my app.

By the way, in first answer given by @GBlodgett, you know why your program is not showing the result you were expecting. This is how you can achieve your result.

// Using while Loop

int counter = 0;

while(counter < 5){
    counter++;
    if(counter == 3){
        continue;
    }


    System.out.println(counter);

    }

Java continue Statement (With Examples), While working with loops, it is sometimes desirable to skip some statements The continue statement in Java skips the current iteration of a loop ( for , while  If you want to skip a particular iteration, use continue. 2. If you want to break out of the immediate loop use break 3 If there are 2 loop, outer and inner. and you want to break out of both the loop from the inner loop, use break with label.

Continue statement in java, Continue statement in java - The continue keyword can be used in any of the It causes the loop to immediately jump to the next iteration of the loop. In a while loop or do/while loop, control immediately jumps to the Boolean expression. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

The Java Loop Continue Control Statement, Java continue Control Statement The continue statement makes a loop skip all the lines after the continue and jump ahead to the beginning of the next iteration. control jumps to the update statement, and in a while or do while loop, control  Continue Statement in Java programming : Skip Part of Loop Continue Statement in Java is used to skip the part of loop. Unlike break statement it does not terminate the loop , instead it skips the remaining part of the loop and control again goes to check the condition again.

Pass control to next iteration of for or while loop, It skips any remaining statements in the body of the loop for the current iteration. The program continues  continue passes control to the next iteration of a for or while loop. It skips any remaining statements in the body of the loop for the current iteration. It skips any remaining statements in the body of the loop for the current iteration.

Comments
  • If you continue there, it doesn't get to counter++
  • so what can I do to fix this?
  • @robert Why not use the for loop that automatically increments i?
  • I've already done this correctly in for loop. Actually the real reason I am doing this exercise is am trying to create a program that will determine if a number is a prime or composite. I've done it correctly in for loop, but my teacher said it is right but is is not the simplest method.. Here is my code: for(int ctr=1; ctr<=n; ctr++){ if(n%ctr==0){ r++; } } He gave me a hint that the simplest is using continue in while loop so I've used if(num%ctr!=0){ continue; } It was obviously wrong..
  • Just add counter++ before the continue statement and the code for your while loop will work the same as the for loop.
  • @robert This to find how many divisors there are in a given number?
  • what i am trying to do is skip the number 3 and print 1 2 4 5. If I use break it will just print 1 2 3
  • Check it again please.
  • Please, consider editing your answer because your text explanation does not match your code.
  • Anytime pal :).