• 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

Loop

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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);
 
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, I am getting 11 as output
 
Tim Cooke
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And why do you think that is?
 
kavi lathigra
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 5555
326
IntelliJ IDE Python Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 136
1
Netscape Opera Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True, this is for a general technical aptitude test given by companies.. As I am a fresher.
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic