• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Codes For Practice

 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,
Like to Practice?
First guess the output and then you can use jdk�����
Hope that helps,
Regards,
N Mukherjee,SCJP
1.Get the result without compiling����..
<WhileLoop.java>
public class WhileLoop {
public static void main(String args[]){
int x = 6;
while (x-- >2) {
x--;
System.out.println("In loop: " + x);
}
System.out.println("After loop: " + x);
}
}
------------------
"Knowledge is Power"****************MY SCJP RESOURCES
[This message has been edited by N Mukherjee (edited January 28, 2001).]
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In loop: 4
In loop: 2
After loop: 1
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In loop: 5
In loop: 4
In loop: 3
After loop: 3
Is this correct ?
~Sri~
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think output will be
in loop:4
in loop:2
afterloop:1
Tejal
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
good Q's
inside loop : 4
inside loop : 2
outside loop : 1
[This message has been edited by vikas singh (edited January 29, 2001).]
 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I got the same answers as above after compiling,
but can any body explain me how
the x-- is executed inside while loop?
thanks

Originally posted by vikas singh:
good Q's
inside loop : 4
inside loop : 2
outside loop : 1


[This message has been edited by Sunil K Bansal (edited January 30, 2001).]
[This message has been edited by Sunil K Bansal (edited January 30, 2001).]
 
Sridhar Katakam
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The whole code works like this:
x gets the value of 6.
x>2 is checked, i.e., 6>2 (evaluates to true);
Before entering the body of the loop, post decrement occurs, i.e., x gets the value of (x-1), i.e., x becomes 5
In the body of the loop, post decrement occurs, i.e., x gets the value of (x-1), i.e., x becomes 4;
'In loop: 4' is printed.
4>2 is checked (true);
x becomes 3.
In the body, x becomes 2;
'In loop: 2' is printed.
2>2 is checked (false); Before coming out of loop, x is decremented, i.e., becomes 1.
(Comes out of the loop) and 'After loop: 1' is printed.
----
If in the code, x-- is replaced by --x for both it's occurances, the result would still be the same.
My earlier post on this was wrong. I was under the impression that no assignment (i.e., x getting a new value), what so ever will occur in the condition of while loop.
~Sri~
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic