To understand the “continue” statement on the Linux operating system, you need to have Ubuntu running on your system. So you must install Virtual Box and after downloading and installation now configure it. Now, add the Ubuntu file to it. Before adding, the file can easily be installed via the internet through a website that will take some hours to be completed. Run that file and after successful configuration, you can now use Linux flavor easily. During configuration, make sure that the user is created, this is necessary to create a user so that you can access all features of Ubuntu.
The version we have used is Ubuntu 20.04, all these codes explained in this guide are executable on any version. For the implementation, you need to have a text editor and must have access to the Linux terminal, because we will be able to see the output of the source codes on the terminal through the query. The user must have basic knowledge of C++ especially for and while loop should be familiar for the end-users.
To implement the continue statement, the keyword continue is used.
Difference of Continue Statement with the Break Statement
The break statement is said to be made for the switch statement, moreover, it can be included inside the while loop. And in the do-while loop and for a loop too. On the other hand, the continue statement is not a part of the switch statement, whereas it is used inside all types of loops. Our tutorial will guide you the best regarding the usage of the continue statement inside the loops. When in any C++ code, a break statement is faced by the compiler then it sends the control from the loop.
Working of Continue Statement in C++
The statements inside the loop are executed depending on the conditions applied to the loop. If the condition we have used inside the loop is true, then the control is inside the loop and it keeps the loop to execute further, but once the condition that we have applied is not true then the control comes away from the loop. And the execution also ceased to go further.
This example deals with the usage of FOR loop in the source code. Go to any text editor in Ubuntu operating system. Write the below-given code in it. Firstly for the input and output expression, we have used a library here.
The for loop is used in the main program. The limit applied is 7. It means that the loop will iterate 7 times. Inside the FOR loop, we have used an if-else statement. The condition using this statement is that if the iterating number reaches 4 during the execution of the loop, then the loop keeps on iterating without displaying that number. Otherwise, display the current number. In other words, all the numbers except 4 till 7 will be displayed in the output.
For each code to execute, we need a compiler that compiles the source code inside the file and then runs it. For C++ codes, we need a G++ compiler to execute the code.
From the output, you can see that our desired output doesn’t contain 4 in it, otherwise other numbers are displayed.
Now in this example, in the main program, we have used a while loop. The whole program is designed to calculate the sum of all the numbers until the condition becomes false. Two variables are initialized as 0. The while loop follows a condition, that states that a number entered by a user should be positive.
Inside the while loop, the sum variable calculates the sum of the numbers entered by a user. If-statement is used to check the condition if the given number is greater than 30, the further execution of the loop is terminated and the process of calculating the sum is stopped temporarily and a message is displayed.
After that, the system will again ask the user to enter the new value. And this will be carried out by using the ‘continue’ statement. And the loop continues to execute till the while statement remains true. If the number we enter is negative, then all the numbers the user has entered till now will be summed up.
Now go to the terminal again to see the output, use the compiler, and then the code will be executed.
The first system will display a message to enter the value in the blank space that is provided. As you enter the value, the compiler checks and the control remains inside the loop as the number is according to the condition we have applied.
You keep on entering the values. Even at 30, the number is acceptable, but if we exceed the number from 30, like 31, the number is not acceptable, and instead, a message is displayed. Now, the program will again continue because of the statement we use. Keep entering numbers until it’s negative, in that condition, the control will come out of the loop. The sum of all the numbers is displayed.
This is an example of nested loops. A nested loop is one in which a loop is used inside the other loop. For this kind of explanation, we have used for-loop twice. An outer FOR-loop and the second one inside it that is called an inner for loop. Inside the inner for loop, an if–statement is used. The terminating boundary for the outer loop is 2 and for the second loop, it is also 2.
The if-statement contains the ‘j’s variable to be equal to 2, to precede the execution of the loop. After that, continue is used to keep the loop executing. Similarly, when the execution of the inner loop is completed, the control comes out from the inner loop towards the outer one.
It means whenever the outer loop executes the value of j remains 1 always, for I =1,2,3 the value is one, in the next iteration, when the value of j becomes 2, the loop is terminated. You can see the output by using the command on the Linux terminal.
From the output, you can see that the value of j remains 1 irrespective of the value of I in the outer loop.
To explain the “continue” statement, you need to create a Linux environment. We have explained three examples in which a continued statement can be used. The concept is very easy and is clearly defined to remove the ambiguity from the end user’s mind. Moreover, the difference between the break and the continue statements is also explained.