Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Difficulty understanding code with for loop?

  1. #1
    Junior Member
    Join Date
    Mar 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Difficulty understanding code with for loop?

     
    import java.util.Scanner; 
     
    public class Multiples 
    {     
     
         public static void main (String [] args) 
         {    
     
             final int PER_LINE = 5; 
             int value, limit, mult, count = 0; 
     
             Scanner scan = new Scanner ( System.in); 
     
             System.out.print(" Enter a positive value: " ); 
             value = scan.nextInt(); 
     
             System.out.print( " Enter an upper limit: " ); 
             limit = scan.nextInt(); 
     
             System.out.println (); 
             System.out.println(" The multiples of " + value + " between " + value + " and " + limit + " ( inclusive ) are: " ); 
     
             for ( mult = value; mult <= limit; mult += value)
             { 
     
                System.out.println( mult + "\t" ); 
                count++; 
                if ( count % PER_LINE ==0 ) 
                    System.out.print(); 
              }
          } 
     
    }


    So I'm just really confused about how the for loop works in this program, my book says " the increment portion of the for loop in the multiples program adds the value entered by the user after each iteration. The number of values printed per line is controlled by counting the values printed and then moving to the next line when ever count is evenly divisible by the PER_LINE constant. "


    so in the initiation part of the for loop, mult = value, so if the value entered in was 7, with limit being entered in at 400, then mult = 7, in which case by " mult += value", it would be 14 right? if this is true, how does the program keep adding on values to where the output produces multiples of 7?? Sorry i'm just really lost here lol


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Difficulty understanding code with for loop?

    mult += 7 is executed after each time the loop completes. mult = 7 the first time through, then mult += 7, so mult = 14 the second time through, then mult += 7, so mult = 21 the third time through, etc.

    Make sense?

Similar Threads

  1. Understanding methods in my example code
    By bodylojohn in forum Java Theory & Questions
    Replies: 7
    Last Post: November 19th, 2013, 04:38 AM
  2. difficulty with this code
    By ch2207 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: July 4th, 2013, 08:21 AM
  3. Need help understanding this code!
    By alex067 in forum Java Theory & Questions
    Replies: 1
    Last Post: October 23rd, 2012, 01:14 AM
  4. Difficulty with understanding interaction between Classes
    By JBRPG in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 14th, 2012, 07:16 AM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM