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

Thread: Continue and break statements

  1. #1
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Continue and break statements

    Code then description of problem:

    class Weather 
    {
    	public static void main(String[] args) 
    	{
    	// declare variables
    	int i = 1; // represents year
    	double temp = 0.0;
    	final double ANNUAL_INCREASE = 1.0;
    	double uncertainty = 0.0;
    	String result = " ";
    	System.out.println(" Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
     
    	while(true)
    	{
    	if (i > 5) break;
    	else if (i == 3) continue;
    	System.out.println(i);
    	i++;
    	}
     
    	}
    }

    So part of my assignment is to loop though my variable for year (named i) from 1 through 5. Then skip through once "i" has the value of 3, and continue on afterwards. The code above compiles and displays this output:

     Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
    1
    2

    It stops and leaves 3 as an empty blinking line inside of my terminal. Switching the break and continue statements around like this:

    while(true)
    	{
    	if (i == 3) continue;
    	else if (i > 5) break;
    	System.out.println(i);
    	i++;
    	}

    gives me the same result as the first code. It was my understanding that the break statement is essentially a loops stopping point, and continue carries on to the next iteration without terminating the loop.


    This code below gives me the nearly desired result:

    while(true)
    	{
    	if (i > 5) break;
    	System.out.println(i);
    	i++;
    	}

    It prints out like this:

    1
    2
    3
    4
    5

    So that makes me know that the stopping point is at least correct. The confusion comes when trying to skip over once (i == 3).
    The only time i was instructed to use the continue statement was inside of a for loop like this:

    for (int i = 0 ; i <= 5 ; i++)
    {
    	if (i == 3) continue;
    	System.out.println("I = " + i);
    }

    Which gives me the perfect output (1, 2, 4, 5). I'm hoping this is just a syntax thing rather than a misunderstanding on my end for combining a continue/break statement.

    Any help towards the solution is appreciated


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Continue and break statements

    One wonders why you just didn't program it as:
    while( i < 6 )
    {
    	if (i != 3)
    	{
    		System.out.println(i);
    	}
    	i++;
    }

  3. #3
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Continue and break statements

    "Using a while(true) statement, loop over Year i from 1 through 5, EXCEPT i = 3. Use a break statement to exit the loop as appropriate. When i = 3, it should skip the calculations and go the next iteration, so use a continue statement for that."

    I agree, my courses are online and it' s more difficult to follow along i guess, but that's how i was instructed to do it, points get deducted if i don't do the assignment "as written"

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Continue and break statements

    Okay, the requirement wasn't clear.

    In either of your original while( true ) loops, when will the index, i, be incremented?

  5. #5
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Continue and break statements

    Well, after the println, it's what worked in the final while(true) loop. I tried rearranging everything in the loop like this:

    while(true)
    	{
    	System.out.println(i);
    	if (i > 5) break;
    	else if (i == 3) continue;
    	i++;
    	}

    That sends the code into an infinite loop at 3, which isn't ideal especially that what's being printed is the number 3 (the one number i don't want haha). Lastly, in my notes for the continue statement,it seems the continue statement simply skips over to the next part of the code like this:

    for (int i = 0 ; i < = 5 ; i++)
    {
    	if (i == 3) continue;
    	System.out.println("I = " + i);
    }

    "When i == 3, continue on to the next part"

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Continue and break statements

    I think that means you haven't figured it out. The path to a solution is understanding or finding the answer to the question I asked. You seem to have figured it out, at least you have experienced what happens if the continue; statement occurs BEFORE the i++: an infinite while( true ) loop occurs. So, what must you do to ensure the while ( true ) loop doesn't become inescapable when i == 3? There are a number of possible solutions, but they may all be different versions that reorder the statements you have.

    And the if/else construct isn't necessary. I think it's clearer without the if/else, but that may just be me.

  7. #7
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Continue and break statements

    OK, I'll give understanding your post a solid effort before i report back. Just scratching my head how a while(true) statement would be formatted without the if-else statements. I am still quite new, so i over think just about everything.

  8. #8
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Continue and break statements

    OK, i'm really close i think. I got the code to compile and display the desired output, only problem is that in the assignment it requires me to declare a variable of type int, name it i and set it with a value of 1. The only way i could get it to compile and display what i wanted was to set the value of i to 0.

    class Weather 
    {
    	public static void main(String[] args) 
    	{
    	// declare variables
    	int i = 0; // represents year
    	double temp = 0.0;
    	final double ANNUAL_INCREASE = 1.0;
    	double uncertainty = 0.0;
    	String result = " ";
    	System.out.println(" Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
     
    	while(true)
    	{
    	if (i >= 5) break;
    	i++;
    	if (i == 3) continue;
    	System.out.println(i);
    	}
     
    	}
    }

    This code does exactly what i want, but is not written in a way that i was instructed. When i set the value to 1 (as instructed) it prints out 2, 4 and 5. Even if i put the print statement before the if statements it prints 2, and that doesn't make sense since i shouldn't = 2 until after it loops once.

  9. #9
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Continue and break statements

    Are you interpreting the instructions in post #3 as requiring i to be initialized to 1, or do you have other instructions that specifically say i must be initialized to 1? If the former, I don't agree with your interpretation. If the latter, I'll have to think on it some more in the morning.

  10. #10
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Continue and break statements

    Yes, prior instructions stated that i be initialized to 1. I'm just leaving it for the moment, the whole rest of the assignment is a freaking mess as well.

  11. #11
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Continue and break statements

    Then it seems the only way to satisfy all of the requirements you've been given is to add an i++ statement to be executed before the continue statement, like:
    if (i == 3)
    {
    	i++;
    	continue;
    }
    Then you can move the i++ in the while loop to after the print statement so that i can be initialized to 1 for the first pass through the while loop.

  12. The Following User Says Thank You to GregBrannon For This Useful Post:

    ///M Dood (September 22nd, 2013)

  13. #12
    Member
    Join Date
    Jul 2013
    Location
    Mars
    Posts
    50
    My Mood
    Relaxed
    Thanks
    5
    Thanked 1 Time in 1 Post

    Default Re: Continue and break statements

    Here's what i got to work:


    public class Test 
    {
    	public static void main(String[] args) {
    	int i = 1;
     
    	while(true)
    	{
    	if (i > 5) break;
            if (i == 3)
            {
                   i++;
                  continue;
             }
    	System.out.println(i);
    	i++;
    	}
     
     
    	 }
    }

Similar Threads

  1. Break/Continue example doesn't work with ConsoleReader
    By Tedstriker in forum What's Wrong With My Code?
    Replies: 7
    Last Post: September 16th, 2013, 10:15 AM
  2. i'm stack, have no idea how to continue this. Help
    By chingu in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 7th, 2013, 01:55 AM
  3. Help with interrupting a loop without break or continue
    By mwr76 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 11th, 2011, 08:51 PM
  4. continue statements
    By monroe in forum Java Applets
    Replies: 1
    Last Post: March 20th, 2010, 06:26 PM
  5. Break it down
    By [Kyle] in forum Java Theory & Questions
    Replies: 5
    Last Post: September 19th, 2009, 09:04 PM