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

Thread: help with for loop

  1. #1
    Junior Member
    Join Date
    Jan 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default help with for loop

    Hi, I work in a lab that uses Java to control a light array. I have very little experience with Java. We're currently using the script below that runs for 6 hours. I have been trying to extend the run time to 12 hours. I tried changing the condition to " i < 1440 " and it will not compile. It says "Compiling.... Infinite loop in code." Ultimately, I would like it to loop for 12hrs then keep the light intensity at "0" for 12hrs and repeat that indefinitely. Can anyone help? Thanks in advance.


    /**
    * License header here
    */

    /**
    * New Script
    */

    light=390
    dark=0
    time="180s"
    limit = 3000;

    for (var i = 0; i < 720; i++){
    set_intensity(54)
    wait("1s")
    set_intensity(107)
    wait("1s")
    set_intensity(255)
    wait("1s")
    set_intensity(2000)
    wait("2s")
    set_intensity(255)
    wait("1s")
    set_intensity(107)
    wait("1s")
    set_intensity(54)
    wait("1s")
    set_intensity(0)
    wait("22s")
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: help with for loop

    What does the value 720 represent? Instead of hardcoding a value, use a variable with a proper name that is given a value using an expression. For example:
    int timeToRunInSec = 12*60*60; // number of seconds in 12 hours with 60 min/hr, 60 sec/min

    How long does it take for the code inside of the loop to execute? Say 30 seconds per iteration
    Then for the loop to execute for 12 hours takes this number of iterations:
    number of seconds in 12 hours / 30 sec per iteration

    Please copy the exact text of any compiler error message and paste it here.
    Also post the code that caused the errors.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Jun 2022
    Posts
    41
    Thanks
    1
    Thanked 3 Times in 2 Posts

    Default Re: help with for loop

    Also, the value in set_intensity(255) suggests an 8-bit integer with a range of 256 [0..255], or some larger multiple thereof. But there's also an entry for set_intensity(2000)
    If 2000 were a reasonable value, then it would follow that 250 would be a reasonable fraction of it.

    Valid range of values is something that can be added into comment blocks, which is then read and displayed in IDE mouse-over windows
      /**
       * Set light level
       * @param intensity [0..255]
       */
      public static void set_intensity(int intensity){
        // code
      }
    Screenshot
    Last edited by AngleWyrm; January 6th, 2023 at 08:58 PM.

  4. #4
    Junior Member
    Join Date
    Jan 2023
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help with for loop

    @Norm: I believe 720 is part of the condition. As long as the variable " i " is less than 720, the script will loop (i < 720). Do we just use the timeToRunInSec in place of the condition?

    @AngleWyrm: The intensities are specific light settings that the researchers need the array to be set at for each time point.

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: help with for loop

    Do we just use the timeToRunInSec in place of ...
    In place of the 720.
    Its value would be the number of times you want to loop to iterate.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM