We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › mousePressed events
Page Index Toggle Pages: 1
mousePressed events (Read 646 times)
mousePressed events
Oct 25th, 2009, 7:46am
 
I'm using a mouse event and a for loop, but I cannot get them to work together, I don't see anything here that is much different to the examples so i'm not sure what i'm doing wrong..?

Code:
 

void mousePressed() {
int increase = 1;
currentRow = currentRow+increase;
currentRow = constrain(currentRow, 0, 200);
lastLevel = level;
level = level+5;

println(time);

for(int i = getHour(); i < 25; );
clockHoursArray[getHour()] = clockHoursArray[getHour()]+1;
println(clockHoursArray[12]);

}

Re: mousePressed events
Reply #1 - Oct 25th, 2009, 8:21am
 
Its not very clear what you want the for loop to do but what you have is likely to go into an infinite loop

The syntax of a for loop is

Code:

for(initialise; test; change){
 // statements to be executed in for loop
}
// come here when for loop finished


When a for loop is encountered -
1) the code in initialise is executed
2) the test is performed
3) if the test is false then the for loop is finished
4) if the test is true then the code inside the loop is executed
5) the 'change' code is executed and we go back to (2)

In your code you have a ; at the end of the for statement which means the code on the next 2 lines are not part of the loop.

You initialise i with getHour() which I assume returns a value <25 the test (i < 25) then will ALWAYS be true, since there is no 'loop code ' the change code is executed but this is empty so i is not changed so we do the test again ......... inifinite loop.

Could help more if you explainedwhat the program is trying to do.
Re: mousePressed events
Reply #2 - Oct 25th, 2009, 8:39am
 
Code:
 for(int i = getHour(); i < 25; );
  clockHoursArray[getHour()] = clockHoursArray[getHour()]+1;
  println(clockHoursArray[12]);
}


You can't be looking too carefully at the examples...

First off your for loop declaration should be followed by an opening brace '{', not a semi-colon...  Next up it's very unusual not to define some kind of increment on your iterator in the declaration - eg 'i++' being the most common.  You can get away without doing that if you manipulate 'i' within the loop and can be sure it will eventually reach the limit.  If you don't then you'll land up with an infinite loop and that's bad...

TBH you don't appear to have understood the use of a for loop properly.  If you're trying to access values in an array this is a more standard approach:

Code:
for(int i=0; i<myArray.length; i++){
 // use i as an index in the array:
 println(myArray[i]);
}


Now it may be that you're using getHour() to get the current time and want to then iterate over the remaining hours in the day; in which case you can define i as getHour() to start with (so long as it returns an int); but you then need to access your array with 'i', not getHour(), within the loop.
Re: mousePressed events
Reply #3 - Oct 26th, 2009, 2:59am
 
Ok, it might well be that there is a better way of doing what I want to do, I thought it might be a for loop but possibly not!

getHour() is a function that returns the 'hour numbers' from a table of 4-digit times from the current row, then when the mouse is pressed the next time from the list is found and getHour updated.

I have an empty array (24 bits long), and I am looking for th best way to increment (array item12) each time getHour() returns '12', arrayitem10 each time getHour() returns '10' etc.

Im not sure if I want this to be a loop then, because I dont want to change the value of 'i' with the loop, or save 'i' as another variable, just need the loop to 'assign an increment of 1 to the array item with the same index number as getHour();.

If there's a more sensible way to look at this I'd appreciate it, so I can give it a look as I am trying to get my head around how this language works!
Page Index Toggle Pages: 1