# Thread: why there's a difference between the 'while' and 'if' in this code

1. ## why there's a difference between the 'while' and 'if' in this code

this code is showing the min value .
with the while loop its working fine- the min is 2
if you replace the 'while' with 'if' the min is 33 which is wrong.

i tried to write myself the iterations. i got little confused.
also if i check the max value instead of min its working fine with if.

Code:
``` #include <stdio.h>
int main(){

int number[] = {35,32,21,4,33,43,2};

int i, j , k ,min;
i= 0 ; j=6; k = 0;  //j = the end of the array
//k = start of the array

for(; i<=j; i++){
while(number[k]>number[0+i]) //put 'if' is wrong.why?
{
k++;
}
}
min=number[k];
printf("min=%d",min);

return0;

} ```

2. Because you wrote the for loop in a very strange way.

Tim S.

3. The correct statement would be:
Code:
```if (number[k] > number[i])
{
k = i;
}```

4. why do you need k at all?
Code:
```if(min_val > number[i])
{
min_val = number[i];
}```

5. Code:
```int find_min(const int* arr, unsigned int siz)
{
assert(arr && siz);
int min = *arr;
while( --siz ) {
++arr;
if( *arr < min ) { min = *arr; }
}
return min;
}```