• 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

while(m++

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is one problem from MindQ mock exam, could anybody explain the answer to me? why there is two outputs, instead of just "2"
29. For the code:
m = 0;
while( m++ < 2 )
System.out.println( m );
Which of the following are printed to standard output?
a) 0
b) 1
c) 2
d) 3
e) Nothing and an exception is thrown
thank you in advance.
lucy
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since u are using System.out.println(m) in side loop the value of m
which is =1 for first time
the =2 for next time
so values 1 and 2 are printed.
-Harish
Of course this happens when u say
int m=0;
U haven't declared 'm' here so it gives error
[This message has been edited by Harish Babu.N (edited December 17, 2000).]
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
The answer is two values are printed, 0 and 1.
1. first iteration m=0 (Assuming m is an instance variable, it is automatically initialized to 0) so the condition m<2 is true
so 0 is printed then m is incremented(post increment) now,m=1
2. second iteration m=1 m<2 is true. therefore, 1 is printed.
m is then incremented to 2
3. third iteration m=2 m<2 is false, so exit loop
HTH,
------------------
Regards,
Shree
 
lucy hu
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Harish Babu.N:
since u are using System.out.println(m) in side loop the value of m
which is =1 for first time
the =2 for next time
so values 1 and 2 are printed.
-Harish
Of course this happens when u say
int m=0;
U haven't declared 'm' here so it gives error
[This message has been edited by Harish Babu.N (edited December 17, 2000).]


Harish:
why "since u are using System.out.println(m) in side loop..." ?
what's the point of "System.out.println(m)"
I still don't get it,
Lucy
 
Ranch Hand
Posts: 73
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
shree vijay,
The output will be 1 and 2 but not 0 and 1.
Initially m is 0
so ( m++ < 2 ) ==> (0<2) and m increases to 1 and prints 1
Now m = 1
so ( m++ < 2 ) ==> (1<2) and m increases to 2 and prints 2
The point here is m increases after the comparision but not after printing.

Correct me if i am wrong
rajani


[This message has been edited by rajani peddi (edited December 17, 2000).]
 
shree vijay
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rajani,
You are correct. It prints 1 and 2.
------------------
Regards,
Shree
 
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How can it print 2 when the condition of the loop says the m must be less than 2 ... therefore it should print 0 and 1. I'm going to run it to check though!
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ahh no i see .... it does print 1 and 2 because the condition is that m++ is less than 2 ...
so first time round the loop :
m = 0;
while (m++ < 2) //m is counted as 0 then incremented
System.out.println (m) // m == 1
then next time round
while ( m++ < 2 ) // m is = 1 then incremented
System.out.println (m) // m == 2
oooh I can imagine this being a common trap in the exam!
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its quite simple in the while loop & do-while loop the checking(boolean condition) is checked first and then the increment is done.
Whereas in a for loop the increment is done first and then the condition is checked.
Vandanam
vijay
 
reply
    Bookmark Topic Watch Topic
  • New Topic