-
Notifications
You must be signed in to change notification settings - Fork 0
Java Loops Control Statement Continue
The continue
statement makes a loop skip all the following lines after the continue and jump ahead to the beginning of the next iteration. In a for
loop, control jumps to the update statement, and in a while
or do while
loop, control jumps to the boolean expression/condition.
for (int j = 0; j < 10; j++)
{
if (j == 5)
{
continue;
}
System.out.print (j + " ");
}
The value of j
will be printed for each iteration, except when it is equal to 5
. The print statement will get skipped because of the continue
and the output will be:
0 1 2 3 4 6 7 8 9
Say you want to count the number of i
s in a the word mississippi
. Here you could use a loop with the continue
statement, as follows:
String searchWord = "mississippi";
// max stores the length of the string
int max = searchWord.length();
int numPs = 0;
for (int i = 0; i < max; i++)
{
// We only want to count i's - skip other letters
if (searchWord.charAt(i) != 'i')
continue;
// Increase count_i for each i encountered
numPs++;
}
System.out.println("numPs = " + numPs);
🚀 Run Code
Learn to code and help nonprofits. Join our open source community in 15 seconds at http://freecodecamp.com
Follow our Medium blog
Follow Quincy on Quora
Follow us on Twitter
Like us on Facebook
And be sure to click the "Star" button in the upper right of this page.
New to Free Code Camp?
JS Concepts
JS Language Reference
- arguments
- Array.prototype.filter
- Array.prototype.indexOf
- Array.prototype.map
- Array.prototype.pop
- Array.prototype.push
- Array.prototype.shift
- Array.prototype.slice
- Array.prototype.some
- Array.prototype.toString
- Boolean
- for loop
- for..in loop
- for..of loop
- String.prototype.split
- String.prototype.toLowerCase
- String.prototype.toUpperCase
- undefined
Other Links