At the beginning of the code, either no figure or active or some unspecified figure is active.
At some point you reach
If that statement is false then none of the rest of your code that you posted gets executed, and nothing at all gets plotted. So it is not that if statement that controls whether you are plotting to figure 1 or figure 2.
Assuming the statement is true (because you end the code if it is not) then you reach
imagesc(im); axis off; hold on; colormap gray;
If there is no active figure then when the imagesc() call goes to find out which axes it is drawing into, the MATLAB graphics library will create a new figure. It will have the lowest available (unused) integer figure number, which would usually be 1, but there are ways in which it is possible for any number of figures to already exist but for none of them to be happen to be active, so we cannot promise that the newly created figure is figure 1... it would just usually be figure 1. An axes would be created inside this new figure, and that would become the current axes.
If there is an active figure when imagesc() is called, then the MATLAB graphics library would look for the current axes in it; if there is no current axes (either because no axes exist in the figure or none of them are active) then MATLAB would create a new axes to draw in, making that the current axes. If there does happen to be a current axes already in the current figure, then it remains the current axes. It is entirely possible that a current figure exists that is not figure 1; if it happened to be figure 17 that was active, then it would be figure 17 that would be drawn on, because the graphics output location was all left to the default.
imagesc() and the other commands on that line would then apply their work to the current axes, no matter what figure it is on. Often that would be part of figure 1, but not always.
This drawing is done conditional only on the image being grayscale (and no drawing is done if it is not grayscale.) It is not conditional upon iszero .
Then you have the for i=0:k_max-1 loop. You have an if is_zero inside that loop, but that if statement only changes data and does not itself trigger any drawing.
After the if is_zero you arrive at the if i~=i_ground where i_ground was initialized to 0 and never changed. The first iteration of the for i=0:k_max-1 loop has i of 0, which would fail the if i~=0 test, so the first iteration of the for i=0:k_max-1 loop would skip the body of the if statement, after which you would be finished with the iteration for i=0 and would loop back and proceed with the i=1 iteration. That time, if i~=i_ground is true because 1~=0 so the body of the if statement will be run, leading to contour() being called. That contour call will go looking for an active axes and will find it -- the same one that the imagesc() was called in; it would draw in there. You would continue this process for the rest of the for i iterations, drawing several contours in that active axes (again, probably in figure 1, but that is not certain.) At no point in this process has if is_zero had any influence as to whether graphics are drawn or not or where there are drawn.
Eventually the for i=0:k_max-1 loop terminates.
After the for i loop has been run completely, figure(2) is called. If there is no figure 2 existing then a figure 2 is created and made the active figure; otherwise an existing figure 2i s made the active figure.
At this point, imagesc(img) is called. That is going to look for a current axes in the current figure, which is figure 2. If figure 2 had no current axes then an axes will be created and made the current axes. The imagesc() will be run in the current axes, drawing in figure 2.
Notice this drawing into figure 2 is done unconditionally provided that we passed the test about img being grayscale (and if we did not pass that test then no graphics are drawn at all.) Whether is_zero is true or not has no effect on whether figure 2 is drawn into.
So, provided that im was grayscale, we have drawn into whatever the active figure was (creating a figure if needed), which would commonly be figure 1 but not always. And then we specifically drew into figure 2.
Now there is an important subtle point to notice: provided that im was grayscale, then the last figure we drew into was figure 2. And that means that after the code, figure 2 is the active figure.
Why does this matter? Well, suppose we run the code again without deleting any figures. We get to the
imagesc(im); axis off; hold on; colormap gray;
line that earlier we said would probably go into figure 1 but not always. In particular we said it will go into the current axes of the current figure if there is a current figure. Well, the second time running the code, the current figure is figure 2, so no matter where the code drew to the first time around, the second time you run it, it is going to draw into figure 2, the same figure that is drawn into by the imagesc(img); axis off; It so happens that hold on is left in effect after the for i loop that creates the contours, so the imagesc(ig) is going to draw on top of all of those contours.
None of this depends upon any if statement other than whether im is grayscale or not. What it does depend upon is the fact that you left yourself defaulting to the current figure and current axes for the imagesc(im) and contours instead of having specifically selected a particular figure and particular axes.