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
posted
0
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
posted
0
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: 1241
posted
0
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: 127
posted
0
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
posted
0
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: 1241
posted
0
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
posted
0
hey guys sorry it is while(j++<i--)>
Neha Sawant
Ranch Hand
Joined: Oct 11, 2001
Posts: 204
posted
0
yes FEI NG i want to type the same but again it is not showing. I won't type again