Can A Loop Remember

If I define a variable in the pre-setup

int x = 1

And change it in the loop

x = 4

But in the beginning of the loop, there is

y = x

What will the value of y be? In other words, can a loop remember?

Really? You can't type 4 or 5 short lines of code to find that out?

Rather than try to answer your question directly, let me suggest that you write a simple sketch to test the behavior that you're interested in. It will be a more thorough learning experience for you.

Here's what I'd recommend:

  • Write a sketch that prints the values you want to examine at the end of setup, at the beginning of loop, and right before loop() ends. You might want to add some delay()'s to keep it from printing so fast you can't read it.
  • Before running your sketch, examine it, and decide what you expect it to do.
  • Run the sketch, and see what it does.
  • If it's something other than what you expected, look at the sketch again, and see if you can tell what it really did. You may want to google the assignment operator in C for more information.
  • If you have questions, come back and ask. Or, you may just want to report what you saw and learned.

Look up "global" and "local" variables in C/C++ programming. Also learn about "scope".

It is extremely important that you understand these concepts and memorize the difference between local and global.

Y first equals 1, then changes to 4. A loop can remember!

A loop doesn't remember squat. I suggest you look in the first post at the top of this Forum by Nick Gammon and then look for the subtopic "Scope" and study it.

But this is for a global variable changed by the loop.

chessy:
But this is for a global variable changed by the loop.

It's not that the "loop" remembers, the global variable only changes when it is explicitly changed. Then it doesn't change after that until it is changed again. If you wish, it's the variable that remembers.

chessy:
Y first equals 1, then changes to 4. A loop can remember!

You are free to believe that the statements execute in random order, and that the bug-eyed spaghetti-monster intervenes and changes the result only in the last (nano-)second, if you like.

However, programming is not black arts. Computers are 100% man-made. They operate according to strict principles and programming them does too. This doesn't mean its easy, or that computers are 100% deterministic machines, but what it DOES mean, is that ... (drum roll)

Things have names!

:slight_smile:

Learning the names and understanding the mechanisms means you get to master programming. Ignoring the names and insisting on redefining a 100% man-made world in terms of how you think things might work, is pure superstition.

A C program consists of functions.
Functions contain statements
A loop is a statement that contain other statements.
Statements may operate on available variables.
Variables can have varying scope.

That's all there is to it.

:slight_smile:

/Rant

chessy:
But this is for a global variable changed by the loop.

You obviously didn't take my suggestion and study up on Scope.

You obviously didn't take my suggestion and study up on Scope.

It seems that "chessy" would rather post lots of naive questions on this forum, than do any independent experimentation or reading.

Writing a minimal test sketch is the most straightforward way, and the most educational way, to find out whether your understanding of how the language works is actually correct. If you can't tell, then posting that sketch, telling us what you expected, and telling us what happened is the easiest way for you to describe what you're talking about - for example, you don't have to tell us that you're talking about global variables if you declare the variables you ask about as global.

I'm not willing to do you the disservice of stealing from you the intellectual thrill of working this out for yourself. Write a test sketch, and post it, please.

tmd3:
Writing a minimal test sketch is the most straightforward way, and the most educational way, to find out whether your understanding of how the language works is actually correct. If you can't tell, then posting that sketch, telling us what you expected, and telling us what happened is the easiest way for you to describe what you're talking about - for example, you don't have to tell us that you're talking about global variables if you declare the variables you ask about as global.

I'm not willing to do you the disservice of stealing from you the intellectual thrill of working this out for yourself. Write a test sketch, and post it, please.

I did that 5 posts ago. Read the other posts before commenting, please.

jremington:
It seems that "chessy" would rather post lots of naive questions on this forum, than do any independent experimentation or reading.

I am lazy. So are you if you choose to waste your time like this.

Read the other posts before commenting, please. ... I am lazy. So are you if you choose to waste your time like this.

As charming as you are clever, I see.

Looks like I've wasted my time, and annoyed you. Sorry for the inconvenience. Based on the quality of the original post, I should have seen it coming.