In programming, loops are used to repeatedly execute the same block of code multiple times. It is used when we have to perform some repetitive task. In C++, we have three types of loops available and these are:
The while and do...while loops are used when we don't know the exact count of how many times the loop is going to run whereas for loop is used when we know the exact count. Here we are going to discuss for loop in detail with many examples.
C++ For Loop Syntax:
for (initialization; condition; update) { // code block to execute }
For loop is an entry control loop which means the given condition is evaluated first each time before entering inside the loop. In the above syntax:
- initialization: Initialize a variable and executed only one time before entering the loop body.
- condition: This condition is checked every time and if it returns true then only the code block inside the body of the loop is executed.
- update: It is executed every time and updates the value of the initialized variable.
Flowchart of For Loop in C++
//C++ For Loop example to print natural number #include<iostream> using namespace std; int main(){ //print 5 natural number for(int i = 1; i <= 5; i++){ cout<<i<<endl; } return 0; }
1
2
3
4
5
Here, we have initialized the variable i with our first value which is 1, and then we have defined a condition (i <= 5) that we will print the value of i till this condition is true and each time after printing the value of i we are updating the value of i by 1 using increment operator.
C++ Example 2: Print the sum of 10 natural numbers
//C++ For Loop example to print sum of n natural number #include<iostream> using namespace std; int main(){ int sum = 0; //print the sum of 10 natural numbers for(int i = 1; i <= 10; i++){ sum += i; } cout<<sum; return 0; }
55
Here we have initialized the variable i with 1 and given a condition (i <= 10) which is indicating that the loop is going to run 10 times and each time it is taking the value of i and adding the value with the sum variable. After the for loop gets terminated program will print the sum of the first 10 natural numbers.
Nested For Loop in C++
When one for loop is present inside another for loop then it is called a nested for loop. The inner loop will execute completely for each iteration of the outer loop.
Example of Nested for Loop.
//C++ Example for Nested For Loop #include<iostream> using namespace std; int main(){ //outer loop for(int i = 1; i <= 3; i++){ cout<<"\nThis is outer loop "<<i; //inner loop for(int j = 1; j <= 3; j++){ cout<<"\nThis is inner loop "<<j; } cout<<"\n"; } return 0; }
This is outer loop 1
This is inner loop 1
This is inner loop 2
This is inner loop 3
This is outer loop 2
This is inner loop 1
This is inner loop 2
This is inner loop 3
This is outer loop 3
This is inner loop 1
This is inner loop 2
This is inner loop 3
Foreach Loop in C++
The for-each loops are used to iterate over the elements of an array or any other data sets. This loop reduces the chances of error and increases the overall performance of the code.
Syntax of a for-each loop:
for (type variable name : array_name){ //code block }
Example of a for-each loop:
//C++ Example of foreach Loop #include<iostream> using namespace std; int main(){ int arr[] = {3, 5, 9, 1, 4}; //printing array element for(int i : arr){ cout<<i<<" "; } return 0; }
3 5 9 1 4
The for-each loop is very easy to use but there are many disadvantages also like you cannot traverse the array in the reverse direction and you cannot skip any element while traversing the array. (alert-warning)
Few example problems:
No comments:
Post a Comment