Nested Loop in c++:
A circle inside another circle is known as a settled circle. We should accept a model,
Assume we need to circle during each time of seven days for a considerable length of time.
To accomplish this, we can make a circle to repeat multiple times (3 weeks). What's more, inside the circle, we can make one more circle to emphasize multiple times (7 days). This is the means by which we can utilize settled circles.
Settled circle implies a circle proclamation inside another circle explanation. For that reason settled circles are likewise called as "circle inside circle".
Sentence structure for Settled For circle:
for ( initialization; condition; increment ) {
for ( initialization; condition; increment ) {
// statement of inside loop
}
// statement of outer loop
}
Syntax for Nested While loop:
while(condition) {
while(condition) {
// statement of inside loop
}
// statement of outer loop
}
Syntax for Nested Do-While loop:
do{
// statement of inside loop
}while(condition);
// statement of outer loop
}while(condition);
Note: There is no rule that a loop must be nested inside its own type. In fact, there can be any type of loop nested inside any type and to any level.
Syntax for nested for loop:
do{
while(condition) {
for ( initialization; condition; increment ) {
// statement of inside for loop
}
// statement of inside while loop
}
// statement of outer do-while loop
}while(condition);
Below are some examples to demonstrate the use of Nested Loops:
Example 1: Below program uses a nested for loop to print a 2D matrix of 3×3.
Given matrix is
123
456
789
Example 2: Below program uses a nested for loop to print all prime factors of a number.
No comments:
Post a Comment