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.
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 {