• 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

confusing incrementa nad decrement operators

 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int x;
int y=5;

for(x=0;x<y;x=++x)
System.out.println(x+" "+y);

System.out.println(x);....i understood output is

0 5
1 5
2 5
3 5
4 5
5

but why this loop is infinite

int x;
int y=5;

for(x=0;x<y;x=x++)
System.out.println(x+" "+y);

System.out.println(x);...unable to get.....



 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akshay Rawal wrote:
but why this loop is infinite

int x;
int y=5;

for(x=0;x<y;x=x++)
System.out.println(x+" "+y);

System.out.println(x);...unable to get.....




see ... https://coderanch.com/how-to/java/PostIncrementOperatorAndAssignment
 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry...but still unable to get..
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Akshay Rawal wrote:sorry...but still unable to get..



Start with your code ...

Akshay Rawal wrote:



Examine the re-initialization expression part of the for-loop...



Now, read the article again.

With a better understanding what is happening to the x variable during re-initialization, do you understand why the loop is doing what it is doing?

Henry
 
This is my favorite show. And this is my favorite tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic