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

Selective if then statement

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



Joined: 25 Apr 2011
Posts: 297

View user's profile Send private message

Selective if then statement
PostPosted: Thu Mar 03, 2022 2:32 pm     Reply with quote

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 Smile

[img]https://ibb.co/5xnTwqf[/img]
PrinceNai



Joined: 31 Oct 2016
Posts: 479
Location: Montenegro

View user's profile Send private message

PostPosted: Thu Mar 03, 2022 5:49 pm     Reply with quote

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: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 03, 2022 7:22 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Mar 04, 2022 1:28 am     Reply with quote

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:
Quote:
Case 1

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: 19512

View user's profile Send private message

PostPosted: Fri Mar 04, 2022 2:25 am     Reply with quote

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: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Mar 04, 2022 5:49 am     Reply with quote

re: break;

I've always added them to cases, 'somehow' my fingers do that automatically.....so I assume that everyone else does. Rolling Eyes
PrinceNai



Joined: 31 Oct 2016
Posts: 479
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Mar 04, 2022 6:58 am     Reply with quote

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: 9226
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Mar 04, 2022 7:04 am     Reply with quote

Really ? Someone else's fingers make mistakes ?? Laughing
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: 479
Location: Montenegro

View user's profile Send private message

PostPosted: Fri Mar 04, 2022 8:55 am     Reply with quote

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: 19512

View user's profile Send private message

PostPosted: Fri Mar 04, 2022 11:19 am     Reply with quote

I find trying to type, while holding a telephone is the 'sure way' of hitting
the wrong keys...
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