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 › if(expression) question
Page Index Toggle Pages: 1
if(expression) question (Read 516 times)
if(expression) question
Feb 6th, 2009, 7:39am
 
I'm a novice programmer so please forgive me if this question is dumb or has been answered elsewhere.

I'm evaluating a string and I was wondering if you can have multiple expressions in an if() statement.  This would make my life a lot simpler.

Right now my code looks like this:

void evalWrd() {
   vowels = 0;
   cons = 0;
   for (int i=0; i<lngth; i++) {
     switch(ltrs[i]) {
       case 'a':
         vowels ++;
         break;
       case 'e':
         vowels ++;
         break;
       case 'i':
         vowels ++;
         break;
... etc.

I'd like my code to look something like this:

void evalWrd() {
   vowels = 0;
   cons = 0;
   for (int i=0; i<lngth; i++) {
      if (ltrs[i] == 'a'; ltrs[i] == 'e'; ltrs[i] == 'o'; ltrs[i] == 'u') {
         vowels ++;
       }

even though this particular snippet creates an error.  If I want to do the same thing for several different cases, what is the most efficient way to code it?

Re: if(expression) question
Reply #1 - Feb 6th, 2009, 8:38am
 
Hi
There are two ways :
1.//not the cleanest way
switch(ltrs[i]) {
  case 'a':
  case 'e':
  case 'i':
    vowels ++;
    break;
}
2. //the cleaner way
if (ltrs[i] == 'a'|| ltrs[i] == 'e'|| ltrs[i] == 'o'|| ltrs[i] == 'u') {
    vowels ++;
  }

please read up on the 'switch' statement and the '||' (OR)operator :D
Re: if(expression) question
Reply #2 - Feb 6th, 2009, 10:06am
 
I disagree with you, nobb, I find the switch way cleaner and much more efficient (optimized by Java).
Now, the way 2) precisely answers the syntax question of MagusArts, so it is a good answer too... :-D
Re: if(expression) question
Reply #3 - Feb 6th, 2009, 2:05pm
 
the c library ctype functions uses an array of bytes, one for each character, which holds characteristics of the letters. there are flags for alphanumerics, control characters, digits, lowercase, punctuation etc (not vowelness, but it wouldn't be hard to add). checking a character's properties then becomes a linear time lookup with no need for any ifs, switches or whatever (at the expense of 256 bytes of memory)

see _sch_istable here
http://cpansearch.perl.org/src/SHEVEK/Text-CPP-0.12/safe-ctype.c

failing that (which is a bit heavy duty tbh) there's always

"aeiou".indexOf(ch);

which will return -1 if not found

http://java.sun.com/j2se/1.3/docs/api/java/lang/String.html#indexOf(int)
Re: if(expression) question
Reply #4 - Feb 6th, 2009, 2:07pm
 
Code:

if (ltrs[i] == 'a' || ltrs[i] == 'e' || ltrs[i] == 'o' || ltrs[i] == 'u') {
vowels ++;
}


there's a bug in this btw 8)
Re: if(expression) question
Reply #5 - Feb 7th, 2009, 10:52am
 
PhilHo ,
Thanks for the information ! I actually like the 'switch' better than the if version but thought the 'if' format was close to textbook recommended form. I'll go with instinct next time.

Koog ,
is it because the vowel 'i' is missing ? remember there's no I in team Cheesy
Page Index Toggle Pages: 1