• 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

Help with while

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class NoBody
{
public static void main(String[] args)
{
int i,j;
i=100;
j=200;
while(++i ==102)
System.out.println("Mid point is" + i);
}
}
Why their is no output?
 
Ranch Hand
Posts: 77
Android MyEclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because at first i value is 100, in expression ++i its preincrementing i to 101 and checking with 102 whether it is equal or not , it is not equal so it comes put of loop ans again the i value is 100 only , so it wont print any output
 
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by damodar kumar:
because at first i value is 100, in expression ++i its preincrementing i to 101 and checking with 102 whether it is equal or not , it is not equal so it comes put of loop ans again the i value is 100 only , so it wont print any output



Actually, when it comes out of the "loop", its value is still 101 since nothing else changes i's value or decrements it.
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sumaraghavi,

You are probably looking to do something more along the lines of this:



but that would be an unusually construct. Generally, when using a counter or index, like i, a 'for' loop is used rather than a 'while' loop. While both a 'while' loop and a 'for' loop would be syntactically correct, the 'for' loop is more conventional, and easier to understand. So it will make your code easier for others to read and understand.



if you need i to be in scope after the 'for' loop, you can do this:


[ June 02, 2008: Message edited by: Mark Vedder ]
 
sumaraghavi ragha
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks all
 
When all four tires fall off your canoe, how many tiny ads does it take to build a doghouse?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic