0

I am using some example code from the internet that manages music. One section of the code is a way of pausing music and preparing it to play again. I am not understanding a double bracket notation. I thought it may have been a fancy way of doing an If-Else, but my equivalent snippet of code does not work, however, the code with the double brackets works perfectly. What exactly does it mean when you add two open brackets to an If-statement?

Here is the snippet of code

// previousMusic should always be something valid
if (currentMusic != -1) {{
    previousMusic = currentMusic;
    Log.d(TAG, "Music is paused");
}

currentMusic = -1;
Log.d(TAG, "Paused music is prepared to play again (if necessary)");
}

Here is what I thought it could have meant. It does not work as intended, so this is not the same actually.

// previousMusic should always be something valid
if (currentMusic != -1) {
    previousMusic = currentMusic;
    Log.d(TAG, "Music is paused");
} else {
    currentMusic = -1;
    Log.d(TAG, "Paused music is prepared to play again");
}

Thank you in advance for the explanation.

Community
  • 1
  • 1
portfoliobuilder
  • 7,556
  • 14
  • 76
  • 136
  • 3
    the nested {} pair is useless in this case. normally we use that to limit certain variable scope\ – someone_ smiley Aug 25 '14 at 02:47
  • @someone_smiley Thank you for your response. You are right, although I do not understand why someone would put this notation in their example code. But then again, I don't understand variable scope either :-). But I will look it up right now! – portfoliobuilder Aug 25 '14 at 02:52

3 Answers3

3
hat exactly does it mean when you add two open brackets to an If-statement?

Nothing special really it is just a block of code where you put your previousMusic = currentMusic in another scope of the method.

It is like saying:

if (currentMusic != -1) {
   previousMusic = currentMusic;
   Log.d(TAG, "Music is paused");

   currentMusic = -1;
   Log.d(TAG, "Paused music is prepared to play again (if necessary)");
}

But if you make a variable inside the block of code then you cant access it outside the block because the scope of the variable is only accessible to the block of code.

    if(1==1)
    {
        int i2; 
        {
            int i;
            i2= 1; //can access from top level scope
        }
        i = 0; //compile time error cant access the variable in the block of code
    }
Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
2

I hope you are aware about the scope of variables.

Example 1:

if(some condition){

    { // x is born here
        int x = 32;
    } // x is dead here
    // not allowed!
    Log.d(TAG,"Value is: " + x);
}   

Example 2:

if(some condition){
    int x = 32;
    // totally legit!
    Log.d(TAG,"Value is: " + x);
}   

See the subtle difference between the two?

In Example 1, the nested {} limit the scope of x. The variable x is available for use only till the closing brace } of its corresponding opening brace {

An SO User
  • 24,612
  • 35
  • 133
  • 221
  • O, Wow! I have just been enlightened! This is clear to me now. So basically, they limited the scope of currentMusic and previousMusic variables. This is why they reassigned currentMusic like that. Awwww, now I get it! – portfoliobuilder Aug 25 '14 at 02:55
  • You and Rod_Algonquin both gave valid answers, but I think I will mark this one, because of the more indepth explanation about the scope of variables. That is when I was able to really understand this notation. – portfoliobuilder Aug 25 '14 at 02:56
1

The correct way to understand that code is:

if (currentMusic != -1) {
    {
        previousMusic = currentMusic;
        Log.d(TAG, "Music is paused");
    }

    currentMusic = -1;
    Log.d(TAG, "Paused music is prepared to play again (if necessary)");
}

When you put a braces in your code, you just create a new scope to handle your variables.

Paulo
  • 2,956
  • 3
  • 20
  • 30
  • Although, I think the one who wrote the code makes it unnecessarily convoluted. I dont see the need for a nested `{}` – An SO User Aug 25 '14 at 02:59