Go Down

Topic: Working with Case & Switches (Read 1 time) previous topic - next topic

lloyddean

#5
Apr 05, 2013, 05:57 am Last Edit: Apr 05, 2013, 06:03 am by lloyddean Reason: 1
'switch' works with integer values only

Code: [Select]

switch ( random('a', 'z') )
{
    case 'a':
        ... some useful code here ...
        break;

    case 'b':
        ... some useful code here ...
        break;

    case 'c':
        ... some useful code here ...
        break;

    case 'd':
        ... some useful code here ...
        break;

    case 'd':
    case 'e':
    case 'f':
    case 'q':
        ... some useful code here ...
        break;

    default:
        // handle all other values ...
        break;
}


CrossRoads

@lloyddean, what are you saying?

"In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run. "

The examples show case 1, case 2, and I know case 'a', case 'b' work also.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

chipthomas

Is a "default" case required?  Is that why I am getting the error?


@lloyddean, what are you saying?

"In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run. "

The examples show case 1, case 2, and I know case 'a', case 'b' work also.

CrossRoads

No, a default case is not required. If none of the cases are met, the code just continues on after the end  }.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

chipthomas


No, a default case is not required. If none of the cases are met, the code just continues on after the end  }.


Can you tell if I am placing it correct in my code?

Go Up