Results 1 to 11 of 11
- 04-26-2012, 11:17 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
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
- 04-26-2012, 11:19 PM #2
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
- 04-27-2012, 12:31 AM #3
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
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.
- 04-27-2012, 01:38 AM #4
Senior Member
- Join Date
- Apr 2012
- Location
- New York State of Confusion, USA
- Posts
- 137
- Blog Entries
- 1
- Rep Power
- 0
Re: Pls help "For Loop" Statement example
- 04-27-2012, 01:43 AM #5
Re: Pls help "For Loop" Statement example
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.how the outcome is calculated,
For example: System.out.println("x=" + x);If you don't understand my response, don't ignore it, ask a question.
- 04-27-2012, 01:53 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
- 04-27-2012, 01:55 AM #7
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
- 04-27-2012, 02:00 AM #8
Re: Pls help "For Loop" Statement example
If you print out what is calculated you will see what the code is doing and how the calculations are done.problem is the calculation.
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.but how does "y" act?If you don't understand my response, don't ignore it, ask a question.
- 04-27-2012, 10:00 AM #9
Senior Member
- Join Date
- Aug 2011
- Posts
- 116
- Rep Power
- 0
Re: Pls help "For Loop" Statement example
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.
- 04-27-2012, 04:59 PM #10
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
- 04-27-2012, 05:00 PM #11
Member
- Join Date
- Apr 2012
- Posts
- 6
- Rep Power
- 0
Similar Threads
-
My for-loop is supposedly not a "statement".
By exeye0h in forum New To JavaReplies: 9Last Post: 02-09-2012, 03:46 AM -
loop "play again" in an 8 ball game , loops but wont let me answer my "out.print"
By IareSmart in forum New To JavaReplies: 1Last Post: 02-01-2012, 08:37 PM -
An "if" statement inside a "for" loop?
By soccermiles in forum New To JavaReplies: 18Last Post: 04-20-2010, 03:44 AM -
[SOLVED] Why does the compiler return "not a statement" for this method body please?
By trueblue in forum New To JavaReplies: 3Last Post: 05-25-2009, 08:50 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote


Bookmarks