As prominent from the title, we are going to write a c++ program to print tables from 2 to 10 using nested loops. We have already discussed all the basics in the previous post about loops and now, we will learn how to nest for and while loops in c++. For this purpose, we will write the same program using nested for loop as well as nested while loop to feel the difference. The comparison between both will help you understand when to use which loop. so let's start.
c++ program to print tables from 1 to 10 using nested for loop
Let's consider we want to write a program to print the table of numbers from 2 to 10. In this program, we will first print the title of the table e.g(table of 2). Then, we will print ten multiples of that table. This whole process will repeat 9 times to print the table of all numbers between 2 to 10 with their respective title and ten multiples. We will print the tables according to the format(2 X 1=2).#include <iostream> using namespace std; int main() { //Outer loop for (int i = 2; i <=10; i++) { cout << "Table of " << i << endl; //Inner loop for (int j = 1; j <= 10; j++) { cout <<i<<" X "<<j<<"=" << i * j <<" "; } cout << endl; } system("pause"); return 0; }
Now Just look at the above code. Its printing table of numbers between 2 to 10. The first and important thing to notice is there are two loops in this program. Its a nested for loop comprised of an outer and an inner loop. The outer loop is independent while the inner loop is dependent on the outer loop for its execution. Let's see what happens when we run the above program.
- First and foremost, the outer loop is executed and it prints the cout statement in the first iteration.
- Then the control is passed to the inner loop and it starts printing multiples of i.
- Once all the 10 iterations of the inner loop are completed, the cout statement followed by the inner loop is printed which completes the first iteration of the outer loop after updating the loop counter(i) value. The control is again passed to the outer loop for the second iteration.
- The above three steps are repeated until the outer loop completes its 9 iterations. In this way the above c++ program to print tables from 1 to 10 using nested for loop works.
c++ program to print tables from 1 to 10 using nested while loop
Now let's see how can we write the same program using nested while loop. As we have discussed in our previous post with an example that while loops are better to use in circumstances where the number of iterations are unknown. This program also has a known number of iterations. Let's see what complexity while loops create in this program.#include <iostream> using namespace std; int main() { int i = 2; //Outer loop while (i <=10) { int j = 1; cout << "Table of " << i << endl; //Inner loop while ( j <= 10) { cout <<i<<" X "<<j<<"=" << i * j <<" "; j++; } cout << endl; i++; } system("pause"); return 0; }
Here is the code of the same program using nested while loop. There is not a big difference between the two loops. We easily wrote the program but few things were a little disturbing that give for loops a plus point. Unlike for loop, we have to manage counter variable, condition, and increment statements separately which slightly increase our effort and complexity. So, in circumstances where iterations are known for loops are highly recommended whether it is nested or simple. While loops/nested while loops are better to implement infinite loops, loops where iterations are unknown, loops where iterations depend on user input, etc.
Output of both programs
c++ program to print tables from 2 to 10 using nested loop |
That's all about nesting for and while loops and their comparison. Thanks for reading...
0 Comments