```// find the smallest element in an array int a[].
assert a.length > 0 : "only works if there are some elements in the array";

// presume the smallest element is the first one.
int smallestSoFar = a[0];

// loop through all elements looking for an even smaller one.
for ( int candidate : a )
{
if ( candidate < smallestSoFar )
{
smallestSoFar = candidate;
}
}
// smallestSoFar now contains the smallest.

/////////////////////////////////////////////////////////////////////////////////////////

// if we need to find out where, (which index) the smallest was, you need code like this:
assert a.length > 0 : "only works if there are some elements in the array";

// presume the smallest element is the first one.
int smallestSoFarIndex = 0;
int smallestSoFar = a[0];

// loop through all elements looking for an even smaller one.
for ( int i=1; i<a.length; i++  )
{
if ( a[i] < smallestSoFar )
{
smallestSoFarIndex = i;
smallestSoFar = a[i];
}
}

// When you fall out the bottom of the loop,
// smallestSoFarIndex will have index of smallest,
// and smallestSoFar will have the smallest value```