View previous topic :: View next topic |
Author |
Message |
aaronik19
Joined: 25 Apr 2011 Posts: 297
|
Selective if then statement |
Posted: Thu Mar 03, 2022 2:32 pm |
|
|
Dear Friends, I have a question and maybe this might be used amongst others. I have a loop where I used a switch case, and inside every case, there is multiple if..then statement. Now since the case is being selected at the initialization of the program, is it possible to create a code that if the user select Case2, case 1, 3 and 4 are being ignored to program faster?
Sorry I have no code because I do not know how I can implement it but hope that someone understand me
[img]https://ibb.co/5xnTwqf[/img] |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 478 Location: Montenegro
|
|
Posted: Thu Mar 03, 2022 5:49 pm |
|
|
How is the user selecting Case2? How fast do you need it to be? For how long are you staying inside each case? The way I see it, switch statement or a state machine is meant to do a few quick things and then exit the case with a break;. If case variable stays the same, do the same thing again and again and again. No delays inside cases, they are handled by timer interrupts and counters at the backstage. If the case variable changes, go there. Have you thought of transforming those if-then statements to more cases, if it is doable? Might be a longer code, but easily readable.
And after you change a state variable and then exit the case with a break; , all other cases will be ignored until that variable changes again. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Thu Mar 03, 2022 7:22 pm |
|
|
It's unclear to me what you mean about 'if not use, program faster' .
...
case 1,3,4 ARE being ignored when case 2 is selected !
If you dump the listing, you'll see how the code executes..
Typically SWITCH() will be coded as a series of 'IF' conditionals...
if(x==1) {do all this}
else
if(x==2) [ do all this stuff}
else
...
if(x==4) [do these things}
Now when SWITCH has 8 or more(?) possible CASES, the compiler creates a 'jump table', so there will be a table of addresses where the program will GOTO, in order to 'do all this stuff' related to a CASE number.
I believe the compiler change coding from if-then-elses to jump-tables to reduce code space and speed up execution. Originally(20+ years ago) it was 8 entries...might be more or less today.....
others will know, whatever it is , it's the best(efficient) code that can be done. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Mar 04, 2022 1:28 am |
|
|
temtronic, he needs to add break statements as PrinceNai said.
Example:
The program posted below displays this output in the MPLAB v8.92
simulator output window:
Test program:
Code: | #include <16F1847.h>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT
#use delay(clock=4M)
#use rs232(UART1, baud=9600, ERRORS)
//====================================
void main()
{
int8 value = 1;
switch(value)
{
case 0:
printf("Case 0 \r");
break;
case 1:
printf("Case 1 \r");
break;
case 2:
printf("Case 2 \r");
break;
case 3:
printf("Case 3 \r");
break;
default:
printf("Default Case \r");
break;
}
while(TRUE);
} |
If you leave off the break statements, you get this output:
Quote: | Case 1
Case 2
Case 3
Default Case |
I believe that's what he's complaining about. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19495
|
|
Posted: Fri Mar 04, 2022 2:25 am |
|
|
and also in terms of 'speed', if he is using a PIC16 or a PIC18, and codes
the switch for at least 5 cases (this varies with compiler version), and does
not have a 'default', the switch will be generated as a jump table, which
is really fast. This is why I always do my 'default' handling as a separate
test. So:
Code: |
#include <18F2520.h>
#device ADC=10
#FUSES NOWDT //No Watch Dog Timer
#use delay(crystal=20000000)
#use RS232(UART1, ERRORS) //default UART for debugging
void main()
{
int testval;
setup_adc_ports(NO_ANALOGS, VSS_VDD);
while (TRUE)
{
for (testval=0;testval<11;testval++)
{
if (testval<2 || testval>9)
{
//default handling
delay_ms(100); //just do something
continue; //so misses switch
}
switch (testval)
{
case 2:
printf("Case 2\n");
break;
case 3:
printf("Case 3\n");
break;
case 4:
printf("Case 4\n");
break;
case 5:
printf("Case 5\n");
break;
case 6:
printf("Case 6\n");
break;
case 7:
printf("Case 7\n");
break;
case 8:
printf("Case 8\n");
break;
case 9:
printf("Case 9\n");
break;
}
delay_ms(1000); //slow down the print
}
}
}
|
Will actually generate a simple addition to the program counter to jump
to each of the cases. Extremely fast indeed (as fast as is possible in fact!...).
It is a 'optimisation', that is well worth being aware of.
If you add the 'default' to the switch, this optimisation disappears... |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Mar 04, 2022 5:49 am |
|
|
re: break;
I've always added them to cases, 'somehow' my fingers do that automatically.....so I assume that everyone else does. |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 478 Location: Montenegro
|
|
Posted: Fri Mar 04, 2022 6:58 am |
|
|
I have a template for a switch with 10 or so cases, with all the braces and breaks already there. Just copy it to the program and go from there. It saves a lot of typing and "expecting a }" error messages :-) |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9221 Location: Greensville,Ontario
|
|
Posted: Fri Mar 04, 2022 7:04 am |
|
|
Really ? Someone else's fingers make mistakes ??
Have to admit I have a few 'premade-fill in the blanks' as well !!!
Especially since these eyes can't easily see ']' vs '}' or ';' vs ':'..... |
|
|
PrinceNai
Joined: 31 Oct 2016 Posts: 478 Location: Montenegro
|
|
Posted: Fri Mar 04, 2022 8:55 am |
|
|
Quote: |
Really ? Someone else's fingers make mistakes ??
|
I blame those mistakes on a sheer speed of two finger typing :-) |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19495
|
|
Posted: Fri Mar 04, 2022 11:19 am |
|
|
I find trying to type, while holding a telephone is the 'sure way' of hitting
the wrong keys... |
|
|
|