ch5C++ Loops
ch5C++ Loops
PROGRAMMING
C++ for Loop
C++ FOR 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.
That was just a simple example; we can achieve much more efficiency and
sophistication in our programs by making effective use of loops.
for loop
while loop
do...while loop
C++ FOR LOOP
The syntax of for-loop is:
int main() {
for (int i = 1; i <= 5; ++i) {
cout << i << " ";
}
return 0;
}
EXAMPLE 2: DISPLAY A TEXT
5 TIMES
// C++ Program to display a text 5 times
#include <iostream>
int main() {
for (int i = 1; i <= 5; ++i) {
cout << "Hello World! " << endl;
}
return 0;
}
EXAMPLE 3: FIND THE SUM OF FIRST N NATURAL NUMBERS
/ C++ program to find the sum of first n natural numbers
#include <iostream>
int main() {
#include <iostream>
int main() {
int num_array[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
for (int n : num_array) {
cout << n << " ";
}
return 0;
}
C++ WHILE AND
DO...WHILE 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.
That was just a simple example; we can achieve much more efficiency and
sophistication in our programs by making effective use of loops.
for loop
while loop
do...while loop
C++ WHILE LOOP
while (condition) {
// body of the loop
}
Here,
#include <iostream>
using namespace std;
int main() {
int i = 1;
// while loop from 1 to 5
while (i <= 5) {
cout << i << " ";
++i;
}
return 0;
}
EXAMPLE 2: SUM OF POSITIVE NUMBERS ONLY
// 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;
int sum = 0;
// take input from the user
cout << "Enter a number: ";
cin >> number;
while (number >= 0) {
// add all positive numbers
sum += number;
// take input again if the number is positive
cout << "Enter a number: ";
cin >> number;
}
// display the sum
cout << "\nThe sum is " << sum << endl;
return 0;
In this program, the user is prompted to enter a number, which is stored in
the variable number.
In order to store the sum of the numbers, we declare a variable sum and
initialize it to the value of 0.
The while loop continues until the user enters a negative number. During
each iteration, the number entered by the user is added to the sum
variable.
When the user enters a negative number, the loop terminates. Finally, the
total sum is displayed.
The do...while loop is a variant of the while loop with one important
C++ DO...WHILE LOOP
The body of the loop is executed at first. Then the condition is evaluated.
If the condition evaluates to true, the body of the loop inside the do
statement is executed again.
The condition is evaluated once again.
If the condition evaluates to true, the body of the loop inside the do
statement is executed again.
FLOWCHART OF DO...WHILE LOOP
EXAMPLE 3: DISPLAY NUMBERS FROM 1 TO 5
#include <iostream>
int main() {
int i = 1;
int count = 1;
do {
// body of loop
}
while(count == 1);
C++
Lessons
C++ BREAK STATEMENT
In C++, the break statement terminates the loop when it is encountered.
The syntax of the break statement is:
break;
WORKING OF C++ BREAK STATEMENT
EXAMPLE 1: BREAK WITH FOR LOOP
/ program to print the value of i
#include <iostream>
using namespace std;
int main() {
return 0;
}
EXAMPLE 2: BREAK WITH WHILE LOOP
/ program to find the sum of positive numbers
#include <iostream>
// add all positive numbers
sum += number;
using namespace std;
}
int main() {
// display the sum
int number; cout << "The sum is " << sum << endl;
int sum = 0;
return 0;
while (true) { }
// take input from the user
cout << "Enter a number: ";
cin >> number;
// break condition
if (number < 0) {
break;
}
C++ CONTINUE
STATEMENT
In computer programming, the continue statement is used to skip the
current iteration of the loop and the control of the program goes to the
next iteration.
continue;
WORKING OF C++ CONTINUE STATEMENT
EXAMPLE 1: CONTINUE WITH FOR LOOP
In a for loop, continue skips the current iteration and the control flow jumps to the
update expression.
/ 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;
}
This means
When i is equal to 3, the continue statement skips the current iteration and
starts the next iteration
Then, i becomes 4, and the condition is evaluated again.
Hence, 4 and 5 are printed in the next two iterations.
Note: The continue statement is almost always used with decision-making
statements.
EXAMPLE 2: CONTINUE WITH WHILE LOOP
In a while loop, continue skips the current iteration and control flow of the
program jumps back to the while condition.
// program to calculate positive numbers till 50 only
// if the user enters a negative number,
// that number is skipped from the calculation
When continue is used with nested loops, it skips the current iteration of the
inner loop. For example,
return 0;
}
C++ SWITCH..CASE STATEMENT
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
}
HOW DOES THE SWITCH
STATEMENT WORK?
The expression is evaluated once and compared with the values of each
case label.
If the break statement is not used, all cases after the correct case are executed.
C++ GOTO STATEMENT
In C++ programming, goto statement is used for altering the normal sequence
of program execution by transferring control to some other part of the
program.
The goto statement can be replaced in most of C++ program with the use
of break and continue statements.
END