C How to Continue at a Line in Lopp
"C is a great programming language, and it's also one of the first ones. "C" is the basis of several contemporary programming languages. In this article, we'll use the "C" programming "continue" statement in conjunction with loops. We utilized the "continue" statement; if we want to ignore certain statements in the loop, it won't terminate the loop like a break statement. When we use "continue," the control moves to the loop's subsequent iteration rather than exiting the loop."
Example# 1: Continue Statement With for-loop
We will demonstrate and learn how to use the "continue" statement inside the "for loop" in this example. The code below illustrates how to implement the "continue" statement in a "for loop".
The code is started by including header files "#include <stdio.h>". It is a directive that instructs the compiler to place the contents of "stdio" at that specific location. All necessary operations related to input and output devices that are contained in the code are provided in this "stdio" part. Next, we used the "main()" function with an "int" type in the following line. It will provide us with integer type values if we utilize the "int main()" function. "Int" is the datatype for integral values.
Inside the curly bracket, a variable with an integer datatype has been initialized as "i=10". Then we initialized the "for-loop" where we set the value of "i" equal to "0," and after that, we defined a condition that says "i" must be less than or equal to "9," and the final component said to raise the value of "i". As a consequence, this loop as a whole asserts a condition for the value of "i" that starts from "0" and increments the value of "i" with each condition check until it is less than or equal to "9". The for-line loop's brackets began appearing in the subsequent line; we are using an "if statement" and a "continue" statement within these brackets. A programming conditional statement is known as an "if" executes a function or displays data if it is true.
Within loops, the continue statement is utilized. When a continue statement is contained within a loop, the control jumps to the start of the loop for the subsequent repetition, skipping over the execution of any statements contained within the loop's content for the current iteration. The condition in this code states that if "i" equals "5", then the continue statement will be encountered. Now in the next line, we used a print statement inside the print function; we used "%d" and "i". The datatype specifier "%d" is used in C programming to define the type of data that will be stored and displayed. Here, "%d" has been added to denote that the data are decimal integers.
After checking and satisfying the for-loop condition, the variable "i" will print each value that is stored in it once at a time. When the loop starts, the controller will check the if condition; if the value is equal to "5," the control will jump inside the if condition and check whatever statement is specified within it, as we have specified the "continue" statement within the if condition this will make the control back to the loop and continuing the implementation process, but when it is not "5" the control will jump outside the if condition and checks whatever the statement is specified within it.
The outcome shows that aside from "5," all digits between 0 and 9 are displayed. The numbers began at zero since we initialized the value of "i" as "0," and it ended on "9" the loop ends when the value exceeds "9". Because we applied the condition if "i ==5," then the loop continued rather than going to the below print function because of the "continue" statement, so "5" is not displayed.
Example # 2: Continue Statement With do-while-loop
In this example, we are going to use the "continue" statement inside the while-loop. To repeatedly iterate a section of the script, uses a do while loop. It is advised to use a do-while loop if the amount of iterations is not set and you need to run the loop at least once. Because the condition is verified after the body of the loop, the do-while loop is run at least once. The image below will show how to use the continue statement with a do-while loop.
Let's start our second example code by first adding the "stdio.h" header files in the script. The "main()" method was then used with "int" datatypes. Furthermore, on the following line, between these curly brackets, we defined the integer variable "x" and gave it the value "8". After that, we used the do function in the following line to apply the condition; if the value is equal to 10, the control will move inside the condition and verify whatever the condition or statement is stated in it. Since we used the "if condition," if the "x" value is equal to "10," it will be increased by once.
The controller will then return to the loop because we defined a "continue" statement within the "if condition" and then continued the procedure. The value of x is printed in the following line using the "print()" function once the do-while loop's condition has been verified, and then the value of "x" has been incremented. After specifying the condition, we also used "%d/n" inside the print function to define the datatype format and jump to the next line. The values are kept inside the variable "x" after being specified. After that, we utilized the while statement to apply the condition that the loop will end if the value of "x" is less than "15". As a result, after printing the numbers 8 through 14, the program will stop because the value did not satisfy the condition. Let's move to its outcome.
You must click "run" to see its output.
In the cited above figure, the values of "x" are displayed. Since the loop passes from "8" to "14," the values of "x" are 8 to 14, but the value "10" is not printed because we applied the "if condition" that says if the value equals "10", it will increment once in it. However, inside the "if condition," we also used the "continue" statement, so because of that, the control will go back to the loop and continue the next iteration.
Conclusion
We have covered the use of the "continue" statement in the "C" language in this guide. Two instances of "continue" statements being used with loops have been discussed. The first example comprehends the use of the "continue" statement inside the for-loop, and the second instance utilizes the continue statement in the do-while-loop. For each type of loop, once the conditions are matched, the control executes the continue-statement. The final outputs are rendered for each C program generated in this tutorial.
Source: https://linuxhint.com/continue-statement-c/
0 Response to "C How to Continue at a Line in Lopp"
Enregistrer un commentaire