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: For loop not executing as expected - Very simple coding

  1. #1
    Junior Member
    Join Date
    Feb 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default For loop not executing as expected - Very simple coding

     
    package myNewTest;
     
    public class MyNewTest {
     
    	public static void main(String[] args) {
     
    		for (int i=0; i<=5; i++) {
    			System.out.println("Outer for loop: i = " + i);
     
    			while (i<=3) {
    				System.out.println("Inner while loop: i = " + i);
    				i++;
    			}
    		}
    	}
    }

    Answer:

    Outer for loop: i = 0
    Inner while loop: i = 0
    Inner while loop: i = 1
    Inner while loop: i = 2
    Inner while loop: i = 3
    Outer for loop: i = 5

    My question: Once i = 4 control breaks out of the inner while loop and returns to the outer for loop. Why doesn't it print "Outer for loop: i = 4" before printing "Outer for loop: i = 5"?
    Last edited by LyndonS; February 1st, 2021 at 06:30 PM.

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: For loop not executing as expected - Very simple coding

    Why doesn't it print "Outer for loop: i = 4"
    Because the print statement is after the for statement which incremented the value in i. If the print statement were executed before the end of for statement by having the print statement before the end of for loop, then the value of i would be 4.

    Add this after the while loop:
        System.out.println("After while loop: i = " + i);

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: For loop not executing as expected - Very simple coding

     
    for (int i=0; i<=5; i++) {
         System.out.println("Outer for loop: i = " + i);

    i is 4 after it breaks out of the while loop and before it re-enters the above for loop. Since 4 <= 5, I thought the next line of code "System.out.println("Outer for loop: i = " + i)" would be executed before i++ in the code "for (int=0; i<=5; i++)" was iterrated?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: For loop not executing as expected - Very simple coding

    Where in the for loop would the value in i be incremented? If its value is 4 after the while loop, where would it be changed to 5?

    --- Update ---

    If the value of i is 4 on exit from the while loop, where will it be incremented to 5?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: For loop not executing as expected - Very simple coding

    My understanding is for loops work in the following way:

    for (statement 1; statement 2; statement 3) {

    The code block to be executed would appear here

    }

    Statement 1 is executed only one time: It is executed before Statement 2 or Statement 3 are executed and before the code block is executed – the code block is only executed if Statement 2 evaluates to true. Statement 1 simply sets a variable before the loop starts.

    Statement 2 defines the criteria which determines if the code block will be executed. If the criteria is met, the code block is executed before Statement 3 is executed. If the criteria is not met, the code block is not executed.

    Statement 3 is executed after the code block is executed.

    Consequently, my understanding is after the code breaks out of the while loop i = 4 and the following should occur:

    [code=Java]

    for (int i=0; i<=5; i++) {
    System.out.println("Outer for loop: i = " + i);

    [\code]

    4<=5 is true. Therefore, the code block would be executed BEFORE i is iterated again. This would print: Outer for loop: i = 4.
    After the code block is executed, i would then be iterated and equal 5.

    5<=5 is true. Therefore, the code block would be executed BEFORE i is iterated again. This would print: Outer for loop: i = 5.
    After the code block is executed, i would then be iterated and equal 6.

    6<=5 is false causing the code to exit the program.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: For loop not executing as expected - Very simple coding

    If the value of i is 4 on exit from the while loop, where will it be incremented to 5?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Feb 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: For loop not executing as expected - Very simple coding

    My understanding of how this code should work is as follows:

    After i is iterated to 4 at the bottom of the inner while loop, it then evaluates statement 2 of the inner while loop. Since 4<=3 is false, it then exits the inner while loop and returns control back to the outer for loop.

    i = 4 as it now enters the for loop for the second time. It then evaluates statement 2 of the outer for loop. Since 4<=5 is true, it then should execute any statements within the curly braces/code block. Therefore, it should print: Outer for loop: i = 4.

    AFTER the for code block(s) are completed then it iterates to 5 and evaluates statement 2 of the outer for loop for a third time. Since 5<=5 is true, it then executes any statements within the curly braces/code block. Therefore, it prints: Outer for loop: i = 5.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: For loop not executing as expected - Very simple coding

    Number the the parts of the code:
    1 the for statement
    2 body of code in loop
    3 end of for statement

    i is 4 when execution exits 2
    i is 4 when execution bounces off 3
    i is incremented to 5 at 1
    code prints 5
    i is 5 after 2( while not executed because i > 3
    i is 5 at bounce off 3
    i is incremented to 6 at 1
    i > 5 execution leaves for loop
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Feb 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: For loop not executing as expected - Very simple coding

    Are you telling me when i enters the for loop it is 4 but as soon as it determines 4>=5 it then iterates to 5 - BEFORE performing any other evaluations/actions?

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: For loop not executing as expected - Very simple coding

    When execution iterates back to the top of for statement, the increment section is executed and then the test for termination is done.

    Read the tutorial: https://docs.oracle.com/javase/tutor...bolts/for.html
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following 2 Users Say Thank You to Norm For This Useful Post:

    LyndonS (February 1st, 2021), risen28 (April 26th, 2021)

  12. #11
    Junior Member
    Join Date
    Feb 2021
    Posts
    7
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up Re: For loop not executing as expected - Very simple coding

    Thank you! Thank you! Thank you! I knew that but had forgotten after going through the while statement that it had never iterated the first time. I was concentrating on the inner while loop so much that I forgot.

    You have made my night! Thanks!

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: For loop not executing as expected - Very simple coding

    You are welcome. That was a tough one. I'm glad you have worked it out.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Problem with simple keystroke/executing code
    By lokismile in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 11th, 2018, 03:59 PM
  2. Can someone help me(Simple coding)
    By poldz123 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 18th, 2012, 10:14 PM
  3. Please i need help in a simple coding
    By ookhai in forum Object Oriented Programming
    Replies: 13
    Last Post: July 6th, 2012, 10:19 AM
  4. [SOLVED] Help figuring out why my method isn't executing as expected.
    By Herah in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 8th, 2011, 05:27 PM
  5. i need coding for simple login
    By nag in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: September 29th, 2011, 02:30 PM