Answers.Body: <p>I, too, will give a somewhat orthogonal answer to your quest. I would suggest that you find a way to incorporate unit testing into your Java based course with Unit. You can give them tests or require that they write tests. You can require an explanation of them when tests fail. </p>
  
  <p>But if you incorporate testing deeply into your teaching and requirements (<a href="https://en.wikipedia.org/wiki/Test-driven_development" rel="nofollow noreferrer">Test First - Test Driven Development</a>) these sorts of misconceptions should start to disappear. </p>
  
  <p>If you demonstrate code to your class live with some projection equipment you can demonstrate such things to good effect. </p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-10T10:21:17.243Z
Answers.Text: Thanks for the answer, it has its place, but it's not adequate for my specific case: my course is not Java, we have no classes (fortunately!), we need just a very very basic intro to the world of computing. The pupils had a taste of COBOL written with C syntax and are addict to printf before computing. I showed them Javascript to work more gently with arrays, but they have a really hard time understanding what do I mean for "working with arrays": there is no output print and they are lost. I think adding the complexity of testing is a bit too much for people who cannot understand a for loop.
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-10T10:25:52.197Z
Answers.Text: Hmmm. You mentioned Java in a comment. There are similar unit testing frameworks for most languages, actually.
Answers.UserDisplayName: Buffy
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-10T10:41:12.240Z
Answers.Text: I don't think I mentioned Java, I may be in error, but it may also be that I can be confused with someone else, my name does not seem designed for identification tasks :) Anyway, no probl. The more discussions I can see the happier I am. I'm starting to give up with this class because besides the technical problems they are also very demotivated and this complicates things even more: they sort of demand that I just write code while they do nothing with their brains, probably I'll end up with this.
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-08T21:27:31.720Z
Answers.LastActivityDate: 2019-03-08T21:27:31.720Z
Answers.UserDisplayName: Buffy
Answers.Body: <p>I get the feeling that we are teaching in the wrong order. We should teach functional first.</p>
  <p>In your first bullet point, it looks like you students are trying to invent a list comprehension/mapper. It may be easier to teach them how to do it this way that to teach how to implement it procedurally.</p>
  <p>The 2nd point is just missing that there is more that one input.</p>
  <p>The 3rd I would introduce <code>foreach</code> loops before general purpose loops.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-13T23:01:47.670Z
Answers.LastActivityDate: 2022-04-13T23:01:47.670Z
Answers.UserDisplayName: ctrl-alt-delor
Answers.Body: <p>Is programming being taught from an abstract perspective?  Some students when taught from an abstract perspective seem to treat statements as magic incantations that cause the computer to do something without seeing that there is an underlying structure to the language that is imposed by the nature of the hardware.  If students have some understanding of how computers actually work and how programming languages match up with capabilities of the hardware, then they are more likely to try and see underlying meaning in the statements comprising the program.  This will mean their underlying mental model will be more correct and their intuition about what will happen will be better.</p>
  <p>I don't think this is a problem with imperative langauges, but an abstract/practical issue.  Some students like an abstract perspective, others can't deal with it at all, so no &quot;one size fits all&quot; strategy will be effective.</p>
  <p>I like @buffy's suggestion to use testing, as to test effectively you need to be able to predict what the code <em>should</em> do to make the test cases, which encourages students to understand the code.  Getting students to do bug fixing is also useful, as again that involves looking at code and trying to understand why it is doing what it is doing, in order to work out how to fix it.  The coursework I set for my second year university C++ class involves writing code, testing, bug fixing and refactoring, and it seems to work quite well.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-04-07T11:15:14.023Z
Answers.LastActivityDate: 2022-04-07T11:15:14.023Z
Answers.UserDisplayName: Dikran Marsupial
Answers.Body: <p>It sounds like the program of study has a few bugs if 4th or 5th year students cannot reason about a loop.</p>
  
  <p>I taught myself programming in 9th grade by writing simple programs that printed shapes on the screen using characters. For example, make a solid rectangle. Now modify the program to make it a hollow rectangle. Accept input to get the width and height, etc. Now draw triangles. Now diamonds scrolling up the screen. Now make them hollow...</p>
  
  <p>Rather than fight against scanf and printf, use them in a way that is direct, gives immediate feedback, and is somewhat interesting.</p>
  
  <p>You can introduce functions by taking the inner loop and making it into a "draw a line" function, with a variable length, offset and hollowness. Then build another function that makes a shape by calling the first function in a loop.</p>
  
  <p>If they can't handle functions, nested loops and conditions after that, you have my permission to yield to despair.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T02:24:13.733Z
Answers.Text: I also was a Teaching Assistant for a "programming 101" course for non-computer majors, back in 1987. Same approach... "What does this line of code do? Now what does the next line do?" Some mastered it quickly, some struggled, some were uninterested. Pouring water on a horse does not result in people learning to program. Apples work though.
Answers.UserDisplayName: Scott Rowe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T09:00:07.903Z
Answers.Text: Yes! Thanks! Sounds like a great approach, I hope I can try it in the short remaining time span.
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T09:22:09.960Z
Answers.Text: Mmh, thinking about it again, maybe what they struggle with the most is not the concept of loop, it's that of mutation. I mean, they have this idea that things in `for` are going to be repeated, but probably they have no idea how: maybe they think "I want repetition -> I write for", but without understanding. They are so much confused that even I don't understand where is the confusion :D [...]
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T09:22:18.117Z
Answers.Text: The other day something shocked me: a girl supposed to be quite good was tracing a loop with my supervision and at the moment of `i++` she added a new line at the bottom with the new `i`, I tried to convince her that it would be more precise to update the `i` where it was, but she resisted a lot and thought there's no difference in updating it or adding one new. Am I wrong wanting them to mutate? Is there value in her method?
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T10:57:47.410Z
Answers.Text: The splendor and mystery of *Mutation*. For some reason I never experienced e dreaded confusion about what exactly it is or how to use it in a program, but I have read that it is a major stumbling block. To be fair, a *variable* is unlike any other thing in the entire universe, you can just *change its value* any time you want. Noting else in our world works that way. When I was in 8th grade and a teacher was explaining programming, he drew a rectangle on the board, wrote 'X' next to it and said, "this is a variable." Then he wrote 7 in it. Then he added one to X, erased the 7 and wrote 8. --
Answers.UserDisplayName: Scott Rowe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T11:03:34.947Z
Answers.Text: -- Apparently we are too proud of ourselves these days to begin the explanation at this low level. But I never had any question in my mind what a variable was, and when people said that *mutation* was confusing to students, I was like, yeah, like a doorknob is confusing... So when I encountered the dreaded concept, the horrific terror if *Indirection* the next year, it was not much of a stretch. **Oh, a variable that holds an address.** We can increment it, etc. But I get flack from people on this site about teaching this way. **I learned this way. It works.**
Answers.UserDisplayName: Scott Rowe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T23:40:46.613Z
Answers.Text: I feel exactly like that: oh this can hold a value, oh now I change it. It's so damn simple I still cannot understand the least how this may be confusing for the students. But the evidence says it is, I would be so glad could I have a clue why, but I don't. All I know how to do is tracing the execution, "drawing memory". It's easy, clear, effective. But they don't do it, they weren't taught to do it, and they are scared that I want them to do it. More than showing it the same crystal clear way as your `x=7; x++;` I don't know what could I do. I can't understand what's the difficult point.
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T23:52:52.410Z
Answers.Text: Probably this is a case where the major difficulty is unlearning a confused way of writing mindless random things without any reason more than learning a concept that would clarify things but that requires thought energy. Btw another strange thing I see in younger students (to their defense it's the first time they see anything related to cs) is that they imagine the code do what they have in their minds instead of what's written. They give personality and force of will to the code. Also here it's quite difficult for me to understand why.
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-21T20:48:56.527Z
Answers.Text: I didn't have any problem with mutation, either. But I learned programming long before I learned algebra. Today, many students start programming after algebra and perhaps that is part of the problem. There is a confusion between programming notation and mathematical notation. Some students think that writing `y=a+3` means that the value of y will always be three more than a, so if you change a, y changes too. In algebra, `=` is an assertion of equality. In programming, it's mutation. Some students have to be told this explicitly.
Answers.UserDisplayName: Barry Brown
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-21T22:56:24.810Z
Answers.Text: @BarryBrown yes, I have heard about this confusion too. I recall a girl saying, "*How can x ever equal x plus 1?*" It took me a moment to figure out what on earth she was saying. But if you drew a box on the board and wrote in it, then it seems obvious that a and y are not magically linked. People are not learning in a way that makes it obvious what is happening, which means that teachers are not writing on the board and uttering the correct words. It is not a magic spell, it is just explanation. The idea of Functional Programming will only increase this confusion.
Answers.UserDisplayName: Scott Rowe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T02:21:01.460Z
Answers.LastActivityDate: 2019-03-15T02:21:01.460Z
Answers.UserDisplayName: Scott Rowe
Answers.Body: <p>My advice to all of these problems is the same. Your students do not know how to draw out memory. This is a basic technique that most people who have been programming for a while do mentally, but that nearly all students need to be explicitly taught and shown on paper. </p>
  
  <h2>How to Draw Memory</h2>
  
  <p>There is a good chance that you have seen something like this before. Maybe when you were learning to program, or maybe when watching another instructor teach a beginner class. Drawing memory is about physically (with pencil or chalk) drawing a diagram of the state of the program's memory. For beginner programmers, some things are typically abstracted away, such as the idea of a register and the specifics about how memory is loaded and stored.</p>
  
  <p>I am going to create a short video tutorial, showing how I personally draw memory, including how I draw loops and functions, but here are some basics in text form:</p>
  
  <ul>
  <li>Variable is declared => I write the name of the variable and draw a box with a slash through it to signal that it is Null.</li>
  <li>The Variable is defined => I erase the slash and write the value in the box. If the variable is ever reassigned, the value is erased, and the new value is written in its place.</li>
  <li>The Variable is a pointer => the box contains an arrow to another, larger box that holds the data.</li>
  <li>Loop => I draw loops (and other smaller scope constructs) as circles or "bubbles." At the top are the condition and an iterator value if it's a for loop (the iterator value is drawn like any other variable). An arrow points from the bottom of the loop to the condition. All locally scoped values are drawn inside the circle, and the whole thing is erased once the loop finishes.</li>
  <li>Function => I draw functions as large blocks. At the very top, any parameter values are placed in their own box that is linked with an arrow. All locally scoped values, loops, etc. are drawn inside the function block, and the output is linked to the bottom of the box. I typically do not erase function boxes since they are likely to be used again, but you can if you wish.</li>
  <li>Print Statements => I find it helpful to have a three-board view when drawing memory. One display contains the code, which we will go through line-by-line. The next display contains my drawing, an abstract representation of what the program "knows." The third contains the text output. This helps students visualize the distinction between what is happening and what is displayed to the console.</li>
  </ul>
  
  <h2>How to Teach Drawing Memory</h2>
  
  <p>In lecture, you should draw memory constantly. Any time you review what a piece of code does, you should draw it out with and for your students.</p>
  
  <p>Since your students already sort-of know some basics, you will likely need to go back and explicitly teach them how to draw. Talk to them about why it's important to understand what the program knows at any given time and how it flows through lines of code, and ask them to brainstorm some ways to keep track of that.</p>
  
  <p>Then, show them the basics for a very simple program that they understand. Something that declares and defines two variables and uses an if statement. Have them practice the drawing structure in small groups or pairs on a slightly different problem. Switch gears back to your normal lecture topic. The next lecture, have your students tell you how to draw yet another variant on the same few lines of code. If they can do that, introduce the next drawing construct, maybe a while loop. Repeat this slow and methodical process over the course of several lectures until your students are able to draw anything they can code. Then, when you introduce a new concept, you should show them how to draw and visualize it as part of the initial learning.</p>
  
  <p>The goal is to make drawing second nature so that they do it when they hit a bug or look at code that is not their own. Tell them that eventually, they will be good enough at drawing that they can do it in their heads, but that for now, they should always do it on paper or a board so that they can practice. Until they are at least intermediate programmers, you should encourage or even mandate that your students draw the state of memory every time they approach a problem.</p>
  
  <p>As you teach them this important skill, you should have them practice on code that works and on code that does not work. Give them something with a bug- a one-off error, or a logical issue. Tell them to draw it out or do it with them on the board. Once they are done, ask them to tell you what the issue is and how to fix it. Have them draw it again once it's fixed.</p>
  
  <p>Once they know how to draw, you can ask them to do so in all sorts of situations. Put it on a quiz or test. Require a memory drawing before helping with a bug in their projects. Make sure your TAs do the same. Have them continue to draw memory during class, and encourage them to draw out tricky parts of assignments as they work on them.</p>
  
  <h2>How Drawing Solves Your Students' Confusion</h2>
  
  <p>You gave three specific examples of holes in understanding. Drawing solves all three.</p>
  
  <blockquote>
    <p>They are convinced that for (i=0; i&lt;5; i++) return i is going to return 5 results. They are obviously so much confused in thinking that they're doing a minor variation of for (i=0; i&lt;5; i++) printf("%d\n", i).</p>
  </blockquote>
  
  <p>A drawing of a function only has one return value, and it is at the bottom. They will see clearly that once a value is returned, the function is over. Since they won't reach the return statement until the end, they will return once, the final value of i. Due to erasing past values, they will only see the most recent assignment.</p>
  
  <blockquote>
    <p>Given a function that needs X as input they constantly point out to me that somewhere in the first lines we need to read X from somewhere, they just ignore that we can add another argument as needed.</p>
  </blockquote>
  
  <p>A drawing of a function has input values included. In practicing drawing, you will demonstrate that those inputs can be used as defined variables.</p>
  
  <blockquote>
    <p>They cannot trace the execution of a for loop, they don't check the loop condition: to them it's just "I do the body N times", I don't yet know what do they think of the index used in the loop besides that it magically happens to take the values 0..N-1.</p>
  </blockquote>
  
  <p>Drawing the loop involves updating the iterator each time through. They will never forget again after drawing out a loop that updates 10+ times.</p>
  
  <p>I hope this helps, I will try to upload a rough video tutorial in the next few days and will update my answer with a link.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T22:47:11.633Z
Answers.Text: I completely agree with you, drawing the memory is the best thing I know for understanding imperative (stateful) code, simply because drawing the memory amounts to **seeing** the states transitions that are described by the code fragment. The problem here is that I cannot really do it in short time, I was hoping for something of strong effect: it's a 5th year class that has been trained for the past 4 to use global variables for everything and to emit swarms of printf in place of computing results. In my opinion this is about the idea of brain damage considered by ED. Am I wrong?
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T22:52:24.710Z
Answers.Text: Looking forward to check out the video anyway, might be helpful! Possibly even just letting them see that I am not the only one to draw on a blackboard what the code does when executed. They think I'm kind of crazy for forbidding them the use of prints, and it's quite frustrating.
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T02:09:33.737Z
Answers.Text: The term I always heard for writing the values of variables as the code is hand-evaluated is **Desk Checking**. I guess the idea was that people didn't formerly have computers sitting on their desks. They simulated the computations with pencil and paper. If someone cannot do this, they cannot program, because they cannot understand the code.
Answers.UserDisplayName: Scott Rowe
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-15T09:35:42.220Z
Answers.Text: Yes, Scott. Can't agree more. My 5th year students told me they last did something similar 3 years ago, not often and not reinforced, I have no idea how they did so but I'd guess not in an effective way because it's not second nature for them, worse it was still difficult to show them how I'd do it and they have no will in following this method. I could accept they consider it boring were they proficient, but clearly they aren't.
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-14T04:41:04.870Z
Answers.Text: Late comment: I try to emphasize drawing. Over the years (15+) I've realized that this is not unproblematic. I'll highlight: "the value is erased" on re-assignment; while it's the most accurate model, it has the downside of not leaving a record or documentation of what's happened to date (for students studying later or backtracking or "go over that gain?"). So we need to judge the options of (1) erasing state, (2) crossing out one and writing the new one next to it, (3) keeping a table with one row per iteration and state of all variables at each step...
Answers.UserDisplayName: Daniel R. Collins
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-12-14T04:45:01.507Z
Answers.Text: ... Likewise if we're going to put this on a quiz or test (and we basically need to), then to verify understanding, we likely need more documentation than just a box with the final value as the submitted solution. So (partly based on a senior professor critiquing me on this point), I've shifted to crossing-out for simple things, and a full iterated state-table for more complex algorithms (e.g., binary search, BFS, Prim's or Dijkstra's algorithms, etc.). As a side note in 2020, using online digital tools, it's generally a lot easier to write new symbols than select and erase old ones.
Answers.UserDisplayName: Daniel R. Collins
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T21:58:44.553Z
Answers.LastActivityDate: 2019-03-07T21:58:44.553Z
Answers.UserDisplayName: Green Griffin
Answers.Body: <p>I recognize the feeling of despair!  However, I don't think that you want what you are asking for.  The most shocking bit of code that I know to show students is the code I wrote up <a href="https://stackoverflow.com/questions/27308336/an-interesting-bit-of-reflective-code-in-java">here</a>.  Beware, it's quite strange!  If you want a bit of fun, don't look at my explanations.  Just copy the code into an IDE, look it over, make a guess about what it will do, and run it.</p>
  <p><strong>I don't think that this will help you with your students, however!</strong></p>
  <p>What you're pointing to are what I call &quot;cognitive traps&quot;, and they must be addressed very specifically during instruction.  You're already on the right track in that you are spotting them, but I don't think you'll make a lot of headway trying to find a single, unified solution.</p>
  <h1>The problem with being an expert</h1>
  <p>There is no one key understanding that will, for a novice, unify everything into a sensible framework.</p>
  <p>There's actually a reason for this, which I previously wrote about in my answer <a href="https://cseducators.stackexchange.com/questions/5403/students-seem-to-be-unable-to-use-code-to-express-their-ideas-is-the-teaching-m/5429#5429">here</a>. In essence, overarching frameworks are the toolsets of experts.  Factoids and small systems are the toolsets of novices.  The way that novices become experts is typically by learning a set of small systems, and then in later revisitings, seeing how they are actually similar ideas.</p>
  <p>Experts often wish to teach novices to see the big system, and show how all of the smaller systems operate under the same principles.  If we could only get them to think about it the way we do, they would be able to work so fluidly!  Principle to small systems is a top-down method of learning.</p>
  <p>An alternative is to move bottom up: here, look at these three, apparently separate ideas.  Now, let's link them together!  While I am very impressed with @GreenGriffin's answer, I believe that this is the approach that they are taking here.</p>
  <p>I would contend (and I know that our top user, Buffy, has also said this many times) that when we learn deeply, we actually do it in a spiral: we revisit an idea many times, each time enriching it by linking it to other ideas.  This fits nicely with our current understanding of <a href="https://www.nap.edu/read/9853/chapter/8" rel="nofollow noreferrer">learning in the brain</a>, in which information is physically structured within the brain as a pattern of interconnected neurons, and new information is learned by linking new ideas to old ones that we have already mastered.</p>
  <h1>Getting practical</h1>
  <p>So, what's a teacher to do?  First, with regards to the three very specific things you've mentioned, explicitly call attention to them when you are presenting function calls and loops.  They are traps that students can fall into, and you want to head them off at the pass.  Give them a chance to practice doing it correctly immediately, and demonstrate what happens when they do it wrong.  The longer you teach, the more such traps you'll discover and can thus help student avoid.  That's part of becoming a better teacher.</p>
  <p>And as for the large systems, revisit older ideas and keep linking them to new ideas as you teach them.  Ask students to reflect directly on how this idea and that idea are similar.  Ask students to make a drawing of how a loop iterator actually operates.  The more that we help students link ideas together, the more abstractly they'll be able to work.</p>
  <p>It's not a lightning-fast quick fix, but you will see improvement if you keep making improvements!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T23:14:22.780Z
Answers.Text: Yes! Definitely agree. (Besides having yet to see the java code :) ). For what I can I am constantly trying to show them small pieces and their connections. Problem is that so many times I already have shown them that a function cannot return many values, how a for translates to a while, how the variables are updated, they just kind of don't want to believe it, and their motivation is really 0. They're used to these crazy I/O operations: oh we need to know how many iterations, let's scanf something! I was even starting wondering: is this what cobol was made of? What's this thing?
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T23:21:34.773Z
Answers.Text: Unfortunately I cannot introduce them functions and loops, they've already been introduced in previous years, they shouldn't even be so much unaware of those concepts. My goal was having them build small real algorithms, let's say for linear algebra, I have already given up on this actually.
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T23:28:28.380Z
Answers.Text: When I mentioned the shocking effect I was thinking something so short as a for (i=0; i<5; i*=2). Just I don't think I am producing examples of good interest and of the right degree between too trivial and too crazy. Has anyone an arsenal of things of this kind? Btw, has anyone else noticed that learning steps in CS seem to be wider than other disciplines?
Answers.UserDisplayName: user9137
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-03-07T22:57:02.960Z
Answers.LastActivityDate: 2019-03-07T22:57:02.960Z
Answers.LastEditDate: 2020-06-18T08:36:57.023Z
Answers.UserDisplayName: Ben I.
Body: <p>I'm becoming more and more convinced that starting with an imperative attitude is not doing a favor to the students, especially if it's all about scheduling and repeating some <code>scanf</code> and <code>printf</code> in loops. I ignore if this teaching technique has any systematic treatment or evidence of efficacy. To me it seems that it produces students unable to process data, as I'll explain soon.</p>
  <p>I have a class of students who show the following features, for which I'm unable to find a good framework besides listing them (but perfectly and angrily knowing where they come from):</p>
  <ul>
  <li><p>They are convinced that <code>for (i=0; i&lt;5; i++) return i</code> is going to return 5 results. They are obviously so much confused in thinking that they're doing a minor variation of <code>for (i=0; i&lt;5; i++) printf(&quot;%d\n&quot;, i)</code>.</p>
  </li>
  <li><p>Given a function that needs X as input they constantly point out to me that somewhere in the first lines we need to read X from somewhere, they just ignore that we can add another argument as needed.</p>
  </li>
  <li><p>They cannot trace the execution of a for loop, they don't check the loop condition: to them it's just &quot;I do the body N times&quot;, I don't yet know what do they think of the index used in the loop besides that it magically happens to take the values 0..N-1.</p>
  </li>
  </ul>
  <p>I'm on the edge of despair. Just showing them how I'd do something and explaining its operational interpretation obviously doesn't work, they even show a sort of resistance to abandon this method of doing any imaginable thing just by throwing <code>scanf</code>'s and <code>printf</code>'s around. Do you have any suggestions for better framing their difficulties, or any examples that might be illuminating in this situation?</p>
  <p>I am finding incredibly difficult to produce some wrong code that might appear correct in their eyes and could possibly shock them and making them understand that things are a bit different from what they believe. Any ideas?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-07T17:30:00.197Z
Comments.Text: The problem isn't that they misunderstand the code; rather, it's they have an incomplete (or incorrect) model of computation. They don't see memory as an array of cells, nor do they understand how the code gets executed. Flowcharts might help. Use a visual debugger that lets you see the call stack and variables. Try avoiding the "for" loop until they've mastered the "while" loop, which makes its steps more explicit.
Comments.UserDisplayName: Barry Brown
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-07T18:21:28.763Z
Comments.Text: Sort of yes, sure. They absolutely lack a model of computation and that's arguably a major point, how can you teach to write code without the least of models of computation? I recall the very first thing at my univ was: these are our building blocks, it was a minimal von Neumann model, but any would do, except the non-existent one. The problem is that this is what I think, not what was thought by the ones who initiated my pupils, and I cannot change their history, just trying to reshape their models, dealing with their resistance and below ground motivation in the meantime.
Comments.UserDisplayName: user9137
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-08T01:18:57.143Z
Comments.Text: What about the mandelbrot set? It's remarkably simple, but nonalgebraic, and the output makes nice T-shirts
Comments.UserDisplayName: pojo-guy
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-09T11:25:20.937Z
Comments.Text: What language are you using?
Comments.UserDisplayName: ctrl-alt-delor
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-10T10:30:54.853Z
Comments.Text: Javascript. They saw C syntax and I chose js to be more gentle with arrays. They would benefit a lot from some scheme but I couldn't afford such a big leap: I would have seen them drowning in parentheses. And my (imposed) target was: implementation of linear algebra algorithms.
Comments.UserDisplayName: user9137
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-15T02:31:46.197Z
Comments.Text: Just out of curiosity (bad motivation, sorry) where is this happening? I have trouble imagining students going through years of instruction and not wanting to learn. Why don't they just drop out, or switch programs of study? Programming is quite demanding, it is not something you can do for a job if you don't enjoy it and have developed the ability to learn on your own.
Comments.UserDisplayName: Scott Rowe
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-15T08:56:28.693Z
Comments.Text: Scott: at school of course ;)
Comments.UserDisplayName: user9137
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2019-03-15T11:10:52.120Z
Comments.Text: Based on your comments to my answer, you won't be able to 'shock' them because they don't have enough correct understanding in place to have their assumptions violated. They need concepts first.
Comments.UserDisplayName: Scott Rowe
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2022-04-07T12:02:18.737Z
Comments.Text: "They cannot trace the execution of a for loop, " I show my first year (university) Java class how to use the debugger tool to step through the code, and I got a fair bit of good feedback from students saying that it really helped them understand what was going on.
Comments.UserDisplayName: Dikran Marsupial
Tags: <lesson-ideas><code-example>
Title: Examples of innocent looking faulty code for shock effect
ThreadText: # Examples of innocent looking faulty code for shock effect
  
  (Asked by: username_0 on 2019-03-07)
  
  I'm becoming more and more convinced that starting with an imperative attitude is not doing a favor to the students, especially if it's all about scheduling and repeating some `scanf` and `printf` in loops. I ignore if this teaching technique has any systematic treatment or evidence of efficacy. To me it seems that it produces students unable to process data, as I'll explain soon.
  
  I have a class of students who show the following features, for which I'm unable to find a good framework besides listing them (but perfectly and angrily knowing where they come from):
  
  * They are convinced that `for (i=0; i<5; i++) return i` is going to return 5 results. They are obviously so much confused in thinking that they're doing a minor variation of `for (i=0; i<5; i++) printf("%d\n", i)`.
  * Given a function that needs X as input they constantly point out to me that somewhere in the first lines we need to read X from somewhere, they just ignore that we can add another argument as needed.
  * They cannot trace the execution of a for loop, they don't check the loop condition: to them it's just "I do the body N times", I don't yet know what do they think of the index used in the loop besides that it magically happens to take the values 0..N-1.
  
  I'm on the edge of despair. Just showing them how I'd do something and explaining its operational interpretation obviously doesn't work, they even show a sort of resistance to abandon this method of doing any imaginable thing just by throwing `scanf`'s and `printf`'s around. Do you have any suggestions for better framing their difficulties, or any examples that might be illuminating in this situation?
  
  I am finding incredibly difficult to produce some wrong code that might appear correct in their eyes and could possibly shock them and making them understand that things are a bit different from what they believe. Any ideas?
  
  **Tags:** lesson-ideas, code-example
  
  > **Comments:**
  > **username_1**: The problem isn't that they misunderstand the code; rather, it's they have an incomplete (or incorrect) model of computation. They don't see memory as an array of cells, nor do they understand how the code gets executed. Flowcharts might help. Use a visual debugger that lets you see the call stack and variables. Try avoiding the "for" loop until they've mastered the "while" loop, which makes its steps more explicit.  
  > **username_0**: Sort of yes, sure. They absolutely lack a model of computation and that's arguably a major point, how can you teach to write code without the least of models of computation? I recall the very first thing at my univ was: these are our building blocks, it was a minimal von Neumann model, but any would do, except the non-existent one. The problem is that this is what I think, not what was thought by the ones who initiated my pupils, and I cannot change their history, just trying to reshape their models, dealing with their resistance and below ground motivation in the meantime.  
  > **username_2**: What about the mandelbrot set? It's remarkably simple, but nonalgebraic, and the output makes nice T-shirts  
  > **username_3**: What language are you using?  
  > **username_0**: Javascript. They saw C syntax and I chose js to be more gentle with arrays. They would benefit a lot from some scheme but I couldn't afford such a big leap: I would have seen them drowning in parentheses. And my (imposed) target was: implementation of linear algebra algorithms.  
  > **username_4**: Just out of curiosity (bad motivation, sorry) where is this happening? I have trouble imagining students going through years of instruction and not wanting to learn. Why don't they just drop out, or switch programs of study? Programming is quite demanding, it is not something you can do for a job if you don't enjoy it and have developed the ability to learn on your own.  
  > **username_0**: username_4: at school of course ;)  
  > **username_4**: Based on your comments to my answer, you won't be able to 'shock' them because they don't have enough correct understanding in place to have their assumptions violated. They need concepts first.  
  > **username_5**: "They cannot trace the execution of a for loop, " I show my first year (university) Java class how to use the debugger tool to step through the code, and I got a fair bit of good feedback from students saying that it really helped them understand what was going on.  
  
  ---
  
  ## 6 answers
  
  ### Answer by username_4 on 2019-03-15. Score: 5 | Accepted answer
  
  It sounds like the program of study has a few bugs if 4th or 5th year students cannot reason about a loop.
  
  I taught myself programming in 9th grade by writing simple programs that printed shapes on the screen using characters. For example, make a solid rectangle. Now modify the program to make it a hollow rectangle. Accept input to get the width and height, etc. Now draw triangles. Now diamonds scrolling up the screen. Now make them hollow...
  
  Rather than fight against scanf and printf, use them in a way that is direct, gives immediate feedback, and is somewhat interesting.
  
  You can introduce functions by taking the inner loop and making it into a "draw a line" function, with a variable length, offset and hollowness. Then build another function that makes a shape by calling the first function in a loop.
  
  If they can't handle functions, nested loops and conditions after that, you have my permission to yield to despair.
  
  > **Comments:**
  > **username_4**: I also was a Teaching Assistant for a "programming 101" course for non-computer majors, back in 1987. Same approach... "What does this line of code do? Now what does the next line do?" Some mastered it quickly, some struggled, some were uninterested. Pouring water on a horse does not result in people learning to program. Apples work though.  
  > **username_0**: Yes! Thanks! Sounds like a great approach, I hope I can try it in the short remaining time span.  
  > **username_0**: Mmh, thinking about it again, maybe what they struggle with the most is not the concept of loop, it's that of mutation. I mean, they have this idea that things in `for` are going to be repeated, but probably they have no idea how: maybe they think "I want repetition -> I write for", but without understanding. They are so much confused that even I don't understand where is the confusion :D [...]  
  > **username_0**: The other day something shocked me: a girl supposed to be quite good was tracing a loop with my supervision and at the moment of `i++` she added a new line at the bottom with the new `i`, I tried to convince her that it would be more precise to update the `i` where it was, but she resisted a lot and thought there's no difference in updating it or adding one new. Am I wrong wanting them to mutate? Is there value in her method?  
  > **username_4**: The splendor and mystery of *Mutation*. For some reason I never experienced e dreaded confusion about what exactly it is or how to use it in a program, but I have read that it is a major stumbling block. To be fair, a *variable* is unlike any other thing in the entire universe, you can just *change its value* any time you want. Noting else in our world works that way. When I was in 8th grade and a teacher was explaining programming, he drew a rectangle on the board, wrote 'X' next to it and said, "this is a variable." Then he wrote 7 in it. Then he added one to X, erased the 7 and wrote 8. --  
  > **username_4**: -- Apparently we are too proud of ourselves these days to begin the explanation at this low level. But I never had any question in my mind what a variable was, and when people said that *mutation* was confusing to students, I was like, yeah, like a doorknob is confusing... So when I encountered the dreaded concept, the horrific terror if *Indirection* the next year, it was not much of a stretch. **Oh, a variable that holds an address.** We can increment it, etc. But I get flack from people on this site about teaching this way. **I learned this way. It works.**  
  > **username_0**: I feel exactly like that: oh this can hold a value, oh now I change it. It's so damn simple I still cannot understand the least how this may be confusing for the students. But the evidence says it is, I would be so glad could I have a clue why, but I don't. All I know how to do is tracing the execution, "drawing memory". It's easy, clear, effective. But they don't do it, they weren't taught to do it, and they are scared that I want them to do it. More than showing it the same crystal clear way as your `x=7; x++;` I don't know what could I do. I can't understand what's the difficult point.  
  > **username_0**: Probably this is a case where the major difficulty is unlearning a confused way of writing mindless random things without any reason more than learning a concept that would clarify things but that requires thought energy. Btw another strange thing I see in younger students (to their defense it's the first time they see anything related to cs) is that they imagine the code do what they have in their minds instead of what's written. They give personality and force of will to the code. Also here it's quite difficult for me to understand why.  
  > **username_1**: I didn't have any problem with mutation, either. But I learned programming long before I learned algebra. Today, many students start programming after algebra and perhaps that is part of the problem. There is a confusion between programming notation and mathematical notation. Some students think that writing `y=a+3` means that the value of y will always be three more than a, so if you change a, y changes too. In algebra, `=` is an assertion of equality. In programming, it's mutation. Some students have to be told this explicitly.  
  > **username_4**: @username_1 yes, I have heard about this confusion too. I recall a girl saying, "*How can x ever equal x plus 1?*" It took me a moment to figure out what on earth she was saying. But if you drew a box on the board and wrote in it, then it seems obvious that a and y are not magically linked. People are not learning in a way that makes it obvious what is happening, which means that teachers are not writing on the board and uttering the correct words. It is not a magic spell, it is just explanation. The idea of Functional Programming will only increase this confusion.  
  
  ---
  
  ### Answer by username_7 on 2019-03-07. Score: 6
  
  My advice to all of these problems is the same. Your students do not know how to draw out memory. This is a basic technique that most people who have been programming for a while do mentally, but that nearly all students need to be explicitly taught and shown on paper.
  
  ## How to Draw Memory
  
  There is a good chance that you have seen something like this before. Maybe when you were learning to program, or maybe when watching another instructor teach a beginner class. Drawing memory is about physically (with pencil or chalk) drawing a diagram of the state of the program's memory. For beginner programmers, some things are typically abstracted away, such as the idea of a register and the specifics about how memory is loaded and stored.
  
  I am going to create a short video tutorial, showing how I personally draw memory, including how I draw loops and functions, but here are some basics in text form:
  
  * Variable is declared => I write the name of the variable and draw a box with a slash through it to signal that it is Null.
  * The Variable is defined => I erase the slash and write the value in the box. If the variable is ever reassigned, the value is erased, and the new value is written in its place.
  * The Variable is a pointer => the box contains an arrow to another, larger box that holds the data.
  * Loop => I draw loops (and other smaller scope constructs) as circles or "bubbles." At the top are the condition and an iterator value if it's a for loop (the iterator value is drawn like any other variable). An arrow points from the bottom of the loop to the condition. All locally scoped values are drawn inside the circle, and the whole thing is erased once the loop finishes.
  * Function => I draw functions as large blocks. At the very top, any parameter values are placed in their own box that is linked with an arrow. All locally scoped values, loops, etc. are drawn inside the function block, and the output is linked to the bottom of the box. I typically do not erase function boxes since they are likely to be used again, but you can if you wish.
  * Print Statements => I find it helpful to have a three-board view when drawing memory. One display contains the code, which we will go through line-by-line. The next display contains my drawing, an abstract representation of what the program "knows." The third contains the text output. This helps students visualize the distinction between what is happening and what is displayed to the console.
  
  ## How to Teach Drawing Memory
  
  In lecture, you should draw memory constantly. Any time you review what a piece of code does, you should draw it out with and for your students.
  
  Since your students already sort-of know some basics, you will likely need to go back and explicitly teach them how to draw. Talk to them about why it's important to understand what the program knows at any given time and how it flows through lines of code, and ask them to brainstorm some ways to keep track of that.
  
  Then, show them the basics for a very simple program that they understand. Something that declares and defines two variables and uses an if statement. Have them practice the drawing structure in small groups or pairs on a slightly different problem. Switch gears back to your normal lecture topic. The next lecture, have your students tell you how to draw yet another variant on the same few lines of code. If they can do that, introduce the next drawing construct, maybe a while loop. Repeat this slow and methodical process over the course of several lectures until your students are able to draw anything they can code. Then, when you introduce a new concept, you should show them how to draw and visualize it as part of the initial learning.
  
  The goal is to make drawing second nature so that they do it when they hit a bug or look at code that is not their own. Tell them that eventually, they will be good enough at drawing that they can do it in their heads, but that for now, they should always do it on paper or a board so that they can practice. Until they are at least intermediate programmers, you should encourage or even mandate that your students draw the state of memory every time they approach a problem.
  
  As you teach them this important skill, you should have them practice on code that works and on code that does not work. Give them something with a bug- a one-off error, or a logical issue. Tell them to draw it out or do it with them on the board. Once they are done, ask them to tell you what the issue is and how to fix it. Have them draw it again once it's fixed.
  
  Once they know how to draw, you can ask them to do so in all sorts of situations. Put it on a quiz or test. Require a memory drawing before helping with a bug in their projects. Make sure your TAs do the same. Have them continue to draw memory during class, and encourage them to draw out tricky parts of assignments as they work on them.
  
  ## How Drawing Solves Your Students' Confusion
  
  You gave three specific examples of holes in understanding. Drawing solves all three.
  
  > They are convinced that for (i=0; i<5; i++) return i is going to return 5 results. They are obviously so much confused in thinking that they're doing a minor variation of for (i=0; i<5; i++) printf("%d\n", i).
  
  A drawing of a function only has one return value, and it is at the bottom. They will see clearly that once a value is returned, the function is over. Since they won't reach the return statement until the end, they will return once, the final value of i. Due to erasing past values, they will only see the most recent assignment.
  
  > Given a function that needs X as input they constantly point out to me that somewhere in the first lines we need to read X from somewhere, they just ignore that we can add another argument as needed.
  
  A drawing of a function has input values included. In practicing drawing, you will demonstrate that those inputs can be used as defined variables.
  
  > They cannot trace the execution of a for loop, they don't check the loop condition: to them it's just "I do the body N times", I don't yet know what do they think of the index used in the loop besides that it magically happens to take the values 0..N-1.
  
  Drawing the loop involves updating the iterator each time through. They will never forget again after drawing out a loop that updates 10+ times.
  
  I hope this helps, I will try to upload a rough video tutorial in the next few days and will update my answer with a link.
  
  > **Comments:**
  > **username_0**: I completely agree with you, drawing the memory is the best thing I know for understanding imperative (stateful) code, simply because drawing the memory amounts to **seeing** the states transitions that are described by the code fragment. The problem here is that I cannot really do it in short time, I was hoping for something of strong effect: it's a 5th year class that has been trained for the past 4 to use global variables for everything and to emit swarms of printf in place of computing results. In my opinion this is about the idea of brain damage considered by ED. Am I wrong?  
  > **username_0**: Looking forward to check out the video anyway, might be helpful! Possibly even just letting them see that I am not the only one to draw on a blackboard what the code does when executed. They think I'm kind of crazy for forbidding them the use of prints, and it's quite frustrating.  
  > **username_4**: The term I always heard for writing the values of variables as the code is hand-evaluated is **Desk Checking**. I guess the idea was that people didn't formerly have computers sitting on their desks. They simulated the computations with pencil and paper. If someone cannot do this, they cannot program, because they cannot understand the code.  
  > **username_0**: Yes, username_4. Can't agree more. My 5th year students told me they last did something similar 3 years ago, not often and not reinforced, I have no idea how they did so but I'd guess not in an effective way because it's not second nature for them, worse it was still difficult to show them how I'd do it and they have no will in following this method. I could accept they consider it boring were they proficient, but clearly they aren't.  
  > **username_8**: Late comment: I try to emphasize drawing. Over the years (15+) I've realized that this is not unproblematic. I'll highlight: "the value is erased" on re-assignment; while it's the most accurate model, it has the downside of not leaving a record or documentation of what's happened to date (for students studying later or backtracking or "go over that gain?"). So we need to judge the options of (1) erasing state, (2) crossing out one and writing the new one next to it, (3) keeping a table with one row per iteration and state of all variables at each step...  
  > **username_8**: ... Likewise if we're going to put this on a quiz or test (and we basically need to), then to verify understanding, we likely need more documentation than just a box with the final value as the submitted solution. So (partly based on a senior professor critiquing me on this point), I've shifted to crossing-out for simple things, and a full iterated state-table for more complex algorithms (e.g., binary search, BFS, Prim's or Dijkstra's algorithms, etc.). As a side note in 2020, using online digital tools, it's generally a lot easier to write new symbols than select and erase old ones.  
  
  ---
  
  ### Answer by username_9 on 2019-03-07. Score: 4
  
  I recognize the feeling of despair! However, I don't think that you want what you are asking for. The most shocking bit of code that I know to show students is the code I wrote up [here](https://stackoverflow.com/questions/27308336/an-interesting-bit-of-reflective-code-in-java). Beware, it's quite strange! If you want a bit of fun, don't look at my explanations. Just copy the code into an IDE, look it over, make a guess about what it will do, and run it.
  
  **I don't think that this will help you with your students, however!**
  
  What you're pointing to are what I call "cognitive traps", and they must be addressed very specifically during instruction. You're already on the right track in that you are spotting them, but I don't think you'll make a lot of headway trying to find a single, unified solution.
  
  # The problem with being an expert
  
  There is no one key understanding that will, for a novice, unify everything into a sensible framework.
  
  There's actually a reason for this, which I previously wrote about in my answer [here](https://cseducators.stackexchange.com/questions/5403/students-seem-to-be-unable-to-use-code-to-express-their-ideas-is-the-teaching-m/5429#5429). In essence, overarching frameworks are the toolsets of experts. Factoids and small systems are the toolsets of novices. The way that novices become experts is typically by learning a set of small systems, and then in later revisitings, seeing how they are actually similar ideas.
  
  Experts often wish to teach novices to see the big system, and show how all of the smaller systems operate under the same principles. If we could only get them to think about it the way we do, they would be able to work so fluidly! Principle to small systems is a top-down method of learning.
  
  An alternative is to move bottom up: here, look at these three, apparently separate ideas. Now, let's link them together! While I am very impressed with @username_7's answer, I believe that this is the approach that they are taking here.
  
  I would contend (and I know that our top user, username_6, has also said this many times) that when we learn deeply, we actually do it in a spiral: we revisit an idea many times, each time enriching it by linking it to other ideas. This fits nicely with our current understanding of [learning in the brain](https://www.nap.edu/read/9853/chapter/8), in which information is physically structured within the brain as a pattern of interconnected neurons, and new information is learned by linking new ideas to old ones that we have already mastered.
  
  # Getting practical
  
  So, what's a teacher to do? First, with regards to the three very specific things you've mentioned, explicitly call attention to them when you are presenting function calls and loops. They are traps that students can fall into, and you want to head them off at the pass. Give them a chance to practice doing it correctly immediately, and demonstrate what happens when they do it wrong. The longer you teach, the more such traps you'll discover and can thus help student avoid. That's part of becoming a better teacher.
  
  And as for the large systems, revisit older ideas and keep linking them to new ideas as you teach them. Ask students to reflect directly on how this idea and that idea are similar. Ask students to make a drawing of how a loop iterator actually operates. The more that we help students link ideas together, the more abstractly they'll be able to work.
  
  It's not a lightning-fast quick fix, but you will see improvement if you keep making improvements!
  
  > **Comments:**
  > **username_0**: Yes! Definitely agree. (Besides having yet to see the java code :) ). For what I can I am constantly trying to show them small pieces and their connections. Problem is that so many times I already have shown them that a function cannot return many values, how a for translates to a while, how the variables are updated, they just kind of don't want to believe it, and their motivation is really 0. They're used to these crazy I/O operations: oh we need to know how many iterations, let's scanf something! I was even starting wondering: is this what cobol was made of? What's this thing?  
  > **username_0**: Unfortunately I cannot introduce them functions and loops, they've already been introduced in previous years, they shouldn't even be so much unaware of those concepts. My goal was having them build small real algorithms, let's say for linear algebra, I have already given up on this actually.  
  > **username_0**: When I mentioned the shocking effect I was thinking something so short as a for (i=0; i<5; i*=2). Just I don't think I am producing examples of good interest and of the right degree between too trivial and too crazy. Has anyone an arsenal of things of this kind? Btw, has anyone else noticed that learning steps in CS seem to be wider than other disciplines?  
  
  ---
  
  ### Answer by username_6 on 2019-03-08. Score: 1
  
  I, too, will give a somewhat orthogonal answer to your quest. I would suggest that you find a way to incorporate unit testing into your Java based course with Unit. You can give them tests or require that they write tests. You can require an explanation of them when tests fail.
  
  But if you incorporate testing deeply into your teaching and requirements ([Test First - Test Driven Development](https://en.wikipedia.org/wiki/Test-driven_development)) these sorts of misconceptions should start to disappear.
  
  If you demonstrate code to your class live with some projection equipment you can demonstrate such things to good effect.
  
  > **Comments:**
  > **username_0**: Thanks for the answer, it has its place, but it's not adequate for my specific case: my course is not Java, we have no classes (fortunately!), we need just a very very basic intro to the world of computing. The pupils had a taste of COBOL written with C syntax and are addict to printf before computing. I showed them Javascript to work more gently with arrays, but they have a really hard time understanding what do I mean for "working with arrays": there is no output print and they are lost. I think adding the complexity of testing is a bit too much for people who cannot understand a for loop.  
  > **username_6**: Hmmm. You mentioned Java in a comment. There are similar unit testing frameworks for most languages, actually.  
  > **username_0**: I don't think I mentioned Java, I may be in error, but it may also be that I can be confused with someone else, my name does not seem designed for identification tasks :) Anyway, no probl. The more discussions I can see the happier I am. I'm starting to give up with this class because besides the technical problems they are also very demotivated and this complicates things even more: they sort of demand that I just write code while they do nothing with their brains, probably I'll end up with this.  
  
  ---
  
  ### Answer by username_3 on 2022-04-13. Score: 0
  
  I get the feeling that we are teaching in the wrong order. We should teach functional first.
  
  In your first bullet point, it looks like you students are trying to invent a list comprehension/mapper. It may be easier to teach them how to do it this way that to teach how to implement it procedurally.
  
  The 2nd point is just missing that there is more that one input.
  
  The 3rd I would introduce `foreach` loops before general purpose loops.
  
  
  ---
  
  ### Answer by username_5 on 2022-04-07. Score: 0
  
  Is programming being taught from an abstract perspective? Some students when taught from an abstract perspective seem to treat statements as magic incantations that cause the computer to do something without seeing that there is an underlying structure to the language that is imposed by the nature of the hardware. If students have some understanding of how computers actually work and how programming languages match up with capabilities of the hardware, then they are more likely to try and see underlying meaning in the statements comprising the program. This will mean their underlying mental model will be more correct and their intuition about what will happen will be better.
  
  I don't think this is a problem with imperative langauges, but an abstract/practical issue. Some students like an abstract perspective, others can't deal with it at all, so no "one size fits all" strategy will be effective.
  
  I like @username_6's suggestion to use testing, as to test effectively you need to be able to predict what the code *should* do to make the test cases, which encourages students to understand the code. Getting students to do bug fixing is also useful, as again that involves looking at code and trying to understand why it is doing what it is doing, in order to work out how to fix it. The coursework I set for my second year university C++ class involves writing code, testing, bug fixing and refactoring, and it seems to work quite well.
  
  
  ---
  
  