0
How exactly do the ''break'' and ''continue'' loop operations work with the for and while loops in Java?
I'm trying to practice definite and indefinite loops in Java, but the ''break'' and '' continue'' loop operations keep tripping me.
3 Réponses
+ 3
example 1 below:
int count10 = 0; // initial value
while(true){ // infinite loop
count10++;
if(count10 == 10) break; // jumps out of loop
}
Example 2 below:
for(int i = 1; i <= 100; i++){ // count from 1 - 100
if(i % 2 != 0) continue;
System.out.println("even number: "+ i);
}
The example above basically states if the number is odd then skip the remainder of the loop body and go to next iteration. However if its not odd then print the even number.
0
Thanks
0
Hope it has helped. Just play around with it and you shall get the hang of it in no time!.