On Sep 3, 7:56 pm, "Ivan Jimenez" <[EMAIL PROTECTED]> wrote:
> Hello everybody
> In this chapter, exercise 1.3, I have a question about this code fragment:
>
> // Compute the greatest number up to this point
> if((counter == 0)||(num[counter] > max))
> max = num[counter];
> }
>
> What does the first condition means?
It means that the first time the loop is entered (that is when
counter=0 in English word, counter==0 in Java word), max is
initialized to the value of num[0].
The or part (after ||) is not evaluated since the first part is true,
that's the way Java works.
>I remove it and It works the same.
You did not test every cases, test with all numbers negative, for
example -10, -5, -3, and you will see that the answer is incorrect. It
will be 0, because precisely you have removed the counter==0 part of
the if statement. Then subsequent loops give the same result, max
being initialized to 0 before the loop, and all numbers being
negative, with only the second part of the if, the result is always
false, hence the program won't go inside the if and max remains 0.
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---