aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how is the flow Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "how is the flow" Watch "how is the flow" New topic
Author

how is the flow

Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
hi,
public class Loop{
public static void main(String args[]){
for (int i=10; i>5; i--){
int j=i/2;
do{
System.out.println(j);
}
while(j++< i--);
}
}
}
Could anyone explain how the output is 5678

[This message has been edited by Marilyn deQueiroz (edited November 08, 2001).]


nss
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Originally posted by Neha Sawant:
hi,
public class Loop{
public static void main(String args[]){
for (int i=10; i>5; i--){
int j=i/2;
do{
System.out.println(j);
}
while(j++<i--);>
}
}
}
Could anyone explain how the output is 5678

Good question again...
The issues are post increment operator and the 'i' variable availability in the block
while(j++ <i--) //5><10 and then increments j, decrements i
This goes on.... until the condition is false and loops out
Result : 5678
ragu
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
Sorry there was a mistake in my previous question
class Loop{
public static void main(String args[]){
for (int i=10; i>5; i--){
int j=i/2;
do{
System.out.println(j);
}
while(j++< i--)//mistake
}
}
}
Could anyone explain how the output is 5678
(You need to put a space after your < sign or the ubb software used in this forum thinks you are trying to write html tags -- Marilyn)
[This message has been edited by Marilyn deQueiroz (edited November 08, 2001).]
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
the "5" is from the for loop.
and "6" "7" "8" is from the do while loop.
first.. j=10/2 which it is 5
that went in the do while() loop and got print it out.
And in the do while() loop, j++ and i-- :
j = 5 , i = 10
j = 6 , i = 9
j = 7 , i = 8
j = 8 , i = 7
and then break in the do while().
After do while() loop i is 6 and becuase of the for() loop's i--
i is 5 now. Since 5 > 5 is false ... the for loop ends.
Darryl Failla
Ranch Hand

Joined: Oct 16, 2001
Posts: 129
while(j++) will not compile. There must be a boolean result in the parentheses for compiliation to complete.


Darryl Failla
Sun Certified Java 2 Programmer
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
I don't know why its not getting printed

class Loop{
public static void main(String args[]){
for (int i=10; i>5; i--){
int j=i/2;
do{
System.out.println(j);
}
while(j++ < i--)
}
}
}
Could anyone explain how the output is 5678

[This message has been edited by Marilyn deQueiroz (edited November 08, 2001).]
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
he meant to write while( j++ < i--);
html took out "< i--", take out the space in "< i".
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
hey guys sorry it is while(j++<i--)>
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
yes FEI NG i want to type the same but again it is not showing.
I won't type again
Fei Ng
Ranch Hand

Joined: Aug 26, 2000
Posts: 1242
hi Neha,

take a look of the post above.
Neha Sawant
Ranch Hand

Joined: Oct 11, 2001
Posts: 204
thanx FEI NG
i got it
Regards
Neha
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how is the flow