Results 1 to 4 of 4
  1. #1
    monster10888 is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Trying to stop a 'for' loop

    Hey guys I'm new here, but I feel like I will be visiting a lot in the next while.

    Any ways, I have to take a program in a 'while' loop and make it into a 'for' loop.

    First off I'm using CMap (I realize it's kind of outdated, but it's an entry level engineering programming class), and the program I am given is
    Java Code:
    main()
    {
    E = 1; N = 0;
    print(^" N E ");
    do
    {
    E = E/2; N = N+1;
    // Search CMAP-Help for "Format-string" on how to format output values
    print(^, "%3.0f", N, "%25.16e", E);
    } while (1+E/2 != 1);
    }
    And what I currently have is
    Java Code:
    main()
    {
    	print("   N           E");
    	N = 0;
    	for( E = 1; E = E/2; 1+E/2 != 1)
    	{	N = N+1;
    		print(^, "%3.0f", N, "%25.16e", E);
    	}
    }
    So the N will count up, as well as the E, but I am unsure how to stop it at a certain value. I tried 'break' and 'if' statements but nothing worked.
    Any help is appreciated. Thanks.

  2. #2
    PhQ's Avatar
    PhQ
    PhQ is offline Senior Member
    Join Date
    Mar 2010
    Location
    Lithuania
    Posts
    358
    Rep Power
    5

    Default Re: Trying to stop a 'for' loop

    Umm... This is a java forum.
    Read this to find out more about for loops in java : The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

  3. #3
    penguinCoder is offline Member
    Join Date
    Oct 2012
    Location
    Tempe, Arizona
    Posts
    77
    Blog Entries
    12
    Rep Power
    0

    Default Re: Trying to stop a 'for' loop

    A for loop:
    Java Code:
    for([initializer]; [condition]; [increment])
    The problem is how you have your for loop setup
    Java Code:
    for( E = 1; E = E/2; 1+E/2 != 1)
    What are you trying to do here?..
    This is saying at the beginning of the loop, E=1. Stop the loop when [I don't know how to represent this]. And after each iteration of the loop execute 1+E/2 !=2.
    When you compare something, you have to use the '==', when you assign a value to something, you use the '='. So your condidtion is actually assigning the value of E/2 to E.


    What language is this? Because some of why I stated might be wrong depending on the language, though I think for loops are pretty universal.

  4. #4
    monster10888 is offline Member
    Join Date
    Oct 2012
    Posts
    2
    Rep Power
    0

    Default Re: Trying to stop a 'for' loop

    Thanks, your post helped and I figured it out. Just seemed to be a matter of understand it.

    Also this is I guess like java code, it looks to me java scaled down. I don't know any better way to put it other than it is the dumb version of Java. It follows all the Java rules but leaves out the bulky stuff I guess you can say.
    I can easily translate any Java code into the one I am using, just have to rid of some works that the program won't pick up.

    Thanks for the help again!

Similar Threads

  1. break or stop for loop
    By mitra in forum New To Java
    Replies: 7
    Last Post: 09-13-2011, 12:39 AM
  2. how to stop for loop
    By rjagan in forum New To Java
    Replies: 3
    Last Post: 04-01-2011, 05:15 PM
  3. help!, while loop wont stop!
    By crank6015 in forum New To Java
    Replies: 4
    Last Post: 03-24-2011, 03:27 AM
  4. The outer loop doesnt stop running
    By Bulelakes in forum JDBC
    Replies: 16
    Last Post: 08-23-2010, 10:42 AM
  5. 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

Posting Permissions

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