Thread: switch syntax

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    9

    switch syntax

    ok so I sort of understand how the switch case works but I cant get the syntax quite right for what I want to do.

    Background info-
    ok so I have a PIC controller that i am programming in C. I have three inputs A B and C. and an output X

    What I want to do-

    the Idea is that I want to make a variable (lets pick "n") and asign a number to that variable depending on what button (or imput A-C) is pressed. that number would then switch the case. so if imput A == 0 then n ==1 and if B == 0 then n == 2 etc. then depending on what n equals I want the corrisponding case # to run. then the Case 1 would give the output X a value of 1 and CASE 2 would give the output X a value of 2 etc.

    I hope I made myself clear. I am new and would like a little help to get started! thanks for your help!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Gutiarplyrinak View Post
    ok so I sort of understand how the switch case works but I cant get the syntax quite right for what I want to do.

    Background info-
    ok so I have a PIC controller that i am programming in C. I have three inputs A B and C. and an output X

    What I want to do-

    the Idea is that I want to make a variable (lets pick "n") and asign a number to that variable depending on what button (or imput A-C) is pressed. that number would then switch the case. so if imput A == 0 then n ==1 and if B == 0 then n == 2 etc. then depending on what n equals I want the corrisponding case # to run. then the Case 1 would give the output X a value of 1 and CASE 2 would give the output X a value of 2 etc.

    I hope I made myself clear. I am new and would like a little help to get started! thanks for your help!
    Well... you could try something like this...

    Code:
    switch (n)
      { 
         case 1 : 
            // call procedure for #1 here
            break;
         case 2 :
           // call procedure for #2 here
           break;
         case 3 : 
           // call procedure for #3 here
           break;
         default :
           puts("SimPal Cindy has a boo boo");
        }
    I'm not much of a fan of putting actual code into switch statements. It is a lot cleaner and less error prone to use it to call procedures. You'll understand the first time you have to fix a broken switch-case in a Windows message loop...

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    9
    yah I understand how that part works but I dont understand how to make an if statement or some other statement to change which case I am running

    and how would you call procedures?? Again I am pretty new to all of this

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Gutiarplyrinak View Post
    yah I understand how that part works but I dont understand how to make an if statement or some other statement to change which case I am running
    I'm sorry... what if statement?

    Post your code... lets see what you've got so far...

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    9
    Code:
    var testimputs (var){
    while (1){
    if (PICBbits.RB5 = 0) //if imput A = 0
    n==1;
    if (PICBbits.RB4 = 0) //if imput B = 0
    n==2;
    if (PICBbits.RB3 = 0) //if imput C = 0
    n==3;
    else
    n==0
    }
    
    var main (var){
    switch (n){
    case 0 :
        /*code 0 here/*
    case 1 :
       /*code 1 here/*
    case 2 :
       /*code 2 here/*
    case 3 :
       /*code 3 here/*
    }
    }

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Gutiarplyrinak View Post
    [CODE]var testimputs (var){
    while (1){
    if (PICBbits.RB5 = 0) //if imput A = 0
    n==1;
    if (PICBbits.RB4 = 0) //if imput B = 0
    n==2;
    if (PICBbits.RB3 = 0) //if imput C = 0
    n==3;
    else
    n==0
    }
    You don't need a switch statement here... you already have it... Here's a made up example to give you the idea... Of course you're going to have to develop your own final version...

    Code:
    void DoB5On(void)
      {  
         Beep(1000);   
         Sleep(1000);  
         Beep(0);         
       }
    
    void DoB4On(void)
       { 
          StartMotor(7,CLOCKWISE) ;
          Sleep(1000);
        }
    
    void DoB3On(void)
       { 
          StopMotor(7);
          Beep(1000);
          Sleep(10)
          Beep(0);
        }
    
    int main(void)
      {
        while (1)
          {
             if (PICBbits.RB5 == 0)
                DoB5On();
             elseif (PICBbits.RB4 == 0)
                DoB4On();
             elseif (PICBbits.RB3 == 0)
                DoB3On();
           }  
         return 0;
      }
    Never mind that the function calls are just made up trash... look at how the button presses are handled in main and how the procedure for each is called as a result.

  7. #7
    Registered User
    Join Date
    Dec 2010
    Posts
    9
    THANK YOU!!! This was what I was trying to do origionally but someone told me it was better to do a switch (programmers like to do their own stuff you know). but I was trying origionally trying to do what you just showed, I just couldnt figure out how. so thank you

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Gutiarplyrinak View Post
    THANK YOU!!! This was what I was trying to do origionally but someone told me it was better to do a switch (programmers like to do their own stuff you know). but I was trying origionally trying to do what you just showed, I just couldnt figure out how. so thank you
    No worries... the if -- elseif construct is very much like a switch statement anyway, so why not use it?

    You may find this link helpful as well...

    Cprogramming.com Tutorial: Introduction to C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  2. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

Tags for this Thread