The code is wrong here
k=(i+j) / 2;
if (a[k] < x) then i = k;
else j = k;
The (correct) code should be:
k=(i+j) / 2;
if (a[k] < x) then i = k + 1;
else j = k - 1;
We can try an example with the given code in question
Let the array be $\qquad a[1,2,3,4,5,6,7,8,9,10]$
Index numbers$\qquad \quad1,2,3,4,5,6,7,8,9,10$
Let $x=10$; now run the code;
Initially $i = 1, j=10$;
first time $k =(i+j) /2 = 11/2 =5.5 = 5$ (because of integer type) $=i$
second time $= k =(i+j) /2 =15/2 =7.5 =7 =i$
third time $= k =(i+j) /2 = 17/2 = 8.5 = 8 =i$
fourth time $= k =(i+j) /2 = 18/2 = 9 = i$
fifth time $= k =(i+j) /2 = 19/2 = 9.5 =9 = i$
sixth time $= k =(i+j) /2 = 19/2 = 9.5 =9 = i$
seventh time $= k =(i+j) /2 = 19/2 = 9.5 =9 = i$
Going to infinite loop (run time error)
For terminating the loop, it should be $i = k + 1$ instead of $i =k$ and $j = k - 1$ instead of $j = k$;