In a programming often a situation may comes where we want to execute the set of code again and again.
Loop has a ability to repeat set of statement until a condition to be satisfied or a particular number of times.
There are following types of loop in C++ language:
In a C++ programming language we can also define a loop within another loop.
- while loop
- do…while loop
- for loop
- nested loops
while loop
“while loop” repeats a set of statements if condition is true.
If condition is false the flow of control do not enter inside of while loop.
It first tests the condition then execute the body of loop.
Syntax of a while loop −
1 2 3 4 5 6 7 8 9 | while(condition) { Statement1; statement2; ---- ---- Increment/Decrement; } |
Example: Write a program to print 1 to 10 using while loop.
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> using namespace std; int main() { int a = 1; while (a <= 10) // loop executes 10 times { cout << a; a++; } return 0; } |
Output:
1 | 1 2 3 4 5 6 7 8 9 10 |
Description: In the above program, line “while( a <= 10 )” will check if condition is true or false.
In the above case when a=1, condition is true and flow of control move inside of while and print 1 after that increment occurs.
Now a=2, again condition is true and flow of control move inside of while and print 2 after that increment occurs.
Now a=3, again condition is true and flow of control move inside of while and print 3 after that increment occurs.
This process is continue until condition is false(in this case condition false occur when a=11).
Example: Write a program to print 10 to 1 using while loop.
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> using namespace std; int main() { int a = 10; while (a > 0) // loop executes 10 times { cout << a<<" "; a--; } return 0; } |
Output
1 | 10 9 8 7 6 5 4 3 2 1 |
do-while loop
like a while loop, “ do while loop” repeats a set of statements if condition is true.
If condition is false then the flow of control do not enter inside the do-while loop.
“do-while” first execute the body of loop even if the condition is false than tests the condition.
Means even if the condition if false do-while executes at least ones.
The syntax of a do…while loop in C programming language is −
1 2 3 4 5 6 7 8 | do { Statement1; statement2; ---- ---- Increment/Decrement; } while( condition ); |
Example: Write a program to print 1 to 10 using while loop.
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> using namespace std; int main() { int a = 1; do { cout << a<<" "; a++; } while (a <= 10); // loop executes 10 times return 0; } |
Output
1 | 1 2 3 4 5 6 7 8 9 10 |
Even if the condition if false do-while executes at least ones.
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> using namespace std; int main() { int a = 100; do { cout << a; a++; } while (a <= 10); // loop executes 10 times return 0; } |
Output
1 | 100 |
Description: in the above program do-while executes ones even condition of is false.
Because do-while first executes the statement then check the condition.
For Looping Statement
“for-loop” repeat the set of statement until a particular condition to be satisfied or a particular number of times.
Syntax of for-loop:
1 2 3 4 | for(initialization; condition; iteration ) { statement 1; statement 2; -------------- } |
“for-loop” has a initialization , condition and iteration (increment/decrement) part separated by semicolon.
Initialization : This phase allows you to initialize loop control variables.
Condition : If condition of for-loop is true, then the body of the loop will execute.
And the body of the loop does not execute if the condition is false, and the flow of control jumps to the next statement just after the ‘for’ loop
Iteration phase: This statement allows you to update(increment/decrement any loop control variables..
Example: Write a program to print 1 to 10 using for-loop.
1 2 3 4 5 6 7 8 9 10 11 | #include <iostream> using namespace std; int main() { int a; for (a = 1; a <= 10; a++) { cout << a << "\n"; } return 0; } |
Output
1 2 3 4 5 6 7 8 9 10 | 1 2 3 4 5 6 7 8 9 10 |
In a for loop either initialization or iteration , or both may be absent but condition must be present in a for loop.
1 2 3 4 5 6 7 8 9 10 11 12 | #include <iostream> using namespace std; int main() { int a = 1; for (; a <= 10;) { cout << a << "\n"; a++; } return 0; } |
Output
1 2 3 4 5 6 7 8 9 10 | 1 2 3 4 5 6 7 8 9 10 |
Example: Write a program to take a number from user and find its factorial.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include <iostream> using namespace std; int main() { int i, n, fact = 1; cout << "Enter a number to calculate its factorial\n"; cin >> n; for (i = 1; i <= n; i++) { fact = fact * i; } cout << "Factorial = " << fact; return 0; } |
Output
1 2 3 | Enter a number to calculate its factorial 5 Factorial = 120 |
Example: Write a program to take a number from user and check whether a number (entered by the user) is a prime number or not.
Prime Number : prime number is a number which satisfies the following conditions.
- It should be whole number
- It should be greated than 1
- It should have only 2 factors. They are, 1 and the number itself.
Example for prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23,29 etc. because this numbers is only divided by 1 and the number itself.
Number 4, 6, 8, 9, 10, 12, 14, 15, 16 etc are not prime numbers. Because, the number 4 can be divided by 2.
As per the rule of prime number, should be divide 2 numbers only.
They are 1 and the number itself. But, number 4 is also divided by 2. Similarly , all remaining numbers 6, 8, 9, 10, 12, 14…….. also divided by a number other than 1 and the number itself.
Therefore these are not a prime numbers.
Number 1 is neither a prime nor a composite number.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #include <iostream> using namespace std; int main() { int n, i, flag = 0; cout << "Enter a number to check Whether a Number is Prime or Not: "; cin >> n; for (i = 2; i <= n / 2; ++i) { if (n % i == 0) // condition for nonprime number { flag = 1; break; } } if (n == 1) { cout << "1 is neither a prime nor a composite number."; } else { if (flag == 0) { cout << " Entered number is a prime number."; } else { cout << " Entered number is not a prime number."; } } return 0; } |
Output
1 2 3 | Enter a number to check Whether a Number is Prime or Not: 7 // enter by user Entered number is a prime number. |