wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Codes For Practice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Codes For Practice" Watch "Codes For Practice" New topic
Author

Codes For Practice

N Mukherjee
Ranch Hand

Joined: Jun 08, 2000
Posts: 141
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).]


"Knowledge is Power"****************<A HREF="http://www.geocities.com/nityananda_mukherjee" TARGET=_blank rel="nofollow">THREAD/SCJP RESOURCES</A>
Deepak Jindal
Ranch Hand

Joined: Nov 16, 2000
Posts: 30
In loop: 4
In loop: 2
After loop: 1
Sridhar Katakam
Greenhorn

Joined: Jan 28, 2001
Posts: 8
In loop: 5
In loop: 4
In loop: 3
After loop: 3
Is this correct ?
~Sri~
Tejal Shah
Greenhorn

Joined: Jan 18, 2001
Posts: 14
I think output will be
in loop:4
in loop:2
afterloop:1
Tejal
vikas singh
Greenhorn

Joined: Jan 22, 2001
Posts: 23
good Q's
inside loop : 4
inside loop : 2
outside loop : 1
[This message has been edited by vikas singh (edited January 29, 2001).]
Sunil K Bansal
Ranch Hand

Joined: Jan 04, 2001
Posts: 62
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

Joined: Jan 28, 2001
Posts: 8
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~
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Codes For Practice