CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

switch statement in CCS version 3.047

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
RT
Guest







switch statement in CCS version 3.047
PostPosted: Wed Nov 28, 2001 7:48 am     Reply with quote

Does it matter if a default case is put in a switch statement in CCS C? Has anyone ever tried this and run into a problem with the compiler generating a GOTO to the beginning of the switch repeatedly? I was under the impression that if no default was used and no cases were matched, the program would just fall through the switch without ever doing anything. Any info on this would be appreciated.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1349
Tomi
Guest







Re: switch statement in CCS version 3.047
PostPosted: Wed Nov 28, 2001 11:18 am     Reply with quote

Yes, it does.
If you don't have default and there are no cases were matched, nothing is executed in switch().
:=I was under the impression that if no default was used and no cases were matched, the program would just fall through the switch without ever doing anything.
Exactly, this is the definition of the switch() statement.
If you have default, it is executed if no other cases were matched. It is a simple puzzle:
char i,j=1;
i = 10;
switch (i) {
case 0: j=0;
break;
default: j=3;
break;
}
j++;
In this case "j=3" is executed, so j=4 after the increment.
char i,j=1;
i = 10;
switch (i) {
case 0: j=0;
break;
}
j++;
In this case j=2 after the increment because the switch() statement is NOT executed anyway.

The list of the first example:
.................... switch (i) { // i=10
002B: MOVF 22,W
002C: MOVWF 77
002D: BTFSC 03,2
002E: GOTO 030
002F: GOTO 032 // goto default
.................... case 0: j=0;
0030: CLRF 23
.................... break;
0031: GOTO 035
.................... default: j=3;
0032: MOVLW 03
0033: MOVWF 23 // load 3 into j
.................... break;
0034: GOTO 035 // continue with the first after switch()
.................... }j++;
0035: INCF 23,F // increment
.................... }


:=Does it matter if a default case is put in a switch statement in CCS C? Has anyone ever tried this and run into a problem with the compiler generating a GOTO to the beginning of the switch repeatedly? I was under the impression that if no default was used and no cases were matched, the program would just fall through the switch without ever doing anything. Any info on this would be appreciated.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1353
RT
Guest







Re: switch statement in CCS version 3.047
PostPosted: Wed Nov 28, 2001 2:54 pm     Reply with quote

Yup. That's what I thought. Thanks Tomi for checking it out.
___________________________
This message was ported from CCS's old forum
Original Post ID: 1358
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group