Simple use case for self-modifying programs
I didn't write any myself, but I encountered several self-modifying MS-DOS batch files (this is possible due to the OS closing the batch file before executing each line), especially during the $1980$'s. I'm not sure if batch files fit within your definition of &quot;programs&quot;, or if this usage was from too long ago since the needs &amp; reasons for this (i.e., primarily due to only a minimal amount of memory being available) don't generally apply now to modern computers. Nonetheless, having batch files modify themselves is a relatively simple concept, and was similarly done for fairly basic reasons as well, so this might make good examples for teaching purposes.
I recall self-modifying batch files were used in several situations, but the main one was to change options based on user feedback. Wikipedia's Self-modifying code gives an example where a batch file runs a program that changes the file itself to next run what the user requested. However, I also recall seeing &amp; using a batch file with a fairly complicated (at least for a batch file) menu type system, with the commands directly modifying the file itself based on user feedback, return codes from the menu and/or other programs which were run. I no longer have a copy of this batch, or any other related, files. As this was over $30$ years ago, I don't recall any other details. Also, I haven't been able to find any such similar example online. Nonetheless, in Changing a batch file when its running, several answers, such as the answer by dbenham and the answer by jeb, give other examples of direct self-modifying batch files.
Note that, as the Wikipedia article section states, these batch file self-modifications were done primarily due to how limited memory generally was in personal computers back then. Most, if not all, of the other reasons people wrote &amp; used self-modifying batch files back then, as I recall, were also due to these significant memory limitations.