Rp Ck
Rp Ck

Reputation: 59

Else if statements

I'm a beginner to java... if statements followed by else if statements are evaluated in order until one that evaluates to true is found, and I've seen many examples of this. But in this program, both statements (the if and the else if) are evaluated. Why?

public int centeredAverage(int[] nums) {

    int[] nums = {1, 1, 5, 5, 10, 8, 7};

    int sum = 0;
    int centered = 0;
    int min = nums[0];
    int max = nums[0];
    int i = 0;

    for (i = 0; i < nums.length; i++){
        if (nums[i] < min){
            min = nums[i];
        } else if (nums[i] > max){
            max = nums[i];
        }
        sum += nums[i];
        centered = ((sum-max-min)/(nums.length-2));
    }

    return centered;
}

Upvotes: 0

Views: 185

Answers (4)

TheArchon
TheArchon

Reputation: 313

 Im guessing this is the same problem from codingbat, next time copy and paste the problem desciption for others!

public int centeredAverage(int[] nums) {
       Arrays.sort(nums); //sorts the array smallest to biggest
       int total = 0;
       //nums is already sorted, so the smallest value is at spot 0
       //and the biggest value is at the end.
       for(int a = 1; a < nums.length - 1; a++){ //avoid the first and last numbers
       total += nums[a];
       }
       return total / (nums.length - 2); //need ( ) so we can substract 2 first

       //Another way could simply sum all the elements then subtract from that sum
       //the biggest and smallest numbers in the array, then divide by nums.length- 2, it is a 
       //little more complex, but allows a for : each loop.
    }

But for you, well since you are a beginner, restate your strategy (algorithm), find the smallest and biggest numbers in the array, subtract that out of the sum of all elements in the array then divide that number by nums.length - 2, since we are ignoring 2 numbers.

Upvotes: 0

user12
user12

Reputation: 1

Working of If statement followed by else-if is fine here. We are getting expected result here. Both the statements if and else-if are not executed. Only that statement is executed which comes TRUE as per logic. Here we can identify the working of the program using "System.out.println". Code and console output is given below...

    int[] nums = {1, 1, 5, 5, 10, 8, 7};

    int sum = 0;
    int centered = 0;
    int min = nums[0];
    int max = nums[0];
    int i = 0;

    for (i = 0; i < nums.length; i++)
    {
        if (nums[i] >  min)
        {
           min = nums[i];

            System.out.println("inside first if:  " + i);
            // taking value of i in SOP to get the iteration value 

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

       sum += nums[i];
        centered = ((sum-max-min)/(nums.length-2));

        System.out.println("inside else if:  " + i);
         // taking value of i in SOP to get the iteration value 

    }

    System.out.println("centered value "
            + " " + centered);

You can make a good usage of SOP in every program to get the execution order.

Upvotes: -1

dmonarch
dmonarch

Reputation: 75

Your passing in array of doubles by reference called nums and the defining an array of the same name in the method which seems odd. Also your start index for your for loop should be 1

Upvotes: 0

candied_orange
candied_orange

Reputation: 7344

Because they're in a loop that changes i and so changes nums[i] and so changes what if's are true.

Upvotes: 3

Related Questions