What Does the Continue Statement Do in Java
The Java continue statement stops one iteration in a loop and continues to the next iteration. This statement lets you skip particular iterations without stopping a loop entirely. Continue statements work in for and while loops.
Java for and while loops automate and repeat tasks. There may be an occasion where you want to skip part of a loop and allow the program to continue executing the loop.
Find Your Bootcamp Match
- Career Karma matches you with top tech bootcamps
- Access exclusive scholarships and prep courses
By continuing you agree to our Terms of Service and Privacy Policy, and you consent to receive offers and opportunities from Career Karma by telephone, text message, and email.
For instance, say you are building a program that calculates whether a student has passed or failed a test. You may want to skip over calculating a specific student's grade.
That's where the continue statement comes in. The Java continue statement is used to skip the current iteration of a loop in Java. This tutorial will discuss using the Java continue statement, with reference to examples to aid your understanding.
Java continue Statement
The Java continue statement skips the current iteration in a loop, such as a for or a while loop. Once a continue statement is executed, the next iteration of the loop begins.
Here's the syntax for the Java continue statement:
continue is a keyword. This means the continue statement stands alone in a program. The continue keyword is often part of a Java if statement to determine whether a certain condition is met. You can use a continue statement in a Java for loop or a Java while loop.
Let's walk through an example to discuss how to use the continue statement in Java.
Java continue Statement Example
Suppose we are creating a program that calculates whether each student in a fifth-grade math class passed their recent test. This program should loop through the lists of student names and grades and print out the student's name alongside whether they have passed.
Students whose grade is higher than 17 have passed. Any student whose grade is below 17 has failed.
However, one student, Lucy, was not present for the test. So, the program should skip calculating whether she passed.
We could use the following code to calculate whether each student in the class, excluding Lucy, has passed their recent test:
class CalculateTestScores { public static void main(String[] args) { String[] students = {"Mark", "Bill", "Lucy", "Chloe"}; int grades[] = {16, 25, 0, 19}; for (int i = 0; i < students.length; ++i) { if (students[i].equals("Lucy")) { continue; } if (grades[i] > 17) { System.out.println(students[i] + " has passed their test with the grade " + grades[i] + "."); } else { System.out.println(students[i] + " has failed their test with the grade " + grades[i] + "."); } } } }
Our code returns:
Mark has failed their test with the grade 16. Bill has passed their test with the grade 25. Chloe has passed their test with the grade 19.
continue Java Example Breakdown
First, we declare a class called CalculateTestScores, which stores the code for our program. Then we declare two arrays: students stores a list of student names and grades stores a list of those students' grades.
On the next line, we initialize a for loop, which iterates through every item in the students list. Our program executes the following statements inside the loop:
- Checks whether the student's name is equal to
Lucy
(students[i] ==Lucy
)- If the student's name is equal to
Lucy
, thecontinue
statement is executed and the program skips to the next iteration. - If the student's name is not equal to
Lucy
, the program continues running through the iteration.
- If the student's name is equal to
- Checks if the student's grade is higher than 17:
- If so, a message stating
[student name] has
passed
their test with the grade [grade].
is printed to the console, wherestudent name
is the name of the student andgrade
is the student's numerical grade. - If not, a message stating
[student name] has
failed
their test with the grade [grade]
. is printed to the console, wherestudent name
is the name of the student andgrade
is the student's numerical grade.
- If so, a message stating
When our program reached Lucy's name, it skipped over calculating her grade.
Another Continue in Java Example
Let's suppose we are creating a game for a third grade math class. This game multiplies three numbers between 1 and 10 that are inserted by the user. If the user inserts a number greater than 10, we want to skip over multiplying that number.
We could use the following code to accomplish this task:
import java.util.Scanner; class MathGame { public static void main(String[] args) { int total = 1; Scanner input = new Scanner(System.in); for (int i = 0; i < 3; ++i) { System.out.print("Enter a number: "); int number = input.nextInt(); if (number > 10) { continue; } total = total * number; } System.out.println("Total = " + total); } }
If we insert the numbers 2, 5, and 4 into our program, we receive the following output:
Enter a number: 2
Enter a number: 5
Enter a number: 4
Total = 40
2 x 5 × 4 is equal to 40. Because all our numbers are below 10, our "continue" statement does not execute. If we inserted 2, 5, and 11 into our code, this response is returned:
Enter a number: 2
Enter a number: 5
Enter a number: 11
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Total = 10
When we insert the number 11, our continue
statement is executed, and our program skips over multiplying the value of total
and 11.
The continue statement is often used with the Java break statement. break
terminates the loop within which it is enclosed, whereas continue
only skips one iteration in a loop and continues the loop.
Our program executes the continue statement when we execute the number 11. This means our program skips over multiplying the value of total and 11.
The continue statement is often used with the Java break statement. break statements terminates the loop within which it is enclosed. continue only skips one iteration in a loop and continues the loop.
Labelled continue Java Statement
A labelled Java continue statement lets you skip to a specified outermost loop. To use the labelled continue statement, specify the continue keyword followed by the name assigned to the outermost loop.
Labelled continue statements skip the execution of a statement that exists inside the outer loop of a program. Loops that contain contain loops are sometimes called nested loops.
Here's the syntax for a labelled continue statement:
Suppose we have a program that contains two loops, and we want to skip out to the outermost loop. We could do so using this code:
label_name: while (true) { while (true) { if (condition_is_met) { continue label_name; } } }
Conclusion
The Java continue statement is used to skip over the execution of a particular iteration in a loop.
This tutorial discussed how to use the Java continue statement to control the flow of a program, along with two examples. Additionally, we covered the basics of labelled continue statements in Java.
Now you're ready to start using the Java continue statement like a professional!
If you're looking for learning resources to help you master Java, check out our How to Learn Java guide.
Source: https://careerkarma.com/blog/java-continue/
0 Response to "What Does the Continue Statement Do in Java"
Post a Comment