Nested Loop Concepts
Nested Loop Concepts
Harapriya Mohanta
Nested for loop means:
→ a for loop inside another for loop
*************
************* or
Harapriya Mohanta
How will i print these Patterns ?
To print these
******
types of Pattern,
******
“nested loop”
******
is needed
******
******
*
**
***
****
***** Harapriya Mohanta
How nested loops work?
How Nested for loop works:
TRUE
TRUE
Harapriya Mohanta
How Nested for loop works:
condition check
first,i initialized TRUE
for (int i = 1;i<=3; i++)
{
Harapriya Mohanta
How Nested for loop works:
Harapriya Mohanta
How Nested for loop works:
Now i++; executed
now i =2; 2<=3(true)
for (int i = 1;i<=3; i++) again go inside the outer loop
{
outer loop variable inner loop runs
}
That means, for each
} value of i, inner loop runs
maximum no. of times
Harapriya Mohanta
How Nested for loop works:
outer loop variable inner loop runs
}
That means, when outer
} loop runs 3 times, my inner
loop will run 9 times.
Harapriya Mohanta
Patterns
Harapriya Mohanta
How will i print these Patterns ?
******
******
******
******
******
*
**
***
****
*****
Harapriya Mohanta
Outer loop print
ROW for (initialization;cond; inc/dec)
{
Inner loop print
for(initialization;cond; inc/dec) Column
{
//innerloop
}
}
Harapriya Mohanta
PATTERN 1 :
Figure out
******
****** How many rows i have to be printed?
******
****** How many columns to print on each row?
Harapriya Mohanta