k1, k2, n1, n2: Variable <Name> might not have been initialized

procedure TForm1.Button1Click(Sender: TObject); var a:array[1..2,1..4] of char; i,j,k1,k2,n1,n2:integer; max,min:char; begin for i:= 1 to 2 do for j:= 1 to 4 do a[i,j]:=stringgrid1.cells[i-1,j-1][1]; max:=a[1,1]; for i:= 1 to 2 do for j:= 1 to 4 do if a[i,j]>max then begin max:=a[i,j]; k1:=i; k2:=j end; min:=a[1,1]; for i:=1 to 2 do for j:=1 to 4 do if a[i,j]<min then begin min:=a[i,j]; n1:=i; n2:=j end; a[k1,k2]:=min; a[n1,n2]:=max; for i:= 1 to 2 do for j:= 1 to 4 do stringgrid1.Cells[i-1,j-1]:=a[i,j]; end; end; end; 

    2 answers 2

    When you have a conditional construction:

     if a[i,j]>max then begin max:=a[i,j]; k1:=i; k2:=j end; 

    The compiler will warn you that before using variables it would be worth defining them in all areas. And you have them defined only under some condition.

      A translation of the phrase "Variable <Name> might not have been initialized" is the phrase "The variable <Name> may not have been initialized." For example, the value of k1 is assigned in the if block and, under certain values ​​in the conditional expression, the conditional operator will not be executed, as a consequence, below the code, when accessing array a by index k1, an error is possible.