Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how is the flow

 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
Ragu Sivaraman
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while(j++) will not compile. There must be a boolean result in the parentheses for compiliation to complete.
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
he meant to write while( j++ < i--);
html took out "< i--", take out the space in "< i".
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey guys sorry it is while(j++<i--)>
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes FEI NG i want to type the same but again it is not showing.
I won't type again
 
Fei Ng
Ranch Hand
Posts: 1245
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Neha,

take a look of the post above.
 
Neha Sawant
Ranch Hand
Posts: 204
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanx FEI NG
i got it
Regards
Neha
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic