File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Codes For Practice

 
N Mukherjee
Ranch Hand
Posts: 141
  • 0
  • Mark post as helpful
  • send pies
  • 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).]
 
Deepak Jindal
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In loop: 4
In loop: 2
After loop: 1
 
Sridhar Katakam
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In loop: 5
In loop: 4
In loop: 3
After loop: 3
Is this correct ?
~Sri~
 
Tejal Shah
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think output will be
in loop:4
in loop:2
afterloop:1
Tejal
 
vikas singh
Greenhorn
Posts: 23
  • 0
  • Mark post as helpful
  • send pies
  • 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).]
 
Sunil K Bansal
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic