• 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

java Modulus

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey all,

I want to print some message after messageCount(which is incrementing) crosses frequency value say 10;
following works


Can this be done in more effective way ?
 
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Satyam, not sure if the functionality will suit your needs but there is a way to slightly optimize it.
What is happening with your code is that while loop is getting executed 105 times and for each iteration the "if" condition is being evaluated. Unless you have logic to execute in between the frequency, the following code will reduce the iterations. The difference may be more prominent for large iterations.



Hope this answers your question
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I doubt it. What Satyam probably omitted was some code that is executed for each iteration. A nested loop may help though:
You'll now get a line printed for 10, 20, 30, ..., 90, 100 and finally 105.
Don't forget to add that j < runs or you will have 5 iterations too many. I've turned the 105 into a final int to make sure that i and j have the same upper bound.


Satyam, I honestly would keep your current code with one modification: it should be messageCount % frequency == 0, or you will get lines printed at 11, 21, etc and not at 10, 20, etc. If I do something like this my guard is similar:
After all, messageCount % frequency is 0 if messageCount is 0 (a case where you don't want to have a line printed) or a non-0 multitude of frequency (a case where you do want to have a line printed).
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Satyam, if you are indeed executing some logic for each iteration too , he is a much simpler and cleaner way

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As Rob has pointed out, you will print something out for 0 messages. Maybe you should start from 1

for (int i = 1; i <= 105; i++) . . .
 
Pranav Raulkar
Ranch Hand
Posts: 73
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:As Rob has pointed out, you will print something out for 0 messages. Maybe you should start from 1

for (int i = 1; i <= 105; i++) . . .



correct
 
satyam pat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for all replies.

May be I was not very clear in my question.

while loop and increment part was just simulation for the problem. In the real time message count will be read from database.

so the actual logic was
if (messageCount >= frequency && (messageCount % frequency == 1))
System.out.println("sent message for " + messageCount);

I was looking for way to avoid checking (messageCount >= frequency) every time.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there some reason you want to take it out? It seems like it is necessary to have it (so you don't print out data the first time around).

Why do you want to get rid of it? What harm is it doing?
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and remember a > test is very fast. You can use > rather than >=, which will look neater, but will probably not affect the performance. The > is faster than the % operator.
 
satyam pat
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
my need/requirement changed to send message on first occurrence and then after subsequent frequencies.

which made is really simple to check
if (messageCount % frequency == 1)

btw thanks for the info regarding performance of '>' and '>='
 
reply
    Bookmark Topic Watch Topic
  • New Topic