0% found this document useful (0 votes)
25 views7 pages

Loops

The document discusses various looping constructs in C++ including for, while, do-while loops. It provides examples of each loop and covers concepts like break, continue and nested loops. The document also covers switch-case statements and the goto statement.

Uploaded by

usoul683
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views7 pages

Loops

The document discusses various looping constructs in C++ including for, while, do-while loops. It provides examples of each loop and covers concepts like break, continue and nested loops. The document also covers switch-case statements and the goto statement.

Uploaded by

usoul683
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C++ Loops

Loop:
In computer programming, loops are used to repeat a block of code.
For example, let's say we want to show a message 100 times. Then instead of writing the print
statement 100 times, we can use a loop.
There are 3 types of loops in C++.
for loop
while loop
do...while loop

C++ for loop


The syntax of for-loop is:
for (initialization; condition; update)
{
// body of-loop
}
Here,

initialization - initializes variables and is executed only once


condition - if true, the body of for loop is executed. if false, the for loop is terminated
update - updates the value of initialized variables and again checks the condition
Example 1: Printing Numbers From 1 to 5
#include <iostream>
using namespace std;
int main()
{
for (int i = 1; i <= 5; ++i)
{
cout << i << " ";
}
return 0;
}
Output
12345
Example: Find the sum of first n Natural Numbers
#include <iostream>
using namespace std;
int main() {
int num, sum;
sum = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 1; i <= num; ++i) {
sum += i;
}
cout << "Sum = " << sum << endl;
return 0;
}
Output
Enter a positive integer: 10
Sum = 55

1
C++ Infinite for loop
If the condition in a for loop is always true, it runs forever (until memory is full). For example,
// infinite for loop
for(int i = 1; i > 0; i++) {
// block of code
}
In the above program, the condition is always true which will then run the code for infinite times.

Nested loop
A loop within another loop is called a nested loop.
Example:
#include <iostream>
using namespace std;
int main()
{
int i,j;
for(i=1;i<4;i++)
{
for(j=1;j<5;j++)
cout<<j<<" ";
cout<<endl;
}
return 0;
}
Output
1234
1234
1234

C++ while Loop


The syntax of the while loop is:
while (condition) {
// body of the loop
}
Here,
A while loop evaluates the condition
If the condition evaluates to true, the code inside the while loop is executed.
The condition is evaluated again.
This process continues until the condition is false.
When the condition evaluates to false, the loop terminates.
Example 1: Display Numbers from 1 to 5
#include <iostream>
using namespace std;
int main() {
int i = 1;
// while loop from 1 to 5
while (i <= 5) {
cout << i << " ";
++i;
}
return 0;

2
}
Output
12345

C++ do...while Loop


The do...while loop is a variant of the while loop with one important difference: the body of
do...while loop is executed once before the condition is checked.
Its syntax is:
do {
// body of loop;
}
while (condition);
Example 3: Display Numbers from 1 to 5
#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;
}
Output
12345
Example: Sum of Positive Numbers Only
#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;
}
Output 1
Enter a number: 6
Enter a number: 12
Enter a number: 7
Enter a number: 0
Enter a number: -2
The sum is 25

3
Output 2
Enter a number: -6
The sum is 0.

C++ break Statement


In C++, the break statement terminates the loop when it is encountered.
The syntax of the break statement is:
break;
// program to print the value of i
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
// break condition
if (i == 3) {
break;
}
cout << i << endl;
}
return 0;
}
Output
1
2

C++ continue Statement


The continue statement is used to skip the current iteration of the loop and the control of the
program goes to the next iteration.
The syntax of the continue statement is:
continue;
Example 1: continue with for loop
// program to print the value of i
#include <iostream>
using namespace std;
int main() {
for (int i = 1; i <= 5; i++) {
// condition to continue
if (i == 3) {
continue;
}
cout << i << endl;
}
return 0;
}
Output
1
2
4
5

4
C++ switch..case Statement
The switch statement allows us to execute a block of code among many alternatives.
The syntax of the switch statement in C++ is:
switch (expression) {
case constant1:
// code to be executed if
// expression is equal to constant1;
break;
case constant2:
// code to be executed if
// expression is equal to constant2;
break;
.
.
.
default:
// code to be executed if
// expression doesn't match any constant
}
Example: Create a Calculator using the switch Statement
#include <iostream>
using namespace std;
int main() {
char oper;
float num1, num2;
cout << "Enter an operator (+, -, *, /): ";
cin >> oper;
cout << "Enter two numbers: " << endl;
cin >> num1 >> num2;
switch (oper) {
case '+':
cout << num1 << " + " << num2 << " = " << num1 + num2;
break;
case '-':
cout << num1 << " - " << num2 << " = " << num1 - num2;
break;
case '*':
cout << num1 << " * " << num2 << " = " << num1 * num2;
break;
case '/':
cout << num1 << " / " << num2 << " = " << num1 / num2;
break;
default:
// operator is doesn't match any case constant (+, -, *, /)
cout << "Error! The operator is not correct";
break;
}
return 0;
}
Output 1
Enter an operator (+, -, *, /): +

5
Enter two numbers:
2.3
4.5
2.3 + 4.5 = 6.8
Output 2
Enter an operator (+, -, *, /): -
Enter two numbers:
2.3
4.5
2.3 - 4.5 = -2.2
Output 3
Enter an operator (+, -, *, /): *
Enter two numbers:
2.3
4.5
2.3 * 4.5 = 10.35
Output 4
Enter an operator (+, -, *, /): /
Enter two numbers:
2.3
4.5
2.3 / 4.5 = 0.511111
Output 5
Enter an operator (+, -, *, /): ?
Enter two numbers:
2.3
4.5
Error! The operator is not correct.

C++ goto Statement


the goto statement is used for altering the normal sequence of program execution by transferring
control to some other part of the program.
Syntax of goto Statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
... .. ...
Example: goto Statement
# include <iostream>
using namespace std;
int main()
{
float num, average, sum = 0.0;
int i, n;
cout << "Maximum number of inputs: ";
cin >> n;
for(i = 1; i <= n; ++i)
{

6
cout << "Enter n" << i << ": ";
cin >> num;
if(num < 0.0)
{
goto jump;
}
sum += num;
}
jump:
average = sum / (i - 1);
cout << "\nAverage = " << average;
return 0;
}
Output
Maximum number of inputs: 10
Enter n1: 2.3
Enter n2: 5.6
Enter n3: -5.6
Average = 3.95

You might also like