Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Thursday, November 26, 2020

Nested for loop in C

Nested for loop: refers to the process of having one loop inside another loop. We can have multiple loops inside one another.

Nested for loop in C | welcome2protec.com

Nested For Loop:

In the previous tutorial of if-else, control statements we saw what nested if is. Nested for loop refers to the process of having one loop inside another loop. We can have multiple loops inside one another. The body of one ‘for’ loop contains the other and so on. The syntax of a nested for loop is as follows (using two for loops): Let's have a look at the below example:



Syntax:

for(initialization; test; update) 
{
  for(initialization; test; update) //using another variable
  {
    //body of the inner loop
  }
 //body of outer loop(might or might not be present)
} 

Example 1: Nested For Loop

/*Program to prints a triangular pattern numbers*/
#include <stdio.h> 
int main()
{
 int i,j;
 for(i=1; i<=5; i++)
 {
  for(j=1; j<=i; j++)
   printf("%d", j);
  
   printf("\n");
 }
 return 0;
 }

Output

1 
12 
123 
1234 
12345 

Explanation:

Above program prints a triangular pattern. Now, the outer loop starts with i=1 and checks the condition whether i<= 5 is true or not which is true so the control goes on the body which contains another loop. This loop starts with the initial value as j=1 and checks if j<=i which is true and hence prints the value of j.

In the next iteration, the condition of the inner loop becomes false and the control exits the inner loop and changes the line. Now it goes for the next iteration of the outer loop and the process goes on. unless the condition becomes false and the loop terminates and the program ends.


Example 2: Nested For Loop

// Example of nested for loop:
#include <stdio.h>
int main()
{
    for(i=0; i<2; i++)
    {
        for(j=0; j<=3; j++)
        {
           printf("%d, %d\n", i, j); 
        {
        sum += count;
    }
    return 0;
 }

Output

0, 0
0, 1
0, 2
0, 3
1, 0
1, 1   
1, 2  
1, 3








Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us