`C++ Break
It was used to "jump out" of a switch statement.
The break statement can also be used to jump out of a loop.
This example jumps out of the loop when i is equal to 4:
Example
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
cout << i << "\n";
}
C++ Continue
The continue statement breaks one iteration (in the loop), if a specified condition
occurs, and continues with the next iteration in the loop.
This example skips the value of 4:
Example
for (int i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
cout << i << "\n";
}
Break and Continue in While Loop
You can also use break and continue in while loops:
Break Example
int i = 0;
while (i < 10) {
cout << i << "\n";
i++;
if (i == 4) {
break;
}
}
Continue Example
int i = 0;
while (i < 10) {
if (i == 4) {
i++;
continue;
}
cout << i << "\n";
i++;
}
Do While loop
Syntax
The syntax of a do...while loop in C++ is −
do {
statement(s);
}
while( condition );
Notice that the conditional expression appears at the end of the loop, so the
statement(s) in the loop execute once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the
statement(s) in the loop execute again. This process repeats until the given
condition becomes false.
Example 1: Do-While Loop
#include <iostream>
using namespace std;
int main() {
int i = 1;
// do...while loop from 1 to 5
do {
cout << i << " ";
++i;
}
while (i <= 5);
return 0;
}
C++ Program
#include <iostream>
using namespace std;
int main() {
int i=0;
do {
cout << "Hello" << endl;
} while (++i < 5);
}
Output
Hello
Hello
Hello
Hello
Hello
Example 2: While Loop to Compute Factorial
C++ Program
#include <iostream>
using namespace std;
int main() {
int n = 5;
int factorial = 1;
int i = 1;
do {
factorial *= i;
} while (++i <= n);
cout << factorial << endl;
}
Example
#include <iostream>
using namespace std;
int main () {
// Local variable declaration:
int a = 10;
// do loop execution
do {
cout << "value of a: " << a << endl;
a = a + 1;
} while( a < 20 );
return 0;
}
When the above code is compiled and executed, it produces the following
result −
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
program to find the sum of positive numbers
// If the user enters a negative number, the loop ends
// the negative number entered is not added to the sum
#include <iostream>
using namespace std;
int main() {
int number = 0;
int sum = 0;
do {
sum += number;
// take input from the user
cout << "Enter a number: ";
cin >> number;
}
while (number >= 0);
// display the sum
cout << "\nThe sum is " << sum << endl;
return 0;
}
LCM of Two Numbers Program in C++
#include <iostream>
using namespace std;
int main() {
int n1, n2;
cout << "Enter first number : ";
cin >> n1;
cout << "Enter second number : ";
cin >> n2;
int lcm;
lcm = (n1 > n2) ? n1 : n2;
do {
if (lcm % n1 == 0 && lcm % n2 == 0) {
break;
}
else {
lcm++;
}
} while (lcm < (n1 * n2));
cout << "LCM : " << lcm << endl;
}
Nested Do-While Loop
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
int j = 1;
do {
cout << " *";
} while (++j <= i);
cout << "\n";
} while (++i <= 5);
}
Output
*
* *
* * *
* * * *
* * * * *
#include <iostream>
using namespace std;
int main() {
int i = 1;
do {
int j = 5;
do {
cout << " *";
} while (--j >= i);
cout << "\n";
} while (++i <= 5);
}