Is a loop an example of abstraction?



So I’m giving feedback to my students’ written responses from this practice PT and several of them wrote about loops as an example of abstraction because it makes code more efficient. Here’s an example:

“An abstraction would be the for loop that was used to make multiple sound waves in the program. A for loop runs the code that is within it until the variable, that was declared within the loop, does not fulfill the condition that is given in the parameters for the for loop. Using a for loop helped us reduce the amount of lines in the program because it ran the code without us having to copy and paste multiple times.”

This student seems to be responding to the fact that this loop helps ‘reduce the complexity’ of his code because he doesn’t have to ‘copy and paste multiple times’. I’ve been responding that this helps makes his code more efficient, but actually doesn’t address this complexity issue.

Thinking about the actual AP task - a loop isn’t an example of an abstraction, right? And when it asks about reducing complexity, the AP is looking for responses more aligned to how it helps organize/partition/group together lower-level code into something bigger, right?

Abstraction via Loop

Yeah, calling a loop an example of abstraction is probably not right. Your response is good. Let me ramble here a few more thoughts that might help you (and your student(s))

  1. First, let’s just get this out of the way: for all practical purposes when it comes to the AP Performance Task “abstraction” means a “function”. So you are never wrong to point to some function you wrote, call that the abstraction, and explain what it does. (There are other things you might “abstract” but the easy one is always a function).

  2. There is a particular nuance to “reducing complexity” that is part of an abstraction that I think is missing from your student’s response – and that is about hiding unnecessary or complicated details. “Unnecessary” here means unnecessary for understanding what the code is doing.

  3. related - Another typical property of an abstraction is that it nicely “encapsulates” a solution to a problem, and presumably that solution can be re-used and applied to other larger problems or in other contexts without having to worry about the inner details. Once you have encapsulated a solution to a problem it’s specific details can be put out of mind, freeing your mind to solve more complex problems.

Here is an example – Take this code, something akin to your student’s loop, I assume.

for(var i=0; i<4; i++){

In some sense this loop makes the code less complicated than it otherwise would be because without the loop you’d need many more lines. But as a reader of this code, to understand what it’s doing, I have to consider the details, I have reason about it, and mentally trace it out to figure out what it’s doing. Now if instead the line of code were:


I don’t even have to think it all to understand what this is trying to do. It reduces complexity because it hides details about exactly how the square is being drawn - really it doesn’t matter whether this function is implemented with a loop or not if what I care about is drawing a square - which makes it easier for me to reason about and furthermore to use the drawSquare behavior in other code that needs square-drawing.

drawSquare is a very small example, but when faced with programs or problems where the code is really complex and tough to reason about, bottling it up into a well-named function really helps. And also, loops usually complicate code because they are hard to reason about.

Here’s another example to prove my point. Imagine you’re reading along in some code and you come across this chunk that uses a loop. Try to figure out what it’s doing:

var newStr ="";
for(var i=0; i<str.length; i++){
    var ascii = str[i].charCodeAt(0);
    if(ascii >= 97 && ascii <= 122){
    newStr += String.fromCharCode(ascii);

Now what if instead, you were reading along in some code and found:


Even if I am an expert and can read the first code segment and figure out what it’s doing in 5 seconds - it’s NOT abstract. It’s a very concrete, explicit process for converting characters to upper case. It takes some amount of brain power to figure it out. Whereas, that same code encapsulated in a function convertToUpperCase it takes me no time to understand and I can use my brain for other things, like whatever larger task I need this case conversion for.

I think it’s fair to say with abstractions there is a bit of a you-know-it-when-you-see-it kind of thing that comes with experience and is hard for the new student to grok. And it’s hard to define because you can start to go down the rabbit’s hole with abstraction - let’s not forget that EVERYTHING we do on the computer is an abstraction in some sense - the whole machine is an abstraction. You’re seeing characters on the screen that are being represented by pulses of electricity behind the screen.

I hope this helps.


In a non-computer explanation of abstraction, we talk about driving a car. We don’t need to know how the engine or gears work, we accept that they will do their job. I then get them to come up with other examples. I know it probably seems a bit simplistic, but I can then extrapolate on to code etc and they seem to get it easier.


I really like using the car to help explain abstractions. Tapping into their life experiences is a great idea!


For real life analogies pretty much anything that has a simple user interface to operate something that is mechanically or electronically complicated - car, elevator, tv, microwave, etc. - will work.

They are good for introducing the idea. But programming involves a bit more, so your explanation might eventually evolve to something like this:

One of the reasons you can operate a car without having to know how it mechanically works is because its purpose is very clear. The purpose of a car is to get you from point A to point B. Because you know that fact it makes it intuitive to operate because the abstractions presented to you map to the fundamental things you need to do to get from point A to point B safely - go, stop, turn.

Programming is a little different because it’s less intuitively clear what the fundamental purpose of a computer is. This makes it harder to apply or combine the abstractions because you don’t know where you’re going, so to speak. The fundamental purpose of a computer, by the way, is to process information, but what that information is and how it needs to be processed is an abstract undertaking in and of itself.

So programming is often about combining known abstractions or building your own to solve problems. In programming most abstractions are a procedure of some kind, and each procedure is intended to solve a single problem.

So back to a real world analogy:

Say that your problem is that you need to microwave some popcorn, but the only microwave in town is on the 10th floor of a building downtown. To solve this problem you decide to: drive your car downtown, get in an elevator up to the 10th floor, and use the microwave. Thus your procedure for microwaveThePopcorn involves combining your knowledge of using the abstractions that let you operate several single-purpose devices: your car, an elevator, a microwave.

Now in programming when you give this procedure a name like microwaveThePopcorn you can put out of sight and out of mind the complications you had to go through to solve it, and you can use it and treat it as though it’s also a single-purpose function. So microwaveThePopcorn can be used as a part of a solution to some other larger problem like: preparing to have friends over for a movie.

I hope that’s helpful.



Hey Baker,

I noticed you ended both of your responses with ‘Hope this helps’.

FYI - I copy-and-pasted your first response verbatim and gave it as part of a larger piece of feedback to my students, then had us discuss how this idea of reducing complexity differed from the ones they gave in their written responses. The consensus was that this was incredibly helpful in clarifying what the prompt is looking for in responses and how to better organize their code. I’m on the next round of revisions for their responses and they’re exponentially better than the first round.

So - I can say with confidence that your response tangibly helped my students better understand the prompt and improve their writing. Thanks for the in-depth response.


Ha ha! Great to hear! Thanks much. Glad it worked out.


Grading a PT in which a student identifies a loop as an abstraction. I’m letting it slide grade-wise this time because I never mentioned the distinction and I myself was confused whether it should count. Your explanation was informative and I’ll make sure to bring it up with my students in the future so they understand better the concept of abstraction.


I told a student to put his loop that calls his function multiple times into a call ALL function. I still can’t decide if this would count as calling it multiple times.


Not speaking from a scoring guide interpretation, but IMO it doesn’t count as calling it multiple times.

I would ask what benefit or simplification does it provide? I see the benefit more specifically from calling it from multiple locations in the code.

One advantage of using a function called from multiple locations is that you don’t have to copy/paste the code into those multiple locations and that if you change that piece of code, you don’t have to change it for every instance.

In the instance you mention, I don’t see any of those advantages. There’s an advantage of using the loop, but it seems the student could replace the one function call with the function definition and there would be no future advantage or disadvantage when he needs to tweak the code.


I would give the points to a student that calls a function in a loop but moves to a different location before calling the function or passes a different parameter each time since each time it is called, the behavior is slightly different. In the example beach scene project, this is what the “student” does in lines 94-97.

As for why you would call the function in the loop instead of just using the code, I think that helps from an abstraction point of view since the function name should make it clear what that chunk of code would do whereas just including the actual code would require a reader or another programmer figuring out how each line works.

I don’t know if that is how a AP reader would grade, but that is what I’m doing for my class.


Hi @lynn.garnaat! :smiley: Nice seeing you on the forum!

As for why you would call the function in the loop instead of just using the code, I think that helps from an abstraction point of view since the function name should make it clear what that chunk of code would do whereas just including the actual code would require a reader or another programmer figuring out how each line works.

Great point! I’m also unsure if this counts AP-wise, but I would give it to my students, especially if they were able to identify this idea and communicate it as clearly as you did.



I know this is a slightly older topic, but it most relates to my question.

Would a for loop inside a function show Mathematical and Logical Concepts? I would argue that it is a mathematical concept.


Hi @glenn.crane,

I think it could work. You can see some examples here with commentary. You will notice that row #5 seems pretty easy for the examples to get. I would especially say that if the student SAYS it uses math by adding 1 to the variable inside the for loop and checking IF the condition is true, then they should get it for talking about math and logic there.

With most of these, if you write convincingly, that is most of the work!

Happy programming!