Q:

Getting largest and second largest number in a sequence

Help me understand the last portion of this code that aims to print the largest and second largest number in a sequence. What I am not fully understanding is, what is the  need for the else if statement? with the code:
 if (input > largest) {
        secondLargest = largest;
        largest = input;

should that not do the job proper? it checks if input is greater than the largest number, sets the second Largest to the previous largest number. and update the new largest number with the one user inputed.
So what exactly is the purpose of this line of code then? and any reason the integers largest and secondLargest is set to -1and not just 0, has it to do with the sentinel that breaks the program is set to 0?
} else if (input > secondLargest) {
    secondLargest = input;

.
 int largest = -1;
  int secondLargest = -1;
  while (true) {
     int input = readInt(" ? ");
     if (input == SENTINEL) break;
     if (input > largest) {
        secondLargest = largest;
        largest = input;
     } else if (input > secondLargest) {
        secondLargest = input;
     }

A:

Try your program with the sequence
    1 2 3 5 4

If you omit else if (input > secondLargest) { secondLargest = input; } then result will be largest=5 and secondLargest=3, that is incorrect.

