How to Continue a Statement to Next Line C
Introduction to Continue Statement in C++
Continue statement is a loop control statement used inside the loop. To implement continue statement, C++ uses continue keyword which transfers the flow of the program at the beginning of the loop, and skip the current statement when it is encountered.
Syntax:
Continue keyword is used. The syntax for the continue statement in C++ is as follows:
continue;
Flow Chart
The Flowchart for the continue statement is as follows:
How does Continue Statement Work in C++?
Based on the loop condition the statements inside the loop are executed. If the given condition is true it continues the iteration of the loop. if the given condition is false, the loop will be terminated. If the continue statement is mentioned inside the loop, it skips the current statement and transfers control to execute the next iteration of the loop. To understand how exactly the continue statement works in C++, let's discuss some examples for better understanding.
Example #1 – Usage of Continue Statement in For Loop
In the following C++ program, we will see how to use of continue statement in a For loop. For this the user can provide the number and then result will be displayed on the screen:
Code:
#include<iostream> using namespace std; int main() { int i, n; cout << "Enter a number"; cin >> n for(i = 0; i <=n; i++) { if(i == 2) { continue; } cout << "List of numbers"; cout << i; } return 0; }
Output:
Explanation of the above Program:In the above program, to show the implementation of the continue statement, we have used for loop method. The program is written to print the list of numbers specified by the user. Here we have initialized two variables n to store the value and me for the iteration. The program first asks the user to enter the number and then store the number into n. In for loop, we have initiated I to 0 and mentioned a condition that checks that I should be less than or equal to n. Inside the loop, we have mentioned the continue statement to skip number 2. when I become 2, it skips that value and continues the loop. if the number is not 2, then Statement outside the if will be executed and prints the number one by one.
Example #2 – Usage of Continue Statement in While Loop
In the following C++ program, we will see how to use of continue statement in a While loop then result will be displayed on the screen:
Code:
#include<iostream> using namespace std; int main() { int i, n; cout << "Enter a number"; cin >> n while(i <= n) { if(i == 2) { i++; continue; } cout << "List of numbers"; cout << i; i++; } return 0; }
Output:
Explanation of the above Program:In the above program, to show the implementation of the continue statement, we have used the While loop method. The program is written to print the list of numbers specified by the user. Here we have initialized two variables n to store the value and me for the iteration. The program first asks the user to enter the number and then store the number into n. While loop, we have initiated I to 0 and mentioned a condition that checks that I should be less than or equal to n. Inside the loop, we have mentioned the continue statement to skip number 2. when I become 2, it skips that value and continues the loop. if the number is not 2, then Statement outside the if will be executed and prints the number one by one.
Example #3 – Usage of Continue Statement in Do While Loop
In the following C++ program, we will see how to use of continue statement in a Do While loop then the result will be displayed on the screen.
Code:
#include<iostream> using namespace std; int main() { int i, n; cout << "Enter a number"; cin >> n do { if(i == 2) { i++; continue; } cout << "List of numbers"; cout << i; i++; }while(i <= n); return 0; }
Output:
Explanation of the above Program:In the above program, to show the implementation of the continue statement, we have used Do While loop method. The program is written to print the list of numbers specified by the user. Here we have initialized two variables n to store the value and me for the iteration. The program first asks the user to enter the number and then store the number into n. In Do While loop, we have initiated I to 0 and mentioned a condition that checks that I should be less than or equal to n. Inside the loop, we have mentioned the continue statement to skip number 2. when I become 2, it skips that value and continues the loop. if the number is not 2, then the Statement outside if will be executed and prints the number one by one.
Conclusion
In this article, we have seen different programs to understand how they continue statement works in C++. I hope you will find this article helpful.
Recommended Articles
This is a guide to Continue Statement in C++. Here we discuss different programs to understand how they continue statement works in C++. You can also go through our other related articles to learn more –
- Constructor and Destructor in C++
- Continue Statement in Java
- Break Statement in Java
- C++ Garbage Collection
Source: https://www.educba.com/continue-statement-in-c-plus-plus/
0 Response to "How to Continue a Statement to Next Line C"
Post a Comment