• Post Reply Bookmark Topic Watch Topic
  • New Topic

Confusing Loop Output (Please, explain)  RSS feed

 
Murphy Collins
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
The following code works fine but I am confused at the while loop and in the following e--; statement.

// Compute integer powers of 2.


Why is the e--; statement written at the end of the while loop and not an e++;? And also, how does the whole squaring
occur in the program?

By the initialization of the result variable in the for loop to 1, it becomes a constant with a repeating value of 1 all through
it's execution in the for-loop so how does the while loop now affect the squaring to the power of 2 of the result variable with both result *= 2; and the e--; statement(s) in the while loop.

I am confused. Please, help!

Blessings,
Murphy

PS: e--; is same thing as e = e - 1; and result *=2; is same thing as result = result * 2; whereby e++; is same thing as e = e +1;
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Murphy Collins wrote:Why is the e--; statement written at the end of the while loop and not an e++;?

Presumably because that's hoow the designer wanted it. You certainly could do it with a '++', but then the loop condition would have to be different. You could even do it with a for loop.

BTW, I corrected your indenting. See how much better it looks? Please try to remember to indent your code correctly when you post,

Thanks

Winston
 
L Foster
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, @Murphy,

"e--" is decrementing the e variable. Your while-loop test (inside the for-loop) is checking if e is greater-than 0. If it were incremented with ++, your while-loop would never end.

Per the "result" variable, it is being changed. When i, from the outer for-loop is set to 0, result will be 1; the inner while-loop will not be executed at all. On the second iteration, when i has been set to 1, the inner while loop will be executed one time; e starts at 1, and decrements to 0. Result is set to 2. So it goes. 'result' will, as the for-loop iterates, be set to 1, 2, 4, ... etc. Result is being raised to greater and greater powers of 2.

To help your understanding, at some point when you have the time to invest, I would suggest learning an IDE with a debugger. You can step through and see every variable change its value. If you continue as a Java programmer, you will definitely want that sooner or later.
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of

it could have been written as
 
Murphy Collins
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Murphy Collins wrote:Why is the e--; statement written at the end of the while loop and not an e++;?

Presumably because that's hoow the designer wanted it. You certainly could do it with a '++', but then the loop condition would have to be different. You could even do it with a for loop.

BTW, I corrected your indenting. See how much better it looks? Please try to remember to indent your code correctly when you post,

Thanks

Winston


@Winston
Hi Winston,
Thanks a million for your explanation. Please, one more thing. You know logic differs on an individual basis, and that's why I somewhat get
lost and confused sometimes by the logic of different programmers in certain loop constructs. Please, why is there no 3, 5, 7, 9 and so on? I get why there's no
zero for now by your explanation but the absence regarding the why's of the other odd numbers stands as a mystery for now.

And thanks a lot for the indentation and your advice on indenting too. By the way, I do have an IDE (NetBeans) but I seem to write more
code with NotePadd++ Portable as of recent in order to employ my entire virtuoso in the coding industry. I want to see how much I
could perfect my syntax acquisition by employing more and more of hand-coded applications in order to enhance the effectiveness of
my code by so doing dissolving any and all forms of deficiencies contained within them with time in a quest for perfection (at least to become very familiar
with the entire Java syntax).

Blessings,
Murphy Collins
 
Murphy Collins
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:Instead of

it could have been written as


@Carey
Hi Carey,
Thanks for your assistance. I feel so blessed to be around well groomed folks
like you guys here who are so blessed and well-rounded upstairs.

Thanks for the IQ booster.

Smiling,
Murphy
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!