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

Thread: Code working, but not the way I thought...

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Location
    The frozen NorthEast
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Code working, but not the way I thought...

    I've been playing around with some Java code (not my first language, but I'm trying to wrap my brain around it), but it is not giving me what I expected. I must not be looking at it correctly. I'd appreciate any suggestions on where I am mistaken..

    HTML Code:
    public class Mix5 {
    
       public static void main(String[] args) {
    		
         int x = 0;  //declare int named "x" & set value to 0
         int y = 30; //declare int named "y" & set value to 30
    		
       for(int outer = 0; outer < 3; outer++) { 
        //Outer for loop: declare int named "outer", set value to 0. 
         //while value is less than 3, increment by 1.
    			
          for(int inner = 4; inner > 1; inner--) { 
           //Inner for loop: declare int named "inner", set value to 0.
            //while is greater than 1, decrement by 1.
    				
    	y = y - 2; //y = value of y (30) - 2 (28)
    				
             if(x == 6) { 
                  break;
             } //when x equals 6, break out of loop.
    				
            x = x + 3; //x = value of x(0) + 3 (3)
    	 } //end inner for loop.
    
    	y = y - 2; //y = value of y (28) - 2 (26)
    	 } //end outer for loop.
    
    	System.out.println(x + " " + y); //Print out value of x and y.
    	 } //end main Method.
    } //end Class.
    Here's how I would THINK it would behave:

    Setup:
    x is set to 0 and y is set to 30.
    In outer loop, int outer set to 0, and incremented by 1 until it = 3.
    In inner loop, int inner set to 4, and decremented by 1 until it = 1.
    Both loops should then complete a total of 3 times.

    Action (Loop 1):
    y equals itself (30) - 2 = 28.
    a check is made, if x equals 6 (it doesn't yet), then break.
    x equals itself (0) + 3 = 3.
    y equals itself (now 28) - 2 = 26

    Action (Loop 2):
    y equals itself (26) - 2 = 24.
    a check is made, if x equals 6 (it doesn't yet), then break.
    x equals itself (3) + 3 = 6.
    y equals itself (now 24) - 2 = 22

    Action (Loop 3):
    y equals itself (now 22) - 2 = 20
    a check is made, if x equals 6 (it now does), then break.
    Break out of loops.
    Print a line with the value of x and the value of y. It should be:

    6 20

    However, what I get when I run this is:

    6 14

    Does my if statement only break me out of the inner for loop, not both? This is the only reason I can think that it would continue to process.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Code working, but not the way I thought...

    Quote Originally Posted by JLogan3o13 View Post
    Does my if statement only break me out of the inner for loop, not both? This is the only reason I can think that it would continue to process.
    Bingo. Use a debugger, or at least put in some print statements, to make sure of that.

    The most obvious way to fix this would be to look into labeled statements.

    Branching Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. can' stop working a part of code.plzz help
    By kyros in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 30th, 2010, 03:10 PM
  2. Code Stop working after converting to jar?
    By Ron6566 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 16th, 2010, 12:17 PM
  3. [SOLVED] I thought that this would be right?
    By jwb4291 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: June 29th, 2010, 03:26 PM
  4. Cannot seem to get this working
    By OttawaGuy in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 28th, 2010, 03:41 PM
  5. Replies: 4
    Last Post: January 27th, 2009, 12:03 AM