Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By fakepics500

Thread: How to make the program run again and again?

  1. #1
    Join Date
    Nov 2010
    Posts
    18
    Rep Power
    0

    Default How to make the program run again and again?

    i want to repeat the program
    for example

    if i calculate
    num1=25
    num2=14
    then i got a answer 39 in java
    but then the program ends how do i recall the program without?
    a End???

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Whenever you want code to repeat, automatically think of using a loop. If you know in advance that you want to repeat x number of times, then use a for loop. If you won't know before the loop begins how many times to loop, then use a while loop.

  3. #3
    fakepics500 is offline Member
    Join Date
    Jul 2011
    Posts
    43
    Rep Power
    0

    Default

    use a loop i.e for loop, while loop, do-while loop

    while is entry controlled loop i.e it checks condition first before entry the loop
    Java Code:
    while(condition)
    {
    
    }
    do while is exit controlled loop i.e condtion is checked at last
    Java Code:
    do
    {
    
    
    }while(condition);
    i prefer do while as it works at least for once if condtion is false also
    sunde887 likes this.

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

    Default

    I'd like to point out that there are times where a do while is unsuitable, but nonetheless I agree with your statement that do whiles are quite helpful at times.

    Too many times have I seen people here with code that does
    Java Code:
    doSomething()
    while(condition)
      doSomething()
    end while
    Unnecessarily setting up the code in advance. Many times you can use a while loop as well to force something, it's a common practice when reading from a file to do something like this

    Java Code:
    String s;
    while((s = file.readLine())!= null)
      process s

Similar Threads

  1. Replies: 3
    Last Post: 11-26-2010, 04:08 PM
  2. How to make autorun program
    By arshadalisoomro in forum New To Java
    Replies: 1
    Last Post: 11-04-2010, 09:09 PM
  3. Make a program notify
    By McChill in forum New To Java
    Replies: 7
    Last Post: 06-03-2009, 10:17 PM
  4. Trying to make a GUI program
    By GettinGwap in forum New To Java
    Replies: 2
    Last Post: 11-05-2008, 12:37 AM
  5. i need help to make this program
    By masaka in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 02:26 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
  •