Gil Savage
Gil Savage

Reputation: 9

Will my counter variable increment before i reach the next if statement?

I am trying to increment a counter variable (mPressCount) every time a button is pressed. My question is: will mPresscount be 1 before it gets to the third if statement, assuming that it was 0 when the button was clicked? In other words, when mPressCount++; is read, does it get incremented immediately or does the button need to be pressed again before the variable is incremented?

Here is my code:

mButton1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(mPressCount < 2) {
                mButton1.setText(mWord4);
                mButton1.setClickable(false);
                mButton1Pressed = true;
                mPressCount++;
                if(mButton7Pressed) {
                    mMatchedWord4 = true;
                }
            }
            if(mPressCount == 1) {
                //im going to do something if the count is at 1 like I hope 
                //it is...Thanks for your insights everyone
            }

Upvotes: 1

Views: 47

Answers (2)

Thinsky
Thinsky

Reputation: 4246

if mPressCount = 0, mPressCount < 2, then mPressCount++ will 1, it gets to the third if statement. if mPressCount = 1, mPressCount < 2, then mPressCount++ will 2, it will not gets to the third if statement

Upvotes: 1

Xander Luciano
Xander Luciano

Reputation: 3893

If mPressCount begins at zero when your button is pressed, your first if statement

if(mPressCount < 2) will be executed. Including mPressCount++; which will make

mPressCount = 1

The code will continue executing down the file until it reaches the third if statement if(mPressCount == 1) which will be executed because as we said previously, mPressCount = 1

Programs are like reading, they go line by line. Hope this helps :)

Upvotes: 2

Related Questions