Sometimes you need to use “If Then Else” logic on something
really simple, but all those brackets and parentheses are ugly and
confusing. So instead use some fancy shorthand!
In this example I want to display the top 5 items in my array, but if my array is less than 5 items long I want to show all of the items up to the length of my array. (Otherwise I’d get an “index out of bounds” error for trying to access array items that don’t exist.)
Your significant other is gonna’ be really impressed with this bit of code! Well, unless your significant other is also a programmer… then my sarcasm is lost. :/
In this example I want to display the top 5 items in my array, but if my array is less than 5 items long I want to show all of the items up to the length of my array. (Otherwise I’d get an “index out of bounds” error for trying to access array items that don’t exist.)
int showTopEntries = 5; showTopEntries = showTopEntries >= totalEntries ? totalEntries : showTopEntries;
This reads as: If 5 is greater than or equal to my array’s length, then use my array’s length, else use 5.
Your significant other is gonna’ be really impressed with this bit of code! Well, unless your significant other is also a programmer… then my sarcasm is lost. :/
Sign up here with your email
ConversionConversion EmoticonEmoticon