Reputation: 149
I'm trying to understand this program in java but i'm new to this language.
Can you tell me what
<=0?0:1;
means?
It's from the following code that decrements the elements of a matrix (tabu)
public void decrementTabu(){
for(int i = 0; i<tabuList.length; i++){
for(int j = 0; j<tabuList.length; j++){
tabuList[i][j]-=tabuList[i][j]<=0?0:1;
}
}
}
Upvotes: 0
Views: 19102
Reputation: 21
This is work as if condition, will take "c<=0?0:1;"
This means if c is less than or equal to zero then answer is 0 else 1
Upvotes: 2
Reputation: 498972
You are not looking at the operator correctly.
This is the conditional operator ?:
, which is the only ternary operator in JavaScript or Java (and other languages, such as C#). Ternary means it has three parameters.
Essentially this is what it means:
(condition)?(true branch):(false branch)
param1 param2 param3
In your code example, the condition (param1) is:
tabuList[i][j]<=0
If true, 0 (param2) is returned. If false, 1 (param3) is returned.
The return value is then decremented from tabuList[i][j]
via the -=
operator.
The whole statement:
tabuList[i][j]-=tabuList[i][j]<=0?0:1;
Can be written as:
if (tabuList[i][j] > 0)
tabuList[i][j]--;
Upvotes: 15
Reputation: 15219
tabuList[i][j]-=tabuList[i][j]<=0?0:1;
can be written as:
int tabuListEntry = tabuList[i][j];
tabuListEntry -=tabuListEntry <=0?0:1;
can be written as:
int tabuListEntry = tabuList[i][j];
tabuListEntry = tabuListEntry - (tabuListEntry <=0?0:1);
can be written as:
int tabuListEntry = tabuList[i][j];
int decrementAmount = tabuListEntry <=0?0:1;
tabuListEntry = tabuListEntry - decrementAmount ;
can be written as:
int tabuListEntry = tabuList[i][j];
int decrementAmount = 0;
if(tabuListEntry <= 0) {
decrementAmount = 0;
} else {
decrementAmount = 1;
}
tabuListEntry = tabuListEntry - decrementAmount ;
can be written as:
int tabuListEntry = tabuList[i][j];
int decrementAmount = 0;
if(tabuListEntry > 0) {
decrementAmount = 1;
}
tabuListEntry = tabuListEntry - decrementAmount ;
can be written as:
int tabuListEntry = tabuList[i][j];
if(tabuListEntry > 0) {
tabuListEntry = tabuListEntry - 1;
}
Upvotes: 5