Help coderanch get a
new server
by contributing to the fundraiser
  • 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

Regarding Looping...

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class JMM114 {
public static void main (String[] args) {
int i = 0, j = 0;
while (i++ < 3) do
System.out.print(j);
while (j++ < 3);
}}

i get answer as 121212
Plz explain me how the flow goes...
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anish,
That program prints 12345
Are you missing a do bit of the second do-while?
[ March 03, 2006: Message edited by: Tamara Lopez ]
 
Anish Nagraj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry.... that was a differnt one... the actual one is

class JMM113 {
public static void main (String[] args) {
int i = 0, j = 0, k = 0;
do while (i++ < 3)
System.out.print(k++);
while (j++ < 3);
}}

for which i get 121212...
plz explain
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Your outout is wrong...please check it again its

012345
 
Seema Ahuja
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Anish,

answer to your second problem is worng too...the correct answer is
012

please check again

Thanks
 
Anish Nagraj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am really really sorry...
here is the actual culprit...

class JMM112 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i++ < 2 do
System.out.print(i);
while (j++ < 2);
}}

i am not clear with any of such lopps... which was there in all the previous examples... plz explain me this one...
 
Tamara Lopez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Anish,
a do while look will always do something once, and then repeat it only if the while isnt satisfied. so step by step

class JMM112 {
public static void main (String[] args) {
int j = 0;
for (int i = 0; i++ < 2; ) do
System.out.print(i);
while (j++ < 2);
}}

int j = 0
int i = 0
- enter for loop#1
i is 0 which is < 2. i++ so i is now 1 after comparison (post-suffix)
- do while until j++ < 2
print 1
j is 0 which is < 2. j++ so j is now 1 after comparison
back to do
print 1
j is 1 which is < 2. j++ so j is now 2 after comparison
print 1
j is 2 which is NOT < 2. Let's go back to for loop

- i is 1. 1 < 2. i is 2 after comparison
enter do.
print 2
j is NOT less than 2
back to for loop
- i is 2. 2 < 2 is FALSE. out of for loop
 
Tamara Lopez
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and it prints 1112
is this the bit of code you wanted?
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!

I don't know if you understood why the first output is 012345. The ideia of all examples are the same. Take a look at code:



FIRST STEP: in the first interaction we have the following setting:
==> i=0
==> at line 5 we compare if i is less than 3 (remember i is 0). The answer is true, then i = i+1 (i++) and go to the line 6.
==> then I going to print the j value until j=3!! At instruction do/while we first print the value of j (line 7), then we compare if j is less than 3(line 8), and after we increment j (j++).

Then the output for the first interaction is: j=0 j=1 j=2 j=3



SECOND STEP: in the second interaction we have the following setting:
==> i=1
==> at line 5 we compare if i is less than 3 (remember i is 1). The answer is true, then i = i+1 (i++) and go to the line 6.
==> then we print the j value (j=4). J is gratter than 3, then we increment j (j++) and came back to while(i++<3) at line 5.

Then the output for the second interaction is: j=4



LAST STEP: in the last interaction we have the following setting:
==> i=2
==> at line 5 we compare if i is less than 3 (remember i is 2). The answer is true, then i = i+1 (i++) and go to the line 6.
==> then we print the j value (j=5). J is gratter than 3, then we increment j (j++) and came back to while(i++<3) at line 5.

Then the output for the last interaction is: j=5



Now i=3, then while loop fails, the program ends and output is 01245!!!


Regards,
Marcella Spiropulos
 
Anish Nagraj
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot
 
Thanks tiny ad, for helping me escape the terrible comfort of this chair.
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic