switch constructs

I am working with Switches and I just am wondering:

Suppose a switch construct has 4 cases and the default. Also suppose the 2nd case is true. If the switch construct does not have a break statement which of the following is true



Answer this question

switch constructs

  • Chandu Sujay

    Hi,

    Yep you can't compile if there are no breaks in your case statement. The only exception on this rule if your case statement is empty.

    cheers,

    Paul June A. Domag



  • gnesbitt

    And if there were no breaks in that, it would promote error right
  • TonyMoore

    Hi,

    C# switch doesn't support code fall-through, except when the case is empty. So you need to place breaks in your case if the case contains a code. Example:

    switch(myBool) {
    case 1:
    x = 100;
    case 2:
    x = 200;
    break;

    }

    The statement above is not permitted.

    switch(myBool) {
    case 1:
    case 2:
    x = 200;
    break;

    }

    The statement above is permitted and would cause a fall-through.

    cheers,

    Paul June A. Domag



  • Jan Kučera

    ah....thank you Paul :)
  • JavaBoy

    Am I correct in thinking that only the first two lines would be executed
  • Ricardo Francisco

    If it didn't have a break, then it would promote error
  • switch constructs