Is this a nested loop or not?

Thread Starter

rohan.ahmad36

Joined Oct 28, 2016
1
I am programming an 8051 micro controller and confused about the below code that is this a nested loop or not? My teacher is saying that this is not nested loop because that would require two labels. please tell me can we call this a nested loop or not?

ORG 00H
MOV R1,#10
MOV R2,#100
HERE:
DJNZ R2,HERE
MOV R2,#100
DJNZ R1,HERE
END
 

joeyd999

Joined Jun 6, 2011
5,285
If your teacher wants to be pedantic about it, show him this code:

Code:
ORG 00H
MOV R1,#10
MOV R2,#100
HERE1:
HERE2:
DJNZ R2,HERE2
MOV R2,#100
DJNZ R1,HERE1
END
There are two labels but the code is equivalent. What's his argument now?
 

ErnieM

Joined Apr 24, 2011
8,377
Since I assume you have a teacher because you are a student let me tell you the best answer:

Always tell your teacher what he told you if you want a good grade.Teachers vary in knowledge and experience but they are the ones who ultimately grade your work.

Is it a nested loop? Yeah I agree with Joey that it is. Does your teacher think so? Obviously not.

So use your teachers answer on any homework or test.

In reality this hardly matters as it is splitting hairs on a minor pedantic point. As long as you understand what the code does you can call it a banana loop for all it matters.
 

joeyd999

Joined Jun 6, 2011
5,285
Since I assume you have a teacher because you are a student let me tell you the best answer:

Always tell your teacher what he told you if you want a good grade.Teachers vary in knowledge and experience but they are the ones who ultimately grade your work.

Is it a nested loop? Yeah I agree with Joey that it is. Does your teacher think so? Obviously not.

So use your teachers answer on any homework or test.

In reality this hardly matters as it is splitting hairs on a minor pedantic point. As long as you understand what the code does you can call it a banana loop for all it matters.
My approach was to write the correct answer to the question. If it was different than the teachers, I'd provide a written explanation -- and a logical argument if necessary -- along with my answer.

This occurred often and I was never penalized for it. Often, the teacher/professor was gracious enough to correct himself to the other students.
 
Last edited:

ErnieM

Joined Apr 24, 2011
8,377
simple:

1) is it a loop?
2) is it nested?

if and only if the answers to both questions are "yes", you have a nested loop.

otherwise, no.
Fine, except you did not define "loop" nor "nested."

As long as we are still splitting hairs I will content that "a loop" can never be nested as it takes a minimum of two loops for one to be nested within another.

Complete aside: when I was in 8th grade I was in the "honors" program studding algebra. For most of the year I was clueless as some fundamental escaped me. This progressed into the time I was with my guidance counselor picking out the next year courses, and I requested to be removed from honors math, and he did so. Shortly after the concept of a variable clicked with me, and I remember well one day in class answering every question the teacher asked and nailing every one, going on to score a 96 on the state exam.

Well... Forgot I had dropped out of the honors program so the next year found myself assigned to a regular class. What is worse, the honors program was a year ahead so I was back in algebra! For a day or two the teacher would call on me as I constantly corrected his mistakes. Apparently, all you needed to pass his class was an appreciatioon for his favorite basketball team. Otherwise he was a disaster as a teacher.

Moral: teachers come in all shapes and sizes, some good, some mediocre, some bad. There is also a trend in lesser skilled people to take offense when you attempt to correct their errors.
 

MrChips

Joined Oct 2, 2009
30,821
Given that there are two loops, loop A and loop B.
Loop A iterates m times
Loop B iterates n times.
If the total number of iterations is m x n then it is a nested loop.
 
Top