-1
\$\begingroup\$

I have three decision digital variable. So I have to make 8 decisions with these digital variables. The problem of mine is in understanding the behaviour of the switch-case statement in MikroC.

Actually I have to made decision as when three digital pin are 0 0 0, 0 0 1, 0 1 0. How to enter the these value for switch case variable?

switch (i) {
  case 1: i++;
  case 2: i++;
  case 3: i++;
}

Here my i is three digital pins, such as 0 1 0. How can I implement this?

\$\endgroup\$
  • 1
    \$\begingroup\$ In your example if i=1, you will increment it three times. Is that what you are intending? \$\endgroup\$ – Tom Carpenter Sep 22 '17 at 20:14
  • \$\begingroup\$ In any case, it's not clear what you are trying to do. \$\endgroup\$ – Tom Carpenter Sep 22 '17 at 20:14
  • 2
    \$\begingroup\$ MikroC is just a compiler that compiles bog standard ANSI C programs. There is no difference in behaviour from the way a switch-case statement works for any C program. \$\endgroup\$ – Tom Carpenter Sep 22 '17 at 20:19
  • 1
    \$\begingroup\$ I don't understand this method of programming. Looks like you have a requirement, then you code some random stuff and then you are trying to understand what you have coded and how it is matching your requirement? \$\endgroup\$ – Eugene Sh. Sep 22 '17 at 20:19
  • 1
    \$\begingroup\$ You want to make a decision when your input is 0, 1 or 2, yet your switch statement has cases for 1, 2 and 3. \$\endgroup\$ – Steve G Sep 22 '17 at 20:31
1
\$\begingroup\$

Your question is how to enter the case numbers. The answer is to enter them like this:

switch (i) {
case 0x00:   // do something if i == 000
  break; 
case 0x01:   // do something different if i == 001
  break; 
case 0x02:  // do something yet again if i == 010
default:   // do nothing (optional) 
}

If you don't use the break statements, the code 'falls through' to the next case, which may not be what you want (or maybe it is). You can leave out any case numbers that you don't need. Obviously the code you want to execute goes between the case and break statements.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.