subtracting points on waves

I've written some short code for subtracting values of one wave from the next. The code does what I want except for the fact that I'd like to subtract the second point of wave 2 from the first point of wave 1 and so on such that I would have a third result wave with 1 less value than wave2. So far the code works until it gets to the last point which is spitting out 0 but which i'd like to be a Nan-- the algorithm should stop subtracting so the values of wave val should be 4,0,-1,8 and that's it.

function go()


wave wave2
wave wave1
wave val    
make /O/N = (numpnts(wave2)) val
variable x = 1
variable p = 0
print "val has ", numpnts(wave2)

do  

    val[p] = wave2[x] - wave1[p]
       
   

// if (x >= (numpnts(wave2)) ||  p >= (numpnts(wave1) -1))

if (x == numpnts(wave2) ||  p == (numpnts(wave1) -1))
    val[numpnts(wave2)] = NaN
else
    break
endif
        x+=1
        p+=1

        while (1)
       

end
 

               
subtracting_consecutive numbers.pxp (48.1 KB)
Here you go:
function subtract()
wave wave2
wave wave1
   
make /O/N = (numpnts(wave2)-1) val // one point shorter
val = wave2[p] - wave1[p+1]

//if you really want an additional point with 'NaN', uncomment the next two lines
//redimension/n=(numpnts(val)+1) val
//val[numpnts(val)-1]=NaN
end

You could also execute the middle two lines on the command line without needing a function for this operation. I do not know how experienced you are in Igor programming, but it seems to me like some important concepts of wave assignment operations are still somewhat unclear. I recommend you to read the help chapter about this by executing DisplayHelpTopic "Waveform Arithmetic and Assignment"