Results 1 to 4 of 4
  1. #1
    rjagan's Avatar
    rjagan is offline Member
    Join Date
    Feb 2011
    Location
    Puducherry
    Posts
    53
    Rep Power
    0

    Post how to stop for loop

    Hi friends,

    i have developed a gui by using netbeans..

    i have one text box, based upon the text box value the for loop will be executed..

    if i press the start button the for loop will execute..

    for(i=0;i<textboxvalue;i++
    {
    --- some codings---
    }
    i entered 1000 or 10000 it will take more time to finish the loop
    I want to stop the process in between when i press the stop button

    send the code friends for stop the process. ?
    only the process is stop. i dont want to close the gui window..

  2. #2
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Use
    Java Code:
    break;
    to stop any loop
    Last edited by Solarsonic; 04-01-2011 at 01:21 PM.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can also set the counter to no longer meet the condition.

  4. #4
    Vase is offline Member
    Join Date
    Jan 2011
    Location
    Gainesville, FL
    Posts
    45
    Rep Power
    0

    Default

    Loops with sentinel values should really be while or do-while loops, not for loops.

    Java Code:
    while (!stop) {
       // process loop
    }
    However, maybe try this:

    Java Code:
    for (int i = 0; i >= 0; i++) {
       if (!stop)
          do something
       else
          i = -2;
    }

Similar Threads

  1. help!, while loop wont stop!
    By crank6015 in forum New To Java
    Replies: 4
    Last Post: 03-24-2011, 03:27 AM
  2. The outer loop doesnt stop running
    By Bulelakes in forum JDBC
    Replies: 16
    Last Post: 08-23-2010, 10:42 AM
  3. Use stop button to stop moving (stop timers) on JPanel
    By mneskovic in forum New To Java
    Replies: 3
    Last Post: 07-23-2010, 01:50 PM
  4. How to stop SwingWorker?
    By JStarter in forum AWT / Swing
    Replies: 14
    Last Post: 07-20-2010, 05:36 PM
  5. Can you stop a gif? xd
    By Exhonour in forum New To Java
    Replies: 0
    Last Post: 01-16-2009, 09:44 PM

Posting Permissions

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