Results 1 to 11 of 11
Like Tree2Likes
  • 1 Post By jlczuk
  • 1 Post By Norm

Thread: Pls help "For Loop" Statement example

  1. #1
    tuana is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Pls help "For Loop" Statement example

    Hi everybody,

    I am new in Java World and I couldn't understand some statements in Java. I couldn't understand the outcome. If you explain that how the outcome is calculated, I' ll appreciate it.

    The outcome is : 54 6

    Java Code:
    public class MixFor5 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
    		int x = 0;
    		int y = 30;
    		for (int outer = 0; outer < 3; outer++) {
    			for (int inner = 4; inner > 1; inner--) {
    				x = x + 3;
    				y = y - 2;
    				if (x == 6) {
    					break;
    				}
    				x = x + 3;
    			}
    			y = y - 2;
    		}
    		System.out.println(x + " " + y);
    
    	}
    
    }
    Last edited by sunde887; 04-26-2012 at 11:36 PM. Reason: Added code tags

  2. #2
    tuana is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Pls help "For Loop" Statement example

    Pls help &quot;For Loop&quot; Statement example-loop.png

  3. #3
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Pls help "For Loop" Statement example

    Its quite simple, how many times do you run through each for loop?
    Each time you run the outer loop you run the inner loop through however many times it needs to do according to your conditions.
    So your inner loop will be running through more times than your outer as for every run through the outer loop, you do a full run through your inner. Im trying my best to explain it without telling you the answer, output is correct though so some simple maths should help get your head around it as well.

  4. #4
    jlczuk is offline Senior Member
    Join Date
    Apr 2012
    Location
    New York State of Confusion, USA
    Posts
    137
    Blog Entries
    1
    Rep Power
    0

    Default Re: Pls help "For Loop" Statement example

    tuana likes this.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Pls help "For Loop" Statement example

    how the outcome is calculated,
    Add println statements to print out the values of variables every time they are changed. The print out will show you the steps the code takes to calculate the output.
    For example: System.out.println("x=" + x);
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    tuana is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Pls help "For Loop" Statement example

    Quote Originally Posted by wdh321 View Post
    Its quite simple, how many times do you run through each for loop?
    Each time you run the outer loop you run the inner loop through however many times it needs to do according to your conditions.
    So your inner loop will be running through more times than your outer as for every run through the outer loop, you do a full run through your inner. Im trying my best to explain it without telling you the answer, output is correct though so some simple maths should help get your head around it as well.
    hi wdh321,

    thank you for your help. According to your explanation, the loop will run 9 times and x will be 6 because of the "break" and then we will multiply 6 and 9 so we can get 54
    but how does "y" act?

  7. #7
    tuana is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Pls help "For Loop" Statement example

    Quote Originally Posted by Norm View Post
    Add println statements to print out the values of variables every time they are changed. The print out will show you the steps the code takes to calculate the output.
    For example: System.out.println("x=" + x);
    Hi Norm,
    Thank you for your interest bur my problem is not for the "System.out.println" statement, my problem is the calculation...

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default Re: Pls help "For Loop" Statement example

    problem is the calculation.
    If you print out what is calculated you will see what the code is doing and how the calculations are done.
    If you do any programming, there will be many times when you will need to debug code. One of the oldest ways of debugging code is by using the println statement.
    but how does "y" act?
    Add some println statements to show how y changes as the code is executed.
    yellowledbet likes this.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    wdh321 is offline Senior Member
    Join Date
    Aug 2011
    Posts
    116
    Rep Power
    0

    Default Re: Pls help "For Loop" Statement example

    Quote Originally Posted by tuana View Post
    hi wdh321,

    thank you for your help. According to your explanation, the loop will run 9 times and x will be 6 because of the "break" and then we will multiply 6 and 9 so we can get 54
    but how does "y" act?
    Well you have y in both your inner and outer loops.
    You already know how many times your inner loop runs, but you also need to consider when y = y-2 will run in your outer loop and how many times.
    Like Norm said, if you put println statements in you will be able to see how y is changing and that may help you understand.

  10. #10
    tuana is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Pls help "For Loop" Statement example

    Quote Originally Posted by Norm View Post
    If you print out what is calculated you will see what the code is doing and how the calculations are done.
    If you do any programming, there will be many times when you will need to debug code. One of the oldest ways of debugging code is by using the println statement.

    Add some println statements to show how y changes as the code is executed.
    Yes, you are absolutely right.. adding println statement is really useful to understand the calculation.
    Thank you..

  11. #11
    tuana is offline Member
    Join Date
    Apr 2012
    Posts
    6
    Rep Power
    0

    Default Re: Pls help "For Loop" Statement example

    Quote Originally Posted by wdh321 View Post
    Well you have y in both your inner and outer loops.
    You already know how many times your inner loop runs, but you also need to consider when y = y-2 will run in your outer loop and how many times.
    Like Norm said, if you put println statements in you will be able to see how y is changing and that may help you understand.
    Thank you so much...

Similar Threads

  1. My for-loop is supposedly not a "statement".
    By exeye0h in forum New To Java
    Replies: 9
    Last Post: 02-09-2012, 03:46 AM
  2. Replies: 1
    Last Post: 02-01-2012, 08:37 PM
  3. An "if" statement inside a "for" loop?
    By soccermiles in forum New To Java
    Replies: 18
    Last Post: 04-20-2010, 03:44 AM
  4. Replies: 3
    Last Post: 05-25-2009, 08:50 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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