+ 18

Easiest way to Solve loops?

I am good at programming concept but when I see a loop I have to spend time for understanding loop. Is there any faster way to solve this problem

5th Mar 2017, 11:38 AM
Ravindra Sisodia
Ravindra Sisodia - avatar
30 Réponses
+ 19
I usually make flowcharts to understand it... However if there are simple loops, I try to make everything into my mind. I do that to train myself, even if I find it hard.
12th Mar 2017, 10:14 PM
Gami
Gami - avatar
+ 18
I try to understand a loop by reading it from inner to outer. First the statements in the inner loop. Then I check how often that statement is executed by understanding the loop conditions. Then one level up and check statements in the nesting loop if there are any. Then conditions of the nesting loop and so on. Sometimes it is enough to find out how often inner loop and nesting loops are iterated to understand what trey are doing. The resulting number of iteration is then (iter inner)*(iter outer). Sorry I can't tell any cheats, it always depends on the loops, if there is an easier way.
5th Mar 2017, 12:00 PM
Tashi N
Tashi N - avatar
+ 11
I am going to assume you mean for loops since ppl generally have a hard time with those. As far as understanding the process of the loop, it's as follows: 1. variable initialization 2. condition 3. body 4. increment/ decrement 5. back to step 2 until loops ends continue will skip the body execution of that instance break will exit the entire loop with nested loops, inner most loop does complete cycle per # of iterations of outer most loop O(N)^2
8th Mar 2017, 12:36 AM
Elfren Authorlee
Elfren Authorlee - avatar
+ 11
just have a look at the condition and the inner code then it will be easy to understand the code. It will also reduce the time taken by you in understanding the whole loop.
13th Mar 2017, 10:09 AM
Safiya Yunus
Safiya Yunus - avatar
+ 8
It's just a matter of practice
8th Mar 2017, 5:18 PM
keivan Ipchi
keivan Ipchi - avatar
+ 7
try to read statements one at a time
7th Mar 2017, 10:03 AM
Karuma Tendai
Karuma Tendai - avatar
+ 7
Practice makes a man perfect... practice more and more for it
10th Mar 2017, 6:31 AM
Shiv Mishra🎸
Shiv Mishra🎸 - avatar
+ 6
understand the algorithm and practice more loops .....in the beginning you would require a practical approach and would take more time but then you could understand the working of loops faster and easily
8th Mar 2017, 8:22 AM
Yash Raghava
Yash Raghava - avatar
+ 6
Last resort : stick to using it by examples etc and you will eventually understand it !
11th Mar 2017, 5:47 AM
Abdur-Rahmaan Janhangeer
Abdur-Rahmaan Janhangeer - avatar
+ 5
There isn't because you need to understand how is it going to work at each step with increments or decrements.
5th Mar 2017, 12:10 PM
Fahad
Fahad - avatar
+ 5
Just make some points like 1. Conditions for loop to come out. 2. How many times your loop should run 3. Variable that checks the loop conditions 4. How many !oops you require 5. whether loop inside the loop makes the task simple such points will help you solve loop. All the best.
6th Mar 2017, 3:48 PM
Prasannakumar naik
Prasannakumar naik - avatar
+ 5
you have to fully concentrate on the given condition.
7th Mar 2017, 3:22 PM
Ateeq Ur rahman
Ateeq Ur rahman - avatar
+ 5
Let's take a example: 1 2 2 To generate this output, take it as problem statement, make a flowchart which will help in understanding better. Then write a program with loops. Consider distribute variables as primary and secondary or say inner and outer loop variables. In this case we can see there is number of integers increase as line number increases. Also as line number increases value of Integers are also increasing. So we consider outer loop variable as line number and remaining variables as inner loop variables. Similarly each problem can be solved easily by using flowchart and using this method.
9th Mar 2017, 6:25 AM
TVS Harish Naidu
TVS Harish Naidu - avatar
+ 5
while(!understand) {spend time practicing over different examples}
12th Mar 2017, 11:24 PM
Alessio Meloni
Alessio Meloni - avatar
+ 4
see online lectures about loop
8th Mar 2017, 3:59 AM
Haroon Mahmood
Haroon Mahmood - avatar
+ 4
Loop is very easy. When we execute some specific lines of code for many times. We use loop.
11th Mar 2017, 5:02 PM
Haroon Mahmood
Haroon Mahmood - avatar
+ 3
like in patterns programming with loops...try to understand what controls what..is it rows values controlling the columns values or vice versa..always read them inside out for the innermost is controlled by the one before it and the level rises. finish following the execution of the inner first then jump to its follower which is usually it's controller.
5th Mar 2017, 10:03 PM
Michael Jaroya
Michael Jaroya - avatar
+ 3
The way I used is to keep the track of the variables for few iterations to understand the pattern, it helps to get the intuition what the loop really wants to accomplish.
10th Mar 2017, 12:03 PM
Bhargav Upadhyay
Bhargav Upadhyay - avatar
+ 3
easy way to understand loop is to check every thing one by one and move slowly with proper understanding... and try to modify any simple loop question into difficult one
11th Mar 2017, 8:43 AM
Usman Bhatti
Usman Bhatti - avatar
+ 3
Taking an example of few iterations is a very simple but very effective way.. This will help us know how and the no. of times the loop is working. This will enable us to write the code for loops to get the desired results.. All the best...!!!
11th Mar 2017, 9:52 AM
Shubham Agrawal
Shubham Agrawal - avatar