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.
Page Index Toggle Pages: 1
'Or' function. (Read 846 times)
'Or' function.
Oct 30th, 2009, 3:56am
 
I'm using a number of is statements (which is already a bit clunky), so I'm trying to cut down the number I need by using ' || ' to give 2 possible values, but I get an error 'The operator || is undefined for argument types boolean, int. '

- what does it mean to define an operator? - is it the same as defining a variable? I thought || was global in processing..?

Thanks, L.

Code:


int hourNumber = getHour ();

if(hourNumber == 1 || 13) {
clockHoursArray[1] = clockHoursArray[1] + 1;
}else if(hourNumber == 2 || 14){
clockHoursArray[2] = clockHoursArray[2] + 1;
}....

Re: 'Or' function.
Reply #1 - Oct 30th, 2009, 4:25am
 
After an OR you have to include the full condition not just another value to check against:

Code:
 if(hourNumber == 1 || hourNumber == 13) 



I remember making that mistake when I was starting out Wink

For multiple conditions you might also want to check out switch().
Re: 'Or' function.
Reply #2 - Oct 30th, 2009, 6:46am
 
Hehe, glad it's not just me for a change! Thanks that's sorted it, thanks:) L.
Re: 'Or' function.
Reply #3 - Oct 30th, 2009, 7:30am
 
We can reduce this to one if statement
Code:
int hourNumber = getHour ();   
 
  if(hourNumber > 12) {
     hourNumber -= 12; // Subtract 12
  }
  clockHoursArray[hourNumber]++;


We can even get rid of them all

Code:
int hourNumber = getHour ();   
 
  hourNumber = ((hourNumber -1) % 12)+1;
  clockHoursArray[hourNumber]++;


Smiley
Re: 'Or' function.
Reply #4 - Nov 1st, 2009, 10:52am
 
Good codes Quark. Perfect way to simplify logic. It makes me think about K-map or K-diagram. I might oppose your second way since division takes more time (I'm confused, is it 1980?Smiley
Page Index Toggle Pages: 1