• 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

why this code prints 5678?

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
could anyone explain why followng code prints 5678?

public class Loop {
public static void main (String [ ] args) {
for ( int i = 10 ; i>5 ; i--) {
int j = i/2 ;
do {
System.out.print ( j ) ;
} while ( j++ < i--) ;
}
}
}
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


could anyone explain why followng code prints 5678?



The easiest way to understand something like this, is to "walk" through the code -- pretend that you are the computer and execute the code.

Anyway, lets cleanup and comment the code first...



1: Outer for loop -- 1st iteration. Begins as i = 10.

1.1: Inner do loop -- 1st iteration. Begins with i = 10 and j = 5. Prints j which equals 5. Restarts loop since (5 < 10). Ends with i = 9 and j = 6.

1.2: Inner do loop -- 2nd iteration. Begins with i = 9 and j = 6. Prints j which equals 6. Restarts loop since (6 < 9). Ends with i = 8 and j = 7.

1.3: Inner do loop -- 3rd iteration. Begins with i = 8 and j = 7. Prints j which equals 7. Restarts loop since (7 < 8). Ends with i = 7 and j = 8.

1.4: Inner do loop -- 4th iteration. Begins with i = 7 and j = 8. Prints j which equals 8. Ends loop since (8 < 7) is false. Ends with i = 6 and j = 9.

1: Outer for loop -- 1st iteration. Ends from inner loop with i = 6. Ends with i = 5. Ends loop since (5 > 5) is false.

Done
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I do not know that you do not understand where
may be the difference between do {statement }while(exprssion )
and while(expression){statement}
in one word
if do{statement} while(exprssion) the expression is evaluated after the statement is executed. While the expression is true, the statement is executed repeatedly.
if while(exprssion ){statement } exprssion first and then statement executing

this is steps of this program executing
i j
10 5
9 6
8 7
7 8
after it while (j--<i++) will executed one time ,but the exprssion is false
hence do {}while() ends and now i=6;j=9
executed for (initialization-expression;loop-expression;update-expression)
statement
update-expression first hence i--, now i=5
then executed loop-exprssion i>5,hence loop-exprssion is false ,so for loops ends


if you change your code like this

you will get some different result
 
M Mistroni
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
silly me,
i was thinking about it yesterday evening and i figured out that in walking thru the code i completely forgot the do..while within the for...
now it make more sense....

thx and regards
marco
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic