Question about loops


#1


Hello
I'm at the point of learning loops (for and while) and understand each parts of the code, what I'm curious about is what the 'data stream' looks like and where its coming from. I can understand that - kind of like a coin sorter - the data is 'flowing through' (this is my visualization rather than an actual description of 'data flowing!') the code and being 'allowed entry' based on whether or not it meet the criterion in the condition and moves 'through' the code within the {} which has instructions as to how that data will be processed. Obviously i'm visualizing it as a 'flow' - which might be utterly inaccurate - but where is the data coming from? And is it coming sequentially?

If i intellectually acknowledge the parts of code i can manipulate minimally, but if i can SEE HOW it works this will help me to REALLY understand it! Metaphors, similies welcome!

thanks for anyone able to clarify with a visual example! :wink:
Julie


Replace this line with your code.


#2

this sounds like the ideal tool for you:

http://www.pythontutor.com/visualize.html#mode=edit

select javascript ES6, and run the code, the arrow indicates which code is getting executed.


#3

Assuming that by data you mean code/instructions

At a machine level the cpu starts reading at some memory location.

The cpu reads an instruction, then increments the read position, and repeats.

Some instructions are to set that counter to something else, so instead of reading instructions in sequence, there is a jump to somewhere else. This is how if-statements and function calls are accomplished.

Code is placed in memory, the program counter is set to the beginning of the program, and from there on it just keeps executing the instruction that the counter refers to.

For function calls, the position to return to has to be saved somewhere, typically on a stack (put values on top of each other in memory, just increase/decrease the position of the last element to add a value, and do the opposite to remove. Variables are also placed here.

Keep in mind that there are a couple of layers of abstraction between what you write and what the cpu does


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.