Results 1 to 9 of 9

Thread: For loop

  1. #1

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810

    Angry For loop

    Code:
    import java.math.*;
    class Stuff{
    public static void main(String[] args) {
    int var4;  
    var4 *= (3.14/180); // convert from degrees to radians;
      double sin_var4;
      for (int j = 5; j <= 10; j++){
    	sin_var4 += Math.pow(var4,j);
      }
    System.out.println(sin_var4);
    }}

    Doesn't work... sigh always says sin_var4 0.

    For those of you who might ask what sin_var4 is... (or what I am trying to do).

    I am trying to write the defination of Sine function, which in Maclaurin series is x - x^3/3! + x^5/5! - ... x^n/n! + x^n/n! ... etc

    and also seems like the compiler says sin_var4 is undefined.. why??

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    var4 lacks an initial value. Also it should be a double, as int lacks the precision you need. Use the PI constant from the Math class instead of 3.14, it's more exact. There is no java.math package, Math is in java.lang and therefore doesn't need an import. sin_var4 lacks an initial value too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    Why must I define it to be double? Because PI is infinitely digited? If I use 3.14 I can use float right? Since the range is enough and save space.

    And why must I initialize a default value? Doesn't it have a default value of 0.0 when I define it?

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  4. #4

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    And is there anyway to make the For () statement increase 4 each time instead of 1??

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  5. #5

    Thread Starter
    Fanatic Member prog_tom's Avatar
    Join Date
    May 2001
    Location
    Los Angeles and Little Rock
    Posts
    810
    and say i have some variable x inside a for(){x += 1;} condition, how can I view that x outside the bracket?(i need x because is a sum of all stuff.)

    prog_tom
    JOIN THE REVOLUTION!!!! Dual T3 backedup science community.
    http://physics.sviesoft.com/forum

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by prog_tom
    And why must I initialize a default value? Doesn't it have a default value of 0.0 when I define it?
    Everything in Java is defaulted to null. If you try to use a variable before assigning it an initial value, the compiler should yell at you.

    Its been a while since I played with Java, so I can't tell if what you've done is acceptable or not.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by prog_tom
    And is there anyway to make the For () statement increase 4 each time instead of 1??
    Code:
    for (int j = 5; j <= 10; j+=4){
    	sin_var4 += Math.pow(var4,j);
      }
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  8. #8
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Originally posted by prog_tom
    and say i have some variable x inside a for(){x += 1;} condition, how can I view that x outside the bracket?(i need x because is a sum of all stuff.)
    Declare it outside of the for block.
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Originally posted by prog_tom
    1) Why must I define it to be double? Because PI is infinitely digited? If I use 3.14 I can use float right? Since the range is enough and save space.

    2) And why must I initialize a default value? Doesn't it have a default value of 0.0 when I define it?
    1) Float would probably suffice. There's no datatype with infinite digits (duh!), so it's only a matter of precision. However:
    You currently declare it as int.
    sin_var4 is double, and it's always a good idea to have variables in a calculation have the same type. The repeated conversion from float to double would in the long run take so long that it is worth the 4 additional bytes of the double to save this time. And you get better precision, too.

    2) No, variables are not initialized.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width