2

In the following code for finding largest number in an array, why is the max put outside the code? Why is the max outside the brackets?
In this line, I am unable to understand the construct of the if statement.

public class TestArray {

       public static void main(String[] args) {
          double[] myList = {1.9, 2.9, 3.4, 3.5};

          // Print all the array elements
          for (int i = 0; i < myList.length; i++) {
             System.out.println(myList[i] + " ");
          }

          // Summing all elements
          double total = 0;
          for (int i = 0; i < myList.length; i++) {
             total += myList[i];
          }
          System.out.println("Total is " + total);

          // Finding the largest element
          double max = myList[0];
          for (int i = 1; i < myList.length; i++) {
             if (myList[i] > max) max  = myList[i]; 
          }
          System.out.println("Max is " + max);  
       }
    }
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
Mayank
  • 69
  • 9
  • The same reason `total` is "outside the brackets" in the previous loop. If it were inside then it would not be visible to the `println` *after* the loop. As for the `if`, it's *basically* the same as `max = Math.max(max, myList[i]);` – Elliott Frisch Aug 11 '18 at 05:42
  • if statement check the element on index 'i' is greater than your current maximum. if greater update tour current maximum , other wise your maximum not update. – Morteza Jalambadani Aug 11 '18 at 05:46
  • By "brackets" you mean the "()" instead of the "{}", don't you? You need to clarify this, to avoid confusion. – Yunnosch Aug 11 '18 at 06:06
  • just wrap it with curly brackets: `if(myList[i] > max) {max = myList[i];}` and also see https://stackoverflow.com/questions/8898590/short-form-for-java-if-statement – Martin Zeitler Aug 11 '18 at 06:26

3 Answers3

1

This line

if (myList[i] > max) max  = myList[i];

can be seen as

if (myList[i] > max)
{ 
    max  = myList[i];
}

I.e. it has a conditional if and when that is satisfied there is a single statement of what to do. I.e. the second max is not part of the logical expression for the if, it is part of the code to execute conditionally.

The total effect is to look at each entry in myList, compare it to the maximum value seen before and keep the higher one, as that highest value seen before.

Concerning the "why?", it is a matter of taste.
The advantage is less character to type and "shorter" code to read.
My reason for always using the longer version is mostly coding rules, which are the more important for maintenance the higher the number of different people is who work on a given project.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
1

Suppose if you declare "double max = myList[0]; " inside the for loop, for each iteration max variable will get declared and initialized to first element in the list..

example if list contains 2, 4, 1, 9, 8 we are considering 2 is the largest value (so store 2 in max variable) and then comparing 2 with other values in the list

if there is only one statement to perform inside the IF then you can write IF without brackets

like

if(a>b)

    System.out.println(a + "is greater");

it is similiar to

if(a>b)

{

System.out.println(a + "is greater");

}

Sharu Shah
  • 11
  • 2
0

java allows you to put single line statement without braces. if you don't want if statement you can use max in Math package instead.

max = Math.max(myList[i],max);
digitebs
  • 834
  • 8
  • 10