• I am going to offer an alternative for you, that I think is a lot more readable/understandable.

    First, note the DATEFROMPARTS() function, which you would use since you're just feeding in a year and month, that's new as of 2012. Secondly, the idea is that you only really need to find the first Monday (or whatever day we're looking for), if you want the next one then just add a week. Third, since you're using a zero-based array for @DOW, the DATEPART(dw,@1st) function will not equal zero ever, so we have to adjust @DOW when comparing.
    SET DATEFIRST 7 --week starts with Sunday, default for English, but set to make sure
    DECLARE
     @Year SMALLINT = 2018
    , @Month TINYINT = 2 -- 1-12
    , @DOW TINYINT = 0 -- Sunday = 0, Monday = 1, ect...
    , @Itr TINYINT = 1 -- iteration of specified DOW
    BEGIN
     DECLARE @1st DATE = DATEFROMPARTS(@Year,@Month,1)
     DECLARE @RtrnDt DATE
     --if our day of the week for the 1st is the same as we are looking for, then return it
     --we can find the next same day of the week by adding a week to that day
     IF DATEPART(dw,@1st) = (@DOW + 1) --adjusting to accomodate zero-based array
     BEGIN
      SET @RtrnDt = DATEADD(ww,@Itr-1,@1st);
     END
     --if our @DOW is later in the week, offset from the 1st by the number of days larger and add in our week iterator
     ELSE IF DATEPART(dw,@1st) < (@DOW + 1)
     BEGIN
      SET @RtrnDt = DATEADD(ww,@Itr-1,DATEADD(dd,(@DOW+1)-DATEPART(dw,@1st),@1st));
     END
     ELSE -- the @DOW is earlier in the week, subtract the value from the 1st and add in week iterator
     BEGIN
      SET @RtrnDt = DATEADD(ww,@Itr-1,DATEADD(dd,DATEPART(dw,@1st)-(@DOW),@1st));
     END
     SELECT @RtrnDt;
    END

    Hope that helps if you can't get your math working.

    -------------------------------------------------------------------------------------------------------------------------------------
    Please follow Best Practices For Posting On Forums to receive quicker and higher quality responses