# For Loops

• 09-27-2011, 06:02 PM
Shyamz1
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:

Code:

``` 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
• 09-27-2011, 06:16 PM
Norm
Re: For Loops
Quote:

help on correcting this problem
Can you explain what the problem is?
Post any output and explain what is wrong with it and show what it should be.

Have you worked out the problem on a piece of paper doing the math one operation on each line to see what the pattern is that you want in the loop?
• 09-27-2011, 06:22 PM
KevinWorkman
Re: For Loops

Crossposted: For Loops
• 09-27-2011, 10:36 PM
Shyamz1
Re: For Loops
I have explained it. All I want it to do is compute the answer. So If provide the arguments for e.g. 2 3 then it should be 2 to the power of 3 and should give an answer of 8. I just dont understand the for loop properly. Can someone explain what happens. The program is wrong it doesn't work.

Kind regards

Shyam
• 09-27-2011, 10:41 PM
Norm
Re: For Loops
Have you worked out the problem on a piece of paper doing the math one operation on each line to see what the pattern is that you want in the loop?
What are the first two numbers to multiply
What are the next two
etc until done.