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

loop within a loop

so friends i get confused sometimes when to use 2 loops inside main loop or when to use 3. for example in case of bubble sorting we use 2 loops and in case of printing the pyramid we use 3 loops. so basically how do we get to know that a problem requires one loop , 2 loop , 3 loops etc within a loop.

27th Jul 2016, 7:16 AM
Karan Luther
Karan Luther - avatar
2 Answers
+ 2
i think you should first of all think About problem until.you.understand what u need. for example, you want to see whether there is a number in.array or not, so you need one loop,but if you need search in matrix, you need 2 loop... one for cols and one for rows , but if you want search in special method like bubble, you need more... because you do search for sort first element with one loop, then repeat it for remaining elements .
27th Jul 2016, 10:02 AM
Mohammad Nemati
Mohammad Nemati - avatar
+ 1
When you have two loops say i being outermost loop and j being an innermost loop, first the value of i is set and j is incremented for all possible values according to the condition you specify. Then i is incremented and set and all values of j is seen.
27th Jul 2016, 7:24 AM
chaitanya guruprasad
chaitanya guruprasad - avatar