SELECT-CASE-END SELECT

From MCS Wiki AVR
(Difference between revisions)
Jump to: navigation, search
(Created page with "SELECT-CASE-END SELECT <br/><source lang="bascomavr"> </source><br/>{{Languages}} Category:BASCOM Language Reference")
 
 
Line 1: Line 1:
SELECT-CASE-END SELECT
+
= <span class="f_Header">Action</span> =
  
<br/><source lang="bascomavr">
+
Executes one of several statement blocks depending on the value of an expression.
  
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
= <span class="f_Header">Syntax</span> =
 +
 +
<span class="f_Syntax">SELECT CASE</span>&nbsp;var
 +
 +
<span class="f_Syntax">&nbsp; CASE</span>&nbsp;test1&nbsp;: statements
 +
 +
[<span class="f_Syntax">CASE</span>&nbsp;test2&nbsp;: statements ]
 +
 +
<span class="f_Syntax">CASE ELSE&nbsp;</span>: statements
 +
 +
<span class="f_Syntax">END SELECT</span>
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
= <span class="f_Header">Remarks</span> =
 +
<div style="padding: 0px; margin: 0px 0px 0px 4px;">
 +
{| width="488" cellspacing="0" cellpadding="1" border="1" style="border: 2px solid rgb(0, 0, 0); border-spacing: 0px; border-collapse: collapse;"
 +
|- style="vertical-align: top;"
 +
| valign="top" width="15%" style="width: 69px; border: 1px solid rgb(0, 0, 0);" |
 +
Var
 +
 +
| valign="top" width="100%" style="width: 410px; border: 1px solid rgb(0, 0, 0);" |
 +
Variable to test the value of
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="15%" style="width: 69px; border: 1px solid rgb(0, 0, 0);" |
 +
Test1
 +
 +
| valign="top" width="100%" style="width: 410px; border: 1px solid rgb(0, 0, 0);" |
 +
Value to test for.
 +
 +
|- style="vertical-align: top;"
 +
| valign="top" width="15%" style="width: 69px; border: 1px solid rgb(0, 0, 0);" |
 +
Test2
 +
 +
| valign="top" width="100%" style="width: 410px; border: 1px solid rgb(0, 0, 0);" |
 +
Value to test for.
 +
 +
|}
 +
</div>
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
You can test for conditions to like:
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
CASE IS > 2&nbsp;:
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
Another option is to test for a range&nbsp;:
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
CASE 2 TO 5&nbsp;:
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
= <span class="f_Header">See also</span> =
 +
 +
[[IF-THEN-ELSE-END IF|IF THEN]]
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
<span style="font-family: Arial;">&nbsp;</span>
 +
 +
= <span class="f_Header">Example</span> =
 +
 +
<br/><source lang="bascomavr">
 +
'-----------------------------------------------------------------------------------------
 +
'name                     : case.bas
 +
'copyright                : (c) 1995-2005, MCS Electronics
 +
'purpose                  : demonstrates SELECT CASE statement
 +
'micro                    : Mega48
 +
'suited for demo          : yes
 +
'commercial addon needed  : no
 +
'-----------------------------------------------------------------------------------------
 +
 
 +
$regfile = "m48def.dat"                                   ' specify the used micro
 +
$crystal = 4000000                                         ' used crystal frequency
 +
$baud = 19200                                               ' use baud rate
 +
$hwstack = 32                                               ' default use 32 for the hardware stack
 +
$swstack = 10                                               ' default use 10 for the SW stack
 +
$framesize = 40                                             ' default use 40 for the frame space
 +
 
 +
 
 +
Dim I As Byte                                             'dim variable
 +
Dim S As String * 5 , Z As String * 5
 +
 
 +
Do
 +
 
 +
Input "Enter value (0-255) " , I
 +
Select Case I
 +
  Case 1 : Print "1"
 +
  Case 2 : Print "2"
 +
  Case 3 To 5 : Print "3-5"
 +
  Case Is >= 10 : Print ">= 10"
 +
  Case Else : Print "Not in Case statement"
 +
End Select
 +
Loop
 +
End
 +
 
 +
'note that a Boolean expression like > 3 must be preceded
 +
'by the IS keyword
 
</source><br/>{{Languages}}
 
</source><br/>{{Languages}}
  
 
[[Category:BASCOM Language Reference]]
 
[[Category:BASCOM Language Reference]]

Latest revision as of 00:55, 12 February 2013

Contents

Action

Executes one of several statement blocks depending on the value of an expression.

 

 

Syntax

SELECT CASE var

  CASE test1 : statements

[CASE test2 : statements ]

CASE ELSE : statements

END SELECT

 

 

Remarks

Var

Variable to test the value of

Test1

Value to test for.

Test2

Value to test for.

 

 

You can test for conditions to like:

 

CASE IS > 2 :

 

Another option is to test for a range :

 

CASE 2 TO 5 :

 

 

See also

IF THEN

 

 

 

Example


'-----------------------------------------------------------------------------------------
'name                     : case.bas
'copyright                : (c) 1995-2005, MCS Electronics
'purpose                  : demonstrates SELECT CASE statement
'micro                    : Mega48
'suited for demo          : yes
'commercial addon needed  : no
'-----------------------------------------------------------------------------------------
 
$regfile = "m48def.dat"                                   ' specify the used micro
$crystal = 4000000                                         ' used crystal frequency
$baud = 19200                                               ' use baud rate
$hwstack = 32                                               ' default use 32 for the hardware stack
$swstack = 10                                               ' default use 10 for the SW stack
$framesize = 40                                             ' default use 40 for the frame space
 
 
Dim I As Byte                                             'dim variable
Dim S As String * 5 , Z As String * 5
 
Do
 
Input "Enter value (0-255) " , I
Select Case I
  Case 1 : Print "1"
  Case 2 : Print "2"
  Case 3 To 5 : Print "3-5"
  Case Is >= 10 : Print ">= 10"
  Case Else : Print "Not in Case statement"
End Select
Loop
End
 
'note that a Boolean expression like > 3 must be preceded
'by the IS keyword

Languages   English Deutsch  
Personal tools
Namespaces
Variants
Actions
Navigation
In other languages
Language