ooPIC Logo

Case Selection Construct

Main Index
Language Idx
Back to top of pageDescription:
 

A decision structure used for selectively executing one block of statements from among several blocks of statements in a multiple-choice fashion.

Back to top of pageSyntax:
 

The following shows the format of the Case Selection Construct syntax.
Visual Basic & BASICC & Java
Select Case <expression>
  Case <expresionlist1>
    <statements1>
  Case <expresionlist2>
    <statements2>
  Case <expressionistX>
    <statementsX>
  Case Else
    <statementsD>
End Select
switch (<expression>)
{
  Case <expresionlist1>:
    <statements1>
    [break;]
  Case <expresionlist2>:
    <statements2>
    [break;]
  Case <expressionistX>:
    <statementsX>
    [break;]
  default:
    <statementsD>
}

The following table lists the elements of the Case Selection Construct syntax.
ElementDescription
Basic, C & Java
<expressionlist1>Any valid list of expressions.
<expressionlist2>Any valid list of expressions.
<expressionlistX>Any valid list of expressions.
<statements1>One or more statements executed if the first case is true.
<statements2>One or more statements executed if the second case is true.
<statementsX>One or more statements executed if the Xth case is true.
<statementsD>One or more statements executed if no other case is true

Back to top of pageOperation:
 

Basic Syntax:
When a Case Selection Construct is encountered during the program's execution, the test expression specified by <expression> is evaluated once, and the result of that evaluation is compared to the values specified by the <expressionlist...> elements for each Case keyword encountered in the structure. If there is a match, then the program flow skips to and executes the statements specified by the <statement...> element associated with that case's expression. Once the statements have been executed, the program flow skips to the statement following the Case Selection Construct at which point the program's execution resumes.

If more than one of the case expressions matches the test expression then only the statements associated with the first matching case expression will be executed.

If none of the case expressions matches the test expression then the program flow skips to the statement following the Case Selection Construct at which point the program's execution resumes, unless the optional CASE ELSE keywords are encountered in the structure, in which case, the program flow skips to and executes the statements specified by the <statementsD> element. and then skips to the statement following the Case Selection Construct.

C and Java Syntax:
When a Case Selection Construct is encountered during the program's execution, the test expression specified by <expression> is evaluated once, and the result of that evaluation is compared to the values specified by the <expressionlist...> elements for each Case keyword encountered in the structure. If there is a match, then the program flow skips to and executes the statements specified by the <statement...> element associated with that case's expression. Once the statements have been executed, the program flow executes all of the following case's statements as well, unless the optional break keyword is encountered, in which case, the program flow skips to the statement following the Case Selection Construct at which point the program's execution resumes.

If more than one of the case expressions matches the test expression then the program flow skips to the first matching case statements and begins executed at that point.

If none of the case expressions matches the test expression then the program flow skips to the statement following the Case Selection Construct at which point the program's execution resumes, unless the optional default keyword is encountered in the structure, in which case, the program flow skips to and begins executes the statements specified by the <statementsD> element.

NOTE:
The ooPIC 5.0.1 compiler allows only ONE select/switch construct to have a "case else" or "default" option.

BUG: As of compiler 5.0.1 nested select/switch constructs will get unexpected results.,

Back to top of pageExample:
 

In the following example, a Case Selection Construct is shown that executes one of 3 different groups of statements depending on the value of A. In one particular case, if the value of A was equal to 3, then the statements in <statements3> are executed.
Visual Basic & BASICC & Java
Select Case A
  Case 1 : <statements1>

  Case 2 : '<statements2>

  Case 3 : '<statements3>

End Select
Switch (A){
  Case 1 : <statements1> 
           Break;
  Case 2 : '<statements2> 
           Break;
  Case 3 : '<statements3> 
           Break;
}

In the following example, a Case Selection Construct is shown that executes one of 3 different groups of statements depending on the value of B. In one particular case, if the value of B was equal to 2, then the statements in <statements2> and in <statements3> are executed, but if the value of B as equal to 3, then the statements only in <statements3> are executed.
Visual Basic & BASICC & Java
Not possible using only the Do Loop Construct in Basic syntax.
To skip the statements within <statements2>, place them within an If Selection Construct as shown:
Select Case B.Value
  Case 1 : <statements1>

  Case 2, 3
    If B.Value = 2 Then
      '<statements2>
    End If
    '<statements3>
End Select
Switch (B){
  Case 1 : <statements1>
           Break;
  Case 2 : '<statements2>:
           If (B == 2){
            '<statements3> 
	         }
           Break;
}

In the following example, a Case Selection Construct is shown that executes one of 4 different groups of statements depending on the value of A. If A is not one of the values 1, 2 or 3, then the statements in <statements4> are executed.
Visual Basic & BASICC & Java
Select Case A
  Case 1 : <statements1>

  Case 2 : '<statements2>

  Case 3 : '<statements3>

  Case Else : '<statements4>
End Select
Switch (A){
  Case 1 : <statements1> 
           Break;
  Case 2 : '<statements2> 
           Break;
  Case 3 : '<statements3> 
           Break;
  Default : '<statements4>
}

Back to top of pageRelated Items:

 If Selection Construct
Back to top of pageVersion History and Bug List:
 Firmware Ver A1: Introduced.

Bugs: No known bugs.


ooPIC Compiler Ver 6.0 (c) Copyright 1997 - 2007 Savage Innovations, LLC.