for (int i=0; i==5; i++){
Serial.println(i);
}
I see nothing wrong with it what about you all!?
for (int i=0; i==5; i++){
Serial.println(i);
}
I see nothing wrong with it what about you all!?
The syntax is fine.
If you want a different answer I suggest you describe what you (the developer) want to happen when that snippet is executed.
ok sorry for being so fast at posting that...basicly it just seems that the Arduino sketch skips over it for some reason
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#define NUMFLAKES 10
#define XPOS 0
#define YPOS 1
#define DELTAY 2
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH 16
int lin1[5];
void setup() {
// put your setup code here, to run once:
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.display();
Serial.begin(9600);
}
void loop() {
display.setRotation(2);
for (int i=0; i==5; i++){
Serial.println(i);
}
}
not sure if its a problem with the ide (dout it) or my code (most likely).
what I want it to do is count up to 5 then restart
OK. So this is an actual programming question (as opposed to a puzzle / riddle).
Thread moved to the appropriate section.
The middle expression of the 'for' loop is interpreted as the expression that allows the loop to run.
The for loop you wrote
for (int i=0; i==5; i++)
{
Serial.println(i);
}
is really just a while loop
i=0;
while (i==5)
{
Serial.println(i);
i++;
}
Do you see what is wrong with this?
ohhhhhhh I see where I messed up now... yeah thank you Marco... I'm making a Tetris program for an oled and its my first time using arrays... and ive been programing since I was 8 (14 now) ive been avoiding them... thank you so much this has helped me a lot I have the first block line done!... ill probably post it somewhere when I'm all done with it.
brenden_nerd_:
ive been programing since I was 8 (14 now) ive been avoiding them...
Dude....
I'll teach you this stuff in 2 minutes then you'll know it forever. But there is something that can screw up your thinking, so I need to start by asking you to count something.
Question: If you started programming when you were 8 and you're programming now that you're 14, how many different ages (8, 9, 10...14) have you been programmer? Count them on your fingers and tell me, then I'll give you the rest and you don't have to avoid for loops anymore haha.
Change i==5 to i<6 or i<=5
Edit-I stand corrected, it doesn't even print the 0 since i==5 condition fails. Must've been confusing the do while stuff I just learned about.
In other words "i is 0. Print 0.
Nope.
Don't print anything
If fenceposts are one meter apart, and you need to fence in a 5 meter gap, how many fenceposts do you need?
curious48:
Dude....I'll teach you this stuff in 2 minutes then you'll know it forever. But there is something that can screw up your thinking, so I need to start by asking you to count something.
Question: If you started programming when you were 8 and you're programming now that you're 14, how many different ages (8, 9, 10...14) have you been programmer? Count them on your fingers and tell me, then I'll give you the rest and you don't have to avoid for loops anymore haha.
I programed when I was 8-10 then I stopped for a while and that's when I got a raspberry pi and a Arduino (age 12) . I ended up learning to code on the Arduino first as it was more similar to the languages I had already learned... those being trs-80 BASIC (even though ive never owned a trs-80) petit BASIC (runs on a 3DS) and a little bit of batch and Atari coding ... not to mention minucule knowledge of windows VBS... I plan to program as a job when I'm an adult, either with video games or practical applications.
He's not asking about your actual experience. He's trying to get you to do some math. How many unique numbers in the set 8 - 14 inclusive.
well if he means how many numbers there are that are individually different between 8-14 I would have to say infinite because of decimal places sure you can have 8 9 10 11 12 13 14.... but what about the decimal 8.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 and so on adding more zeros to it... its like 0 divided by zero. the answer is infinite because 0 can go into 0 forever and there is no change in the number.
brenden_nerd_:
well if he means how many numbers there are that are individually different between 8-14 I would have to say infinite because of decimal places sure you can have 8 9 10 11 12 13 14.... but what about the decimal 8.0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001 and so on adding more zeros to it... its like 0 divided by zero. the answer is infinite because 0 can go into 0 forever and there is no change in the number.
Yes, you are correct and I think my example might have been a bit confusing. Actually you are giving a very correct answer: you've been programming at an infinite number of ages. My question should have read "how many different ages, in whole years.." because I didn't want you to count 8.4 as a different age from 8.6.
So you are right.
(In C++ this would be calling "casting to an integer"). You are correct and answered my question very literally (just like a compiler would).
So let me rephrase my question: if we consider whole years (8, 9, 10, 11, 12, 13, 14) how many different whole-year-ages did I just list? How many numbers were there in the parentheses just now.
I know it seems "insulting" to make you count but I'd like to hear it from you. Then I will say how it relates to for() loops, the fenceposting that was mentioned by someone else, etc. so please tell me how many values are in the list: 8, 9, 10, 11, 12, 13, 14 (and sorry for this simple question, it will be worth it once you hear the next part haha.)
ive seen this before but I think I'm still to much of a novice to understand it. id say 7 under normal circumstances.. but I know I'm wrong I just don't know the correct answer so continue!
Hi Brenden,
Part 1: Off-by-one errors.
Nope, of course, you're right! There are 7 different values in the range 8,9,10,11,12,13,14.
what's interesting is that it's 'off-by-one' compared w subtracting them. in other words 14-8 is 6, but there are 7 values in there.
If I'd originally asked you "so how many years have you been programming" (a question about a difference) you'd have correctly stated: well, 14 now, started at 8, so 14-8 = 6. This is correct. If you started something July last year, it would be 1 year you've been doing it: 14 now 13 when you started, 14-13 = 1 year. Or: 2016 now, 2015 when you started, 2016-2015 = 1. But there are two values in there. So if you count 'both ends' you get one more than if you subtract them.
Okay, now put this aside in your mind. Just put it aside.
Part 2.
Put aside what I just said. Now I have to tell you something.
There is a history in programming of starting to count at 0, sometimes. This is incredibly confusing.
If I put three apples on a table and asked you to count them off with the names of the numbers, you wouldn't point to the first one, say "zero", point to the second one, say "one" and point to the third one, and say "two". It doesn't make any sense!!!
But this is exactly how an 'array of 3 apples' is indexed in almost all computer science languages. Three apples on a table that are red, green, and yellow (in that order), would be referred to as apples[0] for the red one, apples[1] for the green one, and apples[2] for the yellow one. There is no apples[3].
This is mind-bogglingly confusing. Why is it done this way?
It's done this way because historically an "array" was a pointer to a point in memory. So if I start off pointing at the first apple, I'm already pointing to it, I don't need to add anything (move my finger over). apples[0] is the red one or, using pointer arithmetic, (location I'm pointing) + 0 == the red one . If I point one over (location I started off pointing to) + 1 == the green one. And if I point two over, I get the third one. (location I started off pointing to) + 2 == the yellow one.
So 0, 1, and 2 are the "indexes" we use to refer to 3 elements of an array. apples[0], apples[1], and apples[2] are the first, second, and the third of three apples.
Now let's count them. How many apples are there? 3. What's the biggest index? 2. There is no apples[3].
Part 3: For loops.
Now you can understand for loops. If we want to do something for every apple in a list of 3, we need to start at Apple 0, and do it all the way up to, but not including 3, since there's no such thing.
The syntax of a for loop is: for( ;; <increment/other statement>)
setup statement just runs once; the condition is checked before entering each time, the first time it fails the for loop is over - it's not guaranteed to be true even once; and increment or any other single statement is done after the content of the loop, if it was entered
so, if the middle condition were always true then the loop would always run forever: for( ; true ; )
what would happen? The compiler would check "is true true? yep.. so let's enter the loop" it would go through the for loop, then it would check again "is true true? yep.. so let's enter the loop" and so on forever. (the syntax for( ; ; ) is a shortcut for this)
If instead the condition were false, it would never run even once: for( ; false ; )
Compiler: "is false true? Nope. Don't enter the loop". So, it wouldn't run even once.
But what is most interesting is when we want the condition to be true for a while, and then false.
In case of going through the three apples, we want the compiler to enter the loop for apple[0], apple[1] and apple[2] but NOT for apple[3] because there is no apple[3].
So what is something that is true for 0, 1, and 2 but not for 3? "apple < 3". Note that it doesn't say "less than or equal to" it says "LESS THAN". three isn't less than three.
So, we start off at 0, and the condition is that we enter it while it's less than 3.
int i = 0; //put here for clarity
for ( ; i < 3 ; ) {
//for clarity
i++;
}
I decided to put the increment inside the loop to make clear it only gets executed when the loop is entered into. same meaning.
Now let's go through it. We start off the loop with i still set to 0.
The first time the compiler thinks about entering loop it asks "is i < 3 true" in other words "is 0 < 3" and it is, so it enters the loop. Then as you can see at the end of the loop we increment i.
So the next time it thinks about entering the loop it asks "is 1 < 3" and it is, so it enters the loop a second time.
The third time it enters the loop, it asks, is 2 < 3" and it is, so it enters the loop a third time.
And the fourth time it thinks about entering the loop, it asks "is 3 < 3" and it is false so the computer does not enter the loop for a fourth time. It enters the loop when the values are 0, 1 and 2 but fails on 3 - it's a value it never reaches.
Because, for historical reasons, we wanted i to have the values 0, 1 and 2.
So, that is why programmers count to three by starting at 0 and having a condition that fails at the number of elements that they want. They're REALLY counting from 0 to 2 when they count 3 things.
This is super-duper, maximally, astoundingly confusing. Let's continue with this example.
What would happen if I modified this:
int i = 0;
for ( ; i < 3 ; ) {
i++;
}
to this:
int i = 5;
for ( ; i < 3 ; ) {
i++;
}
in other words, for (int i = 5; i < 3; i++);
well, we start at 5. The compiler asks "is 5 less than 3" and it isn't, so it doesn't enter the loop even once.
In your original program, you asked "is i == 5" and it wasn't, so it didn't enter the loop even once.
What if I asked: "for (int i = 0; i == 0; i++)"
Would this execute zero times, one time, two times, or infinite times?
Well, just go through it. The compiler sets i to 0. Then it asks "Is i equal to 0?" It is. so it enters the loop once. Then it adds one. It asks, is "i == 0" (the condition again), this time it fails. So the answer to this question is "one time." See? You just have to follow the same steps the program does, to figure it out.
For loops can have any condition and stuff in there. you could have for (setup_a_reader(); are_there_More_bytes(); read_next_byte())
The condition in for ( ; condition ; ) just gets evaluated before entering the contents each time.
Okay, now we can see that there are alternative ways to do things.
You could print 10 numbers like this:
for(int i = 1; i <= 10; i++) // notice <= instead of <
because you want it to still be true at 10. You want it to be true for 1 thru 10 and fail at 11. This time i will never have the value of 0 in the loop.
Remember, when you use less-than**-or-equal-to** (which is written as <= ), then it will pass for that one, instead of failing. it'll fail on the next one.
so for (int i = 0; i < 3; i++) will enter the loop with the values 0, 1 and 2 but fail at 3 and not enter it but for (int i = 0; i <= 3; i++) would be off by one, because it will enter for values 0,1,2,3 and only fail for four.
The usual way to count 3 things is i=0; i<3; i++ in other words to count the values 0, 1 and 2 and fail on 3, since three isn't less than three.
Just remember that the usual way counts 3 things because it passes for 0, 1, and 2, and there are 3 values in there. It never reaches 3.
So, this was an overview of syntax of a for loop, why historically we start off at 0 and have them fail for the number of elements you're counting (so the last one the condition is true for is the number just below it).
I hope you've found this helpful. It should help you understand for loops in the future.
Examples of correct and incorrect code:
idiomatic (normal, usual) way to do something ten times:
for(int i = 0; i < 10; i++) // enter w/ values 0 through 9 in the loop (which is ten different values since you're counting both ends 0 thru 9) but never enter with a value of 10, since 10 < 10 is false
The above is the usual way.
Another way to do something ten times:
for(int i = 1; i <= 10; i++) // enter w/ values 1 thru 10 (which is ten values) but never 11. you fail on 11. Because we started at 1 instead of 0, we changed the condition for "< 10" which excluded ten, to "<= 10" which includes (doesn't fail on) ten.
So when you start at 0, you usually use < number_of_items for the condition.
When you start at 1 you usually use <= number_of_items for the condition.
Either 0 thru 9 or 1 thru 10 both will give you ten numbers.
another way to do something ten times. But don't do it this way:
for(int i = 1; i < 11; i++) // enter w/ values 1 thru 10 but fail at 11, since 11 < 11 is false.
Don't do this way because people will wonder why you're starting at 1 but using the < syntax. It's very unusual.
a WRONG way to do something ten times:
for(int i = 0; i <= 10; i++) // enter w/ values 0 thru 10 -- these are 11 values, just like the values 0,1 are two values.
another WRONG way to do something ten times:
for(int i = 1; i < 10; i++) // enter w/ values 1 thru 9 but fail on ten. That's only 9 values.
These are the most common sources of programmer error. You see how close they are!!
The only way to avoid them is to think through exactly what's happening and use small examples, like, 0, 1, 2. If you really get confused, actually count it off and see what happens, go through what the program will do in the loop. Good luck!!!
is see... I found your post very interesting I laughed as some parts as well... not sure how much I rembered...but I got it all figured out... would you guys like me to post the current code for tetris (your gonna need a i2c oled for it)
I'm really out of my league here, but I would venture to say 4 fence post.
|-|-|-|-|-|-|-|-|
5 meter gap
I know I suck at drawings.