-1

Never seen something like this before. Specifically line 4. Currently I understand:

days array at position i-1 gets the value...

I don't know anything beyond the = sign excluding the concatenation from the +.

public String[] months() {
        String[] days = new String[12];
        for (int i = 1; i <= 12; i++) {
            days[i - 1] = i < 10 ? "0" + Convert.ToString(i) : Convert.ToString(i);
        }
        return days;
    }

Also why are there 2 converts?

Looking further into the other code I think the developer copied and pasted previous code. Days array should be months probably, as there are 12 months.


Solved

Thanks, never seen ternary operators before. Thanks!

public String[] months() {
        String[] months = new String[12];
        for (int i = 1; i <= 12; i++) {
            /* faster way of saying */
            /* if i < 10 {} else {} */
            /* if condition is met do everything before the :, else do everything after */

            /* checks for 10 because months can be 1 or 2 digits, 0-9, then 10-12 */
            /* single digits concatenated to the 0 */
            /* double digits replace the 0 */
            months[i - 1] = i < 10 ? "0" + Convert.ToString(i) : Convert.ToString(i);
        }
        return months;
    }
Community
  • 1
  • 1
Nathaniel
  • 70
  • 1
  • 10
  • There is just one "convert" performed: *either* `"0" + Convert.ToString(i)` *or* `Convert.ToString(i)`. It sounds like you're asking about the C# [ternary operator](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator), which comes from the C language, and is also used in Java, Javascript and many other languages. – paulsm4 Oct 05 '18 at 23:09
  • This is several questions at once. Let's start with the ***ternary operator*** (you can [search](https://duckduckgo.com/?q=c%23+ternary+operator) for this term and find plenty of good info), because it's the outermost language construct here that you find alien. Get to grips with it, then you'll see the different clauses of the problem line, and if they're still problematic, you can ask another question. – spender Oct 05 '18 at 23:12
  • FYI the official name is the conditional operator which happens to be the only ternary operator (meaning it has 3 operands) – juharr Oct 05 '18 at 23:27

3 Answers3

2

This is the conditional operator, also known as the ternary conditional operator.

It's shorthand for...

if (i < 10)
{
    days[i - 1] = "0" + Convert.ToString(i);
}
else
{
    days[i - 1] = Convert.ToString(i);
}

The code is basically prepending a "0" to the front of single digit numbers.

Tony Morris
  • 947
  • 8
  • 13
0

It's the ternary operator. If the expression in front of the ? sign is true, the result will be the value after the ? sign, otherwise the value after the colon.

This code adds a leading zero to the value. If the value is less than 10, the value becomes "0" + the value, so 9 becomes "09" etc.

HaukurHaf
  • 13,522
  • 5
  • 44
  • 59
0

?: is shorthand

IF i < 10

days[i-1] = "O" + Convert.ToString(i)

ELSE

days[i-1] = Convert.ToString(i)

emsimpson92
  • 1,779
  • 1
  • 9
  • 24