java continue use of i++ | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

java continue use of i++

Why does the loop stop when i don't have i ++ before the continue, and prints to the equivalent of if and then the cursor blinks? Does not print further while (i < 10) { if (i == 1) { i++; //if remove this line .... continue; } System.out.print("I am Continue" + i + "\n"); i++; } // Output /* I am Continue0 I am Continue2 I am Continue3 I am Continue4 I am Continue4 I am Continue5 I am Continue6 I am Continue7 I am Continue8 I am Continue9 I am Continue10 while (i < 10) { if (i == 1) { // i++; //if remove this line .... continue; } System.out.print("I am Continue" + i + "\n"); i++; } //output //I am Continue 0 |

9th Oct 2019, 6:19 PM
ASP
ASP - avatar
6 Answers
+ 6
Kilowac Line: i++; ➝ in your example is crucial. The loop block 'must' be modified in some way that ultimately makes the loop condition 'false'. If it doesn't, the loop becomes an infinite loop, which, in most cases, you don't want. If your program never stops running, you probably have an infinite loop somewhere in the program.
10th Oct 2019, 5:31 AM
Danijel Ivanović
Danijel Ivanović - avatar
+ 5
If you need help, you can post the code you're struggling with!  • https://www.sololearn.com/post/75089/?ref=app
9th Oct 2019, 7:24 PM
Danijel Ivanović
Danijel Ivanović - avatar
+ 1
Post the code you're working on
9th Oct 2019, 7:03 PM
Odyel
Odyel - avatar
+ 1
The key word "continue" skips the rest of the code in the loop and just goes back to the top of the loop to check the condition. When i = 1 and the code reaches continue it skips the incrementation of i, making i forever 1. The if statement is then always true and reaching the continue, creating an infinite loop, thats why you need an increment before the continue. Edit: Here's the code with comments https://code.sololearn.com/c65yQM4R6yAA/?ref=app
10th Oct 2019, 5:14 AM
Odyel
Odyel - avatar
+ 1
Yes
11th Oct 2019, 10:19 AM
Sonic
Sonic - avatar
0
I don't understand the question without more context. But maybe this helps you. A standard for loop works like this 1. create a variable 2. define a condition 3. do something 4. increase or decrease the variable 5. check condition if true repeat 3 - 5 Example for Java: for(int i = 0; i < 10; i++) { System.out.println("Counter: "+i); } Step 1: int i = 0; Step 2: i < 10; Step 4: i++ Step 3: System.out.println("Counter: "+i); Step 5 works automatically Take a look at the comments they often very helpful. https://www.sololearn.com/learn/Java/2147/?ref=app
9th Oct 2019, 11:27 PM
Stefanoo
Stefanoo - avatar