The course is part of this learning path
In this course, we look at control flow statements in the Java programming language.
- Decision-making statements
- Looping statements
- Branching statements
- Anyone looking to get Oracle Java Certification
- Those who want to learn the Java Programming language from scratch
- Java developers who want to increase their knowledge
- Beginners with no previous coding experience in Java programming
- Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams
- No prior knowledge is required about the Java programming language.
- Basic computer knowledge
Hi there. In this lesson, we'll talk about another branching statement, Continue. The use of the continue keyword is just like the break keyword. The difference is that it's only used in loops. It's not used in the switch or if statements. The task of the keyword skips the relevant step of the loop and continues with the next step. For example, if you use the keyword continue at a stage that coincides with the fifth iteration of a 10 iteration loop, step 5 is skipped and will continue with step 6. Now let's move on to eclipse and get some practice. In exercise project, right click on the branching package and select new, class.
Specify the class name as ContinueStatement and select the check box for the main method. First let's use the continue statement in the standard for loop. In for loop, we declare a variable i and its initial value will be zero. Our condition is i less than 10 and it increments by one. If i modulars operator 2 is equal to zero means that if i is an even number, we use the continue statement to continue the loop. We use the print method to write numbers to screen outside the if statement, but in the for loop. Now let's examine the code. The initial value of i is zero and zero is less than the 10.
So, the condition is true. It moves to the next statement. In the if statement 0% 2 is 0. So, the condition is true and the continue keyword will evaluate and the first iteration will be skipped and the compiler goes to the second iteration. The print method here will not execute. In the second iteration, the new value of i will be 1. So, the 1 is less than the 10 so the condition is true and it moves the if statement again. But this time 1% 2 is 1 and the if condition is false. Therefore the continue keyword will not evaluate and the compiler will skip the if statement and go to the print method. After printing the number 1 to the console, it returns to the beginning of the for loop. Now the new value of i is two like that, it checks all conditions.
If the i is even, the condition of the if statement will be true and the continue keyword will evaluate and the relevant step will be skipped. If the i is odd, the condition of the if statement will be false and the continue keyword will not evaluate. So, it goes to the print method and prints the odd number to the console. Let's run the code. You see the odd numbers in the console. In this code, if the number is divisible by two, then it is even. So, if it's an even number, then the continue statement is executed. So, for this iteration, execution of the print statement is skipped and control of the program jumps to the end of the loop. The iteration process runs until i is 10. When i is 10, the condition is false and the for loop terminates.
Now let's do another example with for each loop, I'll convert these codes to the comment line. First, I'll create a string array. The name of this array can be cars and I'll initialize this array. The first element of this array can be Mercedes, the second element can be BMW, the third can be Ferrari, the fourth can be Opel, the fifth can Be Ford. And now let's create the for each loop. For string car : cars. And I'll create an if statement in the for each loop, if the car is equal to Opel, I'll use the continue keyword and after the if statement, I'll print the car object to the console. Before running the code, let's examine it. The string array has five elements with the for each loop, we access the elements of the array in order. In other words, the Mercedes element will be transferred to the car variable in the first iteration, BMW in the second iteration, Ferrari in the third.
In this way, the loop will continue. Of course we checked the value passed to the car object with the if statement in each loop. Whenever the value passed to the car object is Opel this time, the if condition will be satisfied and the compiler will enter in the if block. Since we are using the continue keyword here this step will be skipped and the next iteration will begin. Therefore Mercedes, BMW, Ferrari, and Ford will be printed on the console screen. Let's run it and test it. As you can see, Mercedes, BMW, Ferrari, and ford were printed on the console screen. Now let's do this example using the while loop and the index numbers of the elements in the array. First, let's convert these codes to the comment line. First, let's create a variable called index number of type int. Its initial value will be zero.
I'll also create a variable called car, where we'll pass each element of the array to a variable of type string. Let the initial value be null. Now let's create the while loop. Also as a condition, index number is less than cars.length. Here, I'll first transfer each element of the array to the variable car. Car = cars [IndexNumber]. If you remember, we could access the index element of the array in this way using the index number. Now, let's create an if statement as before. If the car is equal to Ford, I will use the continue keyword and I'll create the print method outside the if statement, but in the while loop. Finally, let's increase the index number by one. Thus, we'll also see which element was transferred to the variable car last.
We expect elements other than Ford to be printed on the console, but there is a point you should pay attention to here. If the index number is Ford, the if condition will be satisfied and the continue keyword will be executed. Therefore, this step will be skipped and the next step will be taken. But if you pay attention, the index number will not change because the continue keyword is before the increased process and the related step will be skipped before the increased process starts and the next step will continue with the same index number. Therefore, if we create the while loop in this way, an infinite loop will occur here. Let's test this if you want. Of course, I will copy and paste the print method just before the if statement so we can see it better.
We now have an infinite loop that prints the Ford element to the screen. Let's run it and see. As you can see, the Ford element is printed on the console forever. Let's end the loop. To fix this, we'll write the increment before the if statement and I'll delete this print method. Of course, I should also mention that if you use the increments statement at the beginning, you'll get an array index out of bounds exception error this time because the index number in the last element will be equal to the size of the array. However, the last index number was one less than the array size. So, our array consists of five elements and the index number of the last element is four. If you write the increments statement here, the index number variable will be five and there is no element in the fifth index.
The last index is four. Be careful. Yes. After this brief information, let's run the code again. As you can see, the elements other than the Ford were printed on the console, because when the element Ford is passed to the car variable, the condition in the if statement becomes true and the continue keyword is activated, and the compiler goes to the end of the loop without printing any element. Therefore, the last element Ford was not printed to the console. Also you can use the label with the continue keyword. For example you can type a label like car_loop here and put a colon. In this case, you should use the label after the continue keyword. This is optional and you can use it with all loops if and switch statements.
Now I'll give one last tip about the continue keyword and end the lesson. Can we use the continue keyword anywhere? Let's try to use the continue statement anywhere. Let's declare a variable number and its initial value can be 50. If the number equals 50, we try to use the continue keyword. But we get a compilation error because the continue keyword cannot be used outside of a loop. It is only used inside of a loop, but the if and switch are not a loop. If you see such usage, it's wrong. Yes, I think the continue keyword is understood. Let's take a short break here. See you in the next lesson.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.