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 › switch statements
Page Index Toggle Pages: 1
switch statements? (Read 1336 times)
switch statements?
Nov 4th, 2009, 10:52am
 
I've been working on a typography clock, and everything has been going well until i discovered "8:00" is displayed as "eight o'clock eight" which is wrong... obviously

its gone well using just if statements, but the problem is it doesnt stop after. is there a way to stop it from displaying the hour after. Or do i really have to resort to switches? Ive been trying to get those to work but its not been very sucessful. [im still pretty new to this programming lang]

this is what it is at the moment...


PFont myfont;
void setup() {
size (900,90);
background(100,100,100);
smooth();

myfont = loadFont("Garamond-Bold-48.vlw");
textFont(myfont);
}

void draw() {
background(242,239,220);

int s = second();
int m = minute();
int h = hour();

//text(h+":"m":"+s, 15, 50);
textSize(60);
fill(36,36,36);

//------------------add minute in text------------------

if((m > 4) && (m <= 9)){
text("five past " , 25, 60);}

if((m > 9) && (m <= 14)){
text("ten past ", 25, 60);}

if((m > 14) && (m <= 19)){
text("quarter past ", 25, 60);}

if((m > 19) && (m <= 24)){
text("twenty past " , 25, 60);}

if((m > 24) && (m <= 29)){
text("twenty five past " , 25, 60);}

if((m > 29) && (m <= 34)){
text("half past " , 25, 60);}

if((m > 34) && (m <= 39)){
text("twenty five to " , 25, 60);}

if((m > 39) && (m <= 44)){
text("twenty to " , 25, 60);}

if((m > 44) && (m <= 49)){
text("quarter to "  , 25, 60);}

if((m > 49) && (m <= 54)){
text("ten to "  , 25, 60);}

if((m > 54) && (m <= 59)){
text("five to "  , 25, 60);}


//------------------add hour in text------------------
if((h == 12) || (h == 00)){
text("twelve");}
if((h == 11) || (h == 23)){
text("eleven");}
if((h == 10) || (h == 22)){
text("ten");}
if((h == 9) || (h == 21)){
text("nine");}
if((h == 8) || (h == 20)){
text("eight");}
if((h == 7) || (h == 19)){
text("seven");}
if((h == 6) || (h == 18)){
text("six");}
if((h == 5) || (h == 17)){
text("five");}
if((h == 4) || (h == 16)){
text("four");}
if((h == 3) || (h == 15)){
text("three");}
if((h == 2) || (h == 14)){
text("two");}
if((h == 1) || (h == 13)){
text("one");}

//------------------add -ish in text------------------
if((m > 5) && (m <= 9)){
text(" -ish" );}

if((m > 10) && (m <= 14)){
text("-ish" );}

if((m > 15) && (m <= 19)){
text("-ish" );}

if((m > 20) && (m <= 24)){
text("-ish" );}

if((m > 25) && (m <= 29)){
text("-ish" );}

if((m > 31) && (m <= 34)){
text("-ish" );}

if((m > 35) && (m <= 39)){
text("-ish" );}

if((m > 40) && (m <= 44)){
text("-ish" );}

if((m > 45) && (m <= 49)){
text("-ish" );}

if((m > 50) && (m <= 54)){
text("-ish" );}

if((m > 55) && (m <= 59)){
text("-ish" );}

if((m > 0) && (m <= 4)){
text("-ish" );}

//------------------add o'clock and others in text------------------

if((h==00) && (m <= 4)) text("midnight", 25, 60);
if((h==12) && (m <= 4)) text("afternoon", 25, 60);
if((h==11) && (m <= 4)) text("elven o'clock", 25, 60);
if((h==10) && (m <= 4)) text("ten o'clock", 25, 60);
if((h==9) && (m <= 4)) text("nine o'clock", 25, 60);
if((h==8) && (m <= 4)) text("eight o'clock", 15, 60);
if((h==7) && (m <= 4)) text("seven o'clock", 25, 60);
if((h==6) && (m <= 4)) text("six o'clock", 25, 60);
if((h==5) && (m <= 4)) text("five o'clock", 25, 60);
if((h==4) && (m <= 4)) text("four o'clock", 25, 60);
if((h==3) && (m <= 4)) text("three o'clock", 25, 60);
if((h==2) && (m <= 4)) text("two o'clock", 25, 60);
if((h==1) && (m <= 4)) text("one o'clock", 25, 60);
}


thanks
Re: switch statements?
Reply #1 - Nov 4th, 2009, 11:11am
 
IIRC switch statements only allow you to check a single value; so don't allow you to make combined comparisons like '(m > 4) && (m <= 9)' and thus they aren't going to help (may also be why you couldn't get them to work).

However you should certainly consider using else in combination with if:

Code:
if((m > 4) && (m <= 9)){
text("five past " , 25, 60);
}

else if((m > 9) && (m <= 14)){
text("ten past ", 25, 60);
}

else if((m > 14) && (m <= 19)){
text("quarter past ", 25, 60);
}

else {
 // default....
}


If the first condition proves true the following else conditions are skipped.  This is also more efficient since there's no point running those following conditions if you know they will return false.

And if necessary you can finish with a final 'else' on its own which will run if none of the previous conditions returns true...
Re: switch statements?
Reply #2 - Nov 4th, 2009, 11:28am
 
Ah, i see. I shall use the else statement in that case for the minutes.

The main problem is the "o'clock" part, will the the else statements fix that problem of having the hour at the end of the time? times such as "five past" have no problem because it is at the front anyway.

I notice that the hour likes to go on the end and because it does not have the co-ordinates at the end... :S
Re: switch statements?
Reply #3 - Nov 4th, 2009, 1:31pm
 
blindfish wrote on Nov 4th, 2009, 11:11am:
switch statements [...] don't allow you to make combined comparisons like '(m > 4) && (m <= 9)'

For the record, you can do:
Code:
case 5:
case 6:
case 7:
case 8:
case 9:
doStuff();
break;

But well, I prefer a if in this case... Wink
Re: switch statements?
Reply #4 - Nov 4th, 2009, 2:38pm
 
Thanks for the help everyone, but for the moment the problem still hasnt been solved using the else if statements.

when minutes= 00 [eg 8:00] is still appearing as "eight o'clock eight"...
Re: switch statements?
Reply #5 - Nov 4th, 2009, 2:57pm
 
Code:
if((h == 8) || (h == 20)){
text("eight");}
// [...]
if((h==8) && (m <= 4)) text("eight o'clock", 15, 60);

Both codes will be executed at 08:00.
Re: switch statements?
Reply #6 - Nov 4th, 2009, 3:03pm
 
OK - I got diverted by your question about switch and didn't really look through all those conditions.  The problem lies in the fact that, since you used lots of separate if conditions two conditions can be true at the same time.  In the example you quote it's clear that both:
Code:
if((h == 8) || (h == 20)){
 text("eight");
}

...and:

Code:
if((h==8) && (m <= 4)) text("eight o'clock", 15, 60); 


are true at the same time.  One suggestion I'd make in this sort of scenario is that if code is dealing with the same thing, in this case 'h', I'd look into whether it can be combined somehow.

...And actually you can use an 'else if' to fix the problem here too:

Code:
if((h==8) && (m <= 4)) {
 text("eight o'clock", 15, 60);
}
else if((h == 8) || (h == 20)){
 text("eight");
}


So if the first condition is true it won't move on to the second condition and you'll just get the "eight o'clock".  However if h=8 && m > 4 you should just get an "eight"...

Alternatively you could look at nested conditions:

Code:
if((h == 8) || (h == 20)){
 if (m <=4) {
   text("eight o'clock", 15, 60);
 }

 else {
   // i.e. if m > 4
   text("eight");}
 }
}


In actual fact that's probably a better solution.  I notice that you currently don't render h=20 as "eight o'clock": that would require you to add a whole load more conditions; whereas the nested approach avoids this...  nested conditions can often be more efficient, though it pays to indent your code carefully to make sure they're easily followed Wink
Re: switch statements?
Reply #7 - Nov 4th, 2009, 3:18pm
 
OHHH! That simple! I should've thought of that. [palmface] that indeed is a much better way than having many fancy codes. thank you!

if possible, can you teach me more about indenting the code? Its only been a week since I've started using this program just for a 2week project, I havnt been taught how to be "tidy" with my code.   Smiley
Re: switch statements?
Reply #8 - Nov 5th, 2009, 1:27am
 
Try using Python  Grin

Basically whenever you open a condition, loop, function etc. you indent your code, when you close the condition, loop, function you outdent; this makes it easier to figure out what's inside what.  Python forces this by using indentation, instead of braces, to interpret your code.

Consider this snippet:

Code:
void keyPressed() {
if (key == CODED) {
if (keyCode == UP) {
up = true;
}
else if (keyCode == DOWN) {
down = true;
}
else if (keyCode == LEFT) {
left = true;
}
else if (keyCode == RIGHT) {
right= true;
}
}
}


The same code with indentation is easier to read; and this becomes even more important when it is contained within a much larger programme...

Code:
void keyPressed() {
 if (key == CODED) {
   if (keyCode == UP) {
     up = true;
   }
   else if (keyCode == DOWN) {
     down = true;
   }
   else if (keyCode == LEFT) {
     left = true;
   }
   else if (keyCode == RIGHT) {
     right= true;
   }
 }
}


BTW, once you get to grips with conditions you might consider looking at arrays.  They can be tricky to get your head round at first, but for your project they could make things a lot simpler...  For example the following array could save you from writing a lot of conditions:

Code:
String [] times = {"twelve", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven" }; 

Re: switch statements?
Reply #9 - Nov 5th, 2009, 5:30am
 
Ahh, thanks for introducting me to python. I'll keep it in mind next time i have to use processing! Smiley I guess indenting does make it easier to look at o_o

and arrays, gosh, i think i'll save that till I am abit of saavy with the coding first.

Thankyou for your time blindfish and phil!
Re: switch statements?
Reply #10 - Nov 5th, 2009, 6:05am
 
Python

If I were you I'd stick to Processing for the time being...
Page Index Toggle Pages: 1