jeudi 16 janvier 2020

Best way to implement conditional expression based on numeral value (positive, negative, zero)

Is there a better eleganter way to implement the naive following code (diffYear, A, and B are numerals):

diffYear = yearA - yearB;

if (diffYear == 0) {
    A = B = 0;  
}
else if (diffYear > 0) {
    A = diffYear * -1;
    B = 0;
}
else if (diffYear < 0) {   // obviously one could only write a simple else, this is for the sake of the example
    A = 0;
    B = diffYear;
}

Aucun commentaire:

Enregistrer un commentaire