wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Increment Operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Increment Operator" Watch "Increment Operator" New topic
Author

Increment Operator

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi,

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



OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
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'.


SCJP 1.6 96%
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

PostIncrementOperatorAndAssignment
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

if you replace this

with


SCJP6.0,My blog Ranchers from Delhi
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Shanky Sohar wrote:if you replace this

with

Why keep it complicated? Just lose the assignment and use i++;


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Mina Daoud
Ranch Hand

Joined: Sep 24, 2010
Posts: 88
why are you using
using will do the job and keep your code clean and simplified!
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438


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

    SCJP 6 [86%] June 30th, 2010
    OCPWCD [84%] March 26th, 2013
    If you find any post useful, click the "plus one" sign on the right
     
    Consider Paul's rocket mass heater.
     
    subject: Increment Operator
     
    Similar Threads
    Post-increment operator
    A simple question i=i++ ?!
    post increment
    Operator Evaluation
    Passing Value