• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loop  RSS feed

 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can anyone explain how exactly this loop works.. In the sense how i gets incremented in while loop when pre/post-increment operator is assigned to it.. ?

public static void main(String []args){
int i=10;

while(++i<=10){
i++;
}
System.out.print(i);
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What output do you get from this code?
How would you explain what is going on?

Have a read through the Java Tutorial for Assignment, Arithmetic, and Unary Operators, particularly the section on Unary Operators for an explanation of how the ++ operator works.

Once we have your understanding of things then that gives us a basis for further discussion.
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I am getting 11 as output
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And why do you think that is?
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
first of all

in the loop

While(++i<=10) // the i value is already 11, so condition fails..

so !
 
Tim Cooke
Marshal
Posts: 4044
239
Clojure IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. The ++ Unary Operator applied as a prefix to i will increment it before evaluating its value, so the condition (++i<=10) reduces to (11<=10) which is of course false. The while loop is never entered and the value of i is printed which is 11.

Any questions?
 
Red Smith
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kavi lathigra wrote:

first of all

in the loop

while(++i<=10) // the i value is already 11, so condition fails..

so !



To understand what is happening in something like this I usually put a System.out.println() in the loop. Or two, one at the top of the loop and one at the bottom. There could be one before the i++; and one after to show what is happening and how many iterations it is doing.
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, Actually I thought that the value of i in the remains in the loop itself, but does not get printed, as i is defined with 10 at the beginning.
Thank you.
 
Campbell Ritchie
Marshal
Posts: 56541
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I trust that is only “what happens if I try this?” code; you would never write that in a real‑life application.
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
True, this is for a general technical aptitude test given by companies.. As I am a fresher.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!