• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

how is the flow

 
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).]
 
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).]
 
Ranch Hand
Posts: 1246
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.
 
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: 1246
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: 1246
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
 
This is my favorite tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!