The Artima Developer Community
Sponsored Link

Java Answers Forum
a for loop that needs explaing

1 reply on 1 page. Most recent reply: Nov 15, 2002 2:58 PM by twisty

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
ben

Posts: 57
Nickname: loftty
Registered: Oct, 2002

a for loop that needs explaing Posted: Nov 15, 2002 8:00 AM
Reply to this message Reply
Advertisement
hello i just wanted to know what actully is going on in this code where it says (i%10 == 0) i know it reads in every 10th word but how could someone please explain please
thanks ben


for(int i = 0;e.hasMoreElements();i++) {
o = e.nextElement();
if (i%10 == 0){
System.out.println(o.toString());


twisty

Posts: 22
Nickname: twisty
Registered: Nov, 2002

Re: a for loop that needs explaing Posted: Nov 15, 2002 2:58 PM
Reply to this message Reply
The % symbol means modulus, which gives you the remainder after division. For example, 10 / 3 = 3 with a remainder of 1. Modulus does the same thing as division, except it returns the remainder. So, for that same example, 10 % 3 gives you 1.

Thus, in your for loop example, using modulus in this way will ensure that only every 10th word is picked up.

Flat View: This topic has 1 reply on 1 page
Topic: Help please Previous Topic   Next Topic Topic: Help! reading text files!!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use