This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Increment Operator

 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Here increment operators at Line # 9 and 17 aren't working as expected. Please advice if you have any idea

 
Neha Daga
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
at these lines the value of 'i' is assigned first to the variable at left hsnd side and then increased by 1. So, when the value is assigned it is '0'.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
 
Shanky Sohar
Ranch Hand
Posts: 1051
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if you replace this

with
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shanky Sohar wrote:if you replace this

with

Why keep it complicated? Just lose the assignment and use i++;
 
Mina Daoud
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why are you using
using will do the job and keep your code clean and simplified!
 
Prasad Kharkar
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Neha has solved the problem
but let me explain a little more



well we will go through some rules
  • increment operator is used AFTER the variables has been used
  • here = operator comes into act before the ++ operator


  • so in this case
    suppose value of i = 0
    hence
    at first, i = 0 happens
    and hence when printing value of i we get 0 as output

    hope this helps
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic