# For loop with equation?

• January 31st, 2013, 06:51 PM
UPYoda
For loop with equation?
Alright, so I received an assignment from my teacher to make a program that uses if and for loops to allow the reader to pick to calculate the circumference and area of a circle. The teacher said it needs to list the values with the radius increasing in a for loop from 1 to 20. I have only gotten so far as to create the circle with one radius because the looping is not working (supposedly because the radius has to be plugged into an equation). I have tried to switch some code around and add more, however none of my fixes seem to make much difference. I have hit a brick wall so could somebody please help me?

Code Java:

```// *****: This is program lists 20 circumfrence values of a circle and 20 area values of a circle. Comments are in green text. // Syntax error = typing error - Does not compile // Logical error flaw is your reasoning they do compile. // Runtime error does compile but the program crashes when it runs. (Divide by zero) // Import some code   import TerminalIO.KeyboardReader; //This is a piece of code to read characters from the keyboard. import java.text.DecimalFormat; //This piece of code is imported so the twoDecimal value will work.   class Circle {//This class name matches the file name.     public static void main(String args[]) { //Must have this line in all programs.       // decimals = double     KeyboardReader reader = new KeyboardReader();// Declares an object called reader that takes the form of Keyboard reader.   DecimalFormat twoDecimal = new DecimalFormat("0.00"); //Declares how many decimals that is created.   int i;   double circumference, area, pi, circletype;   System.out.println("Enter the value of Pi.");   pi = reader.readDouble(); //Finds pi   System.out.println("To find the circumfrence of a circle type 1. To find the area of a circle type 2.");   circletype = reader.readDouble(); //Reader decides to find the area or the circumfrence   if(circletype == 1){   for (i=1; i<=20; i=i+1); //Loop   circumference = (2 * pi * i);   System.out.println("The circumference of a circle with a radius from 1 to 20 is, "+ circumference); //Loop of 20 different values in increasing order   }       } }```
• January 31st, 2013, 07:30 PM
pbrockway2
Re: For loop with equation?
Use braces with the for loop. And remove the semicolon at the end of the line with "for" in it. Ie all your for loops should look like

Code :

```for(/* etc */) { // code here }```

If your code is not giving the output you expect or intend, say what it *does* do and under what conditions.
• January 31st, 2013, 07:40 PM
UPYoda
Re: For loop with equation?
Wow thanks I hadn't realized how small the error was. Thanks!
• January 31st, 2013, 08:34 PM
pbrockway2
Re: For loop with equation?
You're welcome. The semicolon "ends" the for loop which is saying "for <whatever> do nothing". Then the other statements execute outside the loop.