• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Ways to evaluate loops

 
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following code will output Hi six times


What is the quick formula(s) way for evaluating no. of times a loop will be processed without actually evaluating the loop? Example for above

is it 10-5 + 1= 6



If you actually trace through the code you will see Hi is printed six times.

I thought there was a quick way to do this?
Just like to show my classes different ways to evaluate loops
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi James,
The formula will look some thing like this

(Final - Initial) / Step

Where Final is the final value.
Initial is the initial value and
Step is the increment value.

for <= condition we have to increment Final by one.

In idle condition (i.e. without any conditional break or jump etc.)the loop will execute these many times.

Regards,
Kalai Selvan T.
 
James Chegwidden
Author
Posts: 201
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Formula not quite right:


prints 25 and 1

Formula given above (25- 1)/1 =24 times which is not correct.

So formula does not work for all cases.
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the quick formula(s) way for evaluating no. of times a loop will be processed without actually evaluating the loop?
I suggest you to read some books which contain the algorithm analysis.
From your example, I know that it is required 0(n) which n representing the times of the for loop.
Hope this help
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I may be way off here but wouldn't (x+1)-y = z work? This of course would only work if the ending value is greater then the starting value, and it is a >= statement. Is this just an general thought? Or are you looking for code for this too?
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic