# Thread: Help Understanding this simple code

1. Member Join Date
Nov 2014
Posts
5
Rep Power
0

## Help Understanding this simple code

Im starting my study on array's using the oracle press book and came across a section that has stumped me. The code below works I'm just not sure how.
The following line are where I'm having trouble.

min = max = nums ;
if (nums [i] < min ) min = nums [i];
if (nums [i] > max ) max = nums [i];
-------------------------------------------------------------------------
Java Code:
```public class firstarray {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
int nums [] = new int ;
int min, max;
nums  =99;
nums  = -10;
nums  =100123;
nums  =18;
nums  =978;
nums  =5623;
nums  =463;
nums  =-9;
nums  =287;
nums  =49;
min = max = nums ;
for (int i=1; i < 10; i++) {
if (nums [i] < min ) min = nums [i];
if (nums [i] > max ) max = nums [i];

}
System.out.println("min and max:" + min + " " + max);

}
}```
Last edited by fermon; 11-13-2014 at 02:34 AM.  Reply With Quote

2. ## Re: Help Understanding this simple code

Please edit your post and wrap your code with code tags:
[code]
YOUR CODE GOES HERE
[/code]
to get highlighting and preserve formatting.

Try playing computer with the code. Use a paper and pencil to write done the values of the variables as they are changed by each statement in the code.

What do the comments for the code say that the code is supposed to do?  Reply With Quote

3. Member Join Date
Nov 2014
Posts
5
Rep Power
0

## Re: Help Understanding this simple code

The Code prints the following below which is what its suppose to print, Im just having trouble understanding how it works .
The three line below are where I'm stuck .

line 19 min = max = nums ;
line 21 if (nums [i] < min ) min = nums [i]
line 22 if (nums [i] < min ) min = nums [i];

Code output :
This is sample  : 0
This is sample  : 1
This is sample  : 2
This is sample  : 3
This is sample  : 4
This is sample  : 5
This is sample  : 6
This is sample  : 7
This is sample  : 8
This is sample  : 9  Reply With Quote

4. Senior Member Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
13

## Re: Help Understanding this simple code Originally Posted by fermon The Code prints the following below which is what its suppose to print, Im just having trouble understanding how it works .
The three line below are where I'm stuck .

line 19 min = max = nums ;
line 21 if (nums [i] < min ) min = nums [i]
line 22 if (nums [i] < min ) min = nums [i];

Code output :
This is sample  : 0
This is sample  : 1
This is sample  : 2
This is sample  : 3
This is sample  : 4
This is sample  : 5
This is sample  : 6
This is sample  : 7
This is sample  : 8
This is sample  : 9
The code output shown is not produced by the code in your original post.

Regards,
Jim  Reply With Quote

5. ## Re: Help Understanding this simple code

Did you do the paper and pencil exercise with the code? That is a technique you need to learn.
On paper write down the values in the array.
Then for this statement:
min = max = nums ;
It assigns the value in nums to the two variables: min and max
So after that statement
min = 99
max = 99

Now continue through the rest of the code, look at the compares and see if they are true or not
If true, "execute" the statements inside the if statement by changing the values of min or max as appropriate.
Increment the value of i for the next iteration through the loop and do it all again.  Reply With Quote

6. Member Join Date
Nov 2014
Posts
5
Rep Power
0

## Re: Help Understanding this simple code

Sorry the output should be whats seen below .

min and max:-10 100123  Reply With Quote

7. Member Join Date
Nov 2014
Posts
5
Rep Power
0

## Re: Help Understanding this simple code

Norm thanks for the response, I guess thats the meaning of pseudocode . It really does help when you write it on paper you're absolutely correct.  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•