|
|
View previous topic :: View next topic |
Author |
Message |
RT Guest
|
switch statement in CCS version 3.047 |
Posted: Wed Nov 28, 2001 7:48 am |
|
|
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 |
Posted: Wed Nov 28, 2001 11:18 am |
|
|
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 |
Posted: Wed Nov 28, 2001 2:54 pm |
|
|
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 |
|
|
|
|
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
|