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 4 of 4

Thread: For Loops

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default For Loops

    Hi guys. Hope you are well. I'm particularly stuck on understanding for loops properly. I have this program which computes numbers to whatever power you want it to. There are two command line arguments, the first being the number and the second being the power:

     
    public class Power
    {
      public static void main(String [] args)
      {
        int number = Integer.parseInt(args[0]);
        int power = Integer.parseInt(args[1]);
        int answer = 0;
     
        for (int i = 0; i*i < power; i++)
          answer=number*power;
        System.out.println(answer);
     
      }
    }

    Can anyone help on correcting this problem and possibly explaining how the for loop works, because i'm so confused :S

    Kind regards

    Shyam


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: For Loops

    Why don't you add a print statement inside the for loop? Print out each value, as well as answer's current value each iteration. Or better yet, step through it with a debugger.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: For Loops

    Nevermind, looks like I'm wasting my time here.

    This thread has been cross posted here:

    http://www.java-forums.org/new-java/49022-loops.html

    Although cross posting is allowed, for everyone's benefit, please read:

    Java Programming Forums Cross Posting Rules

    The Problems With Cross Posting

    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  4. #4
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: For Loops

    Read books to learn how to use "for loop".....

    Try to manipulate your answer on book.
    Like i=0;i<power;i++
    Calculating :- answer=number*power; suppose number is 2 and power is 5
    for loop condition is satisfied
    answer= 2*5 (= 10); after this increment i=1;
    answer=10*5 (=50).....ans so on

    actual answer or For loop should be
    answer=(your number)(assign your number to answer);
    for(int i=1;i<power;i++)
    answer=answer*number;


    print answer ;

Similar Threads

  1. For loops
    By JCTexas in forum Loops & Control Statements
    Replies: 4
    Last Post: September 21st, 2011, 05:43 PM
  2. help with loops
    By kidza12 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 26th, 2011, 11:42 PM
  3. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM
  4. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM
  5. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM