Nesting? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Nesting?

I am having a little difficulty wrapping my head around what nesting is could someone explain or give me a simple example of what nesting is and also what is this good for? is it just a fancy word for encapsulating code or is this two different meanings?

29th May 2017, 6:02 PM
Daen Rowe
Daen Rowe - avatar
5 Answers
+ 8
Nesting in a very simple way is- "Something inside Something". In C++, When we use a loop inside a loop, we says that inner loop is nested inside the outer loop. Confused? See this example- for(int i=0;i<10;i++) //loop 1 { for(int j=0;j<10;j++) //loop 2 { cout<<"Hello"; } } You can clearly see that loop 2 is inside loop 1, so it is nested loop. While, Encapsulation is wrapping of all the data into a single unit. In OOP, we say that Class is a user defined datatype that achieves encapsulation because- "We wrap the data members (variables) and member functions (functions) into a single unit called class".
29th May 2017, 6:17 PM
Sachin Artani
Sachin Artani - avatar
+ 8
So many, like you may have heard about programs for making patterns like a pyramid of "*", "#", or pascal triangle, All they are very hard to do without nesting, because they must need two or more loops for their constructions and those loops must be outer loops and an inner loops. There are many more examples you will see when you lesrn more and morw about C++ 😉
29th May 2017, 6:26 PM
Sachin Artani
Sachin Artani - avatar
+ 5
Mark the answer best you are satisfied with. It's a good habit for all us to do 😊 And You're welcome
29th May 2017, 6:32 PM
Sachin Artani
Sachin Artani - avatar
+ 4
Thanks, I now understand it perfectly. What are the advantages of nesting?
29th May 2017, 6:21 PM
Daen Rowe
Daen Rowe - avatar
+ 4
I see, I greatly appreciate you're help Sachin. Thank you.
29th May 2017, 6:28 PM
Daen Rowe
Daen Rowe - avatar