• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

calculating value in nested loops

 
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there an easy way to calculate the values of nested loops?

I was given the following piece of code, with instructions to calculate the output for each loop:



It is my understanding the at outer loop * inner loop will cause the inner loop to iterate that number of times

Am I just calculating incorrectly or using common core or something equally screwed up?

the outer loop ranges from 2 to 3
the inner loop ranges from 0 to 1

I am being told that the output from the outer loop is 20 and 21,
the inner loop output is 30 and 31.

I don't' see how they got there. What am I missing?

Thanks
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am being told that the output from the outer loop is 20 and 21,
the inner loop output is 30 and 31.


Who is telling you this? The only output statement is inside the inner loop so how can anyone say there's output from the outer loop?

The output of this code is this:

20 21 30 31

The outer loop controls this (bolded) part of the output

20 21 30 31

while the inner loop controls this (bolded) part of the output

20 21 30 31

Note that because you have a String as the first term in the expression that is the argument to System.out.print(), what's occurring is String concatenation, not integer addition. So, the expression "" + 2 + 0 produces a String, "20"
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bill Platt wrote:It is my understanding the at outer loop * inner loop will cause the inner loop to iterate that number of times


Be careful with that...since a loop can use a variable as the limit, it is possible for an inner loop to run a different number of times on each iteration...


The first time through the outer loop, the inner loop won't run at all.  
The next time, the inner loop will run once, then twice...etc.
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also avoid <= if you can. There is a basic format for a for loop, which isAlways start from that basc form, even if you have to change it because you are iterating things slightly differently. If you write for (int i = 0; i < 6; i++) ... everybody will look at that for 1″ and see a loop running 6×, which they won't if you use <=. It will be easier to readthan to use <=.
 
Bill Platt
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Who is telling you this?

The output of this code is this:



This was part of the zyBooks assignment that I was working on. The question was to provide the output of the given
code. The provided answer was as stated above


Junilu Lacar wrote:
Note that because you have a String as the first term in the expression that is the argument to System.out.print(),
what's occurring is String concatenation, not integer addition.



OK, that makes a lot more sense. I was trying to use addition, and the thought of concantenation never occurred to me.

Thanks for your help

Bill
 
Bill Platt
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Also avoid <= if you can.



Doesn't the use of <= or any other operator depend upon each specific situation?

From what I have seen in my limited exposure, it is pretty common to see between
3 and 10 (inclusive) for the parameters of what is needed to be done.

Is there a way to achieve the same results without <= or >= ?


Bill
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bill Platt wrote:. . . Doesn't the use of <= or any other operator depend upon each specific situation?

Yes, but <= is much easier to misread than <, and < is what people expect to see.

. . . between 3 and 10 (inclusive) . . . Bill

But as you know it is bad form to use number literals like that. At least provide an explanation of what the numbers mean in a comment.Most of us are used to starting from 0 and working up to a limit, exclusive. It is much easier to see that the last loop will run 8× than to see that 3...10inclusive means 8×.
 
Bill Platt
Ranch Hand
Posts: 42
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Yes, but <= is much easier to misread than <, and < is what people expect to see.

. . . between 3 and 10 (inclusive) . . . Bill

But as you know it is bad form to use number literals like that. At least provide an explanation of what the numbers mean in a comment.Most of us are used to starting from 0 and working up to a limit, exclusive. It is much easier to see that the last loop will run 8× than to see that 3...10inclusive means 8×.



OK thanks. I will have to remember that.

It's not something that I would have thought about. I'm used to trying to accomplish what is given, not go in a "round about way".
It might explain a little bit about why my head is spinning LOL

thanks again
 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell, what if I were counting the number of fingers on both hands; doesn't this make more sense?

 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is how people count, but computers start counting from 0.In 0‑based numbering, you would have to add 1 somewhere to get the count.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would just note that 0-based indexing is an idiom in Java, not a hard-and-fast rule. I think there are occasional cases where starting from an index other than 0 and using <= is less confusing. Even in this example, where you just want to iterate from 2 to 3, I think it's actually easier to understand i = 2; i <= 3; i++ rather than the arguably more idiomatic i = 2; i < 4; i++.

I think the 0-based idiom is really more applicable and preferred when you're using the loop variable as an index into a zero-based structure like an array or list. I don't like being dogmatic about < vs <= just because people expect the former. Its use has to make sense overall and I think if you're simply trying to go from 2 to 3, then i = 2; i <= 3; i++ is actually more intuitive to read. On the other hand, if you're trying to do something 10 times, then i = 0; i < 10; i++ is more idiomatic and should be preferred.
 
Campbell Ritchie
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
All right; I'll go with that and acknowledge my mistake.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:but computers start counting from 0.


Starting from 0 is idiomatic in Java and other C-like languages but it's not a general rule for all programming languages. Pascal uses 1-based indexing as do a number of other languages: http://xahlee.info/comp/comp_lang_array_index_start_0_or_1.html
 
reply
    Bookmark Topic Watch Topic
  • New Topic