<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="../../style.css">
<title>
Gambas Documentation - SELECT
</title>
</head>
<table class="none" width="100%" cellpadding="0" cellspacing="0" border="0">
<tr><td align="left">
<font size="-1">
<a href="../../help+en"><img class="flag" alt="Home" border="0" src="../../img/lang/en.png" align="center"></a>&nbsp;
<a href="../lang+en">Up</a>&nbsp;
<a href="seek2+en">Previous</a>&nbsp;
<a href="sgn+en">Next</a>&nbsp;
</td></tr></table>
<div class="notab">
<h1>
SELECT
</h1>
<div class="black"><font size="-2"><b>Syntax</b></font></div>
<pre class="syntax"><b>SELECT</b> [ <b>CASE</b> ] <u>Expression</u><br>
&nbsp;&nbsp;[ <b>CASE</b> <u>Expression</u> [ <b>TO</b> <u>Expression #2</u> ] [ <b>,</b> ... ]
&nbsp;&nbsp;&nbsp;&nbsp;... ]<br>
&nbsp;&nbsp;[ <b>CASE</b> <u>Expression</u> [ <b>TO</b> <u>Expression #2</u> ] [ <b>,</b> ... ]
&nbsp;&nbsp;&nbsp;&nbsp;... ]<br>
&nbsp;&nbsp;[ { <b>CASE ELSE</b> | <b>DEFAULT</b> }
&nbsp;&nbsp;&nbsp;&nbsp;... ]<br>
<b>END SELECT</b></pre><p>

Selects an expression to compare, and execute the code enclosed in the corresponding matching <a href="case+en">CASE</a> statement.
<p>
If no <a href="case+en">CASE</a> statement matches, the <a href="default+en">DEFAULT</a> or <a href="case+en">CASE</a> <a href="else+en">ELSE</a> statement is executed.
<p>
A <a href="case+en">CASE</a> statement is a list of single values or interval of two values separated by the <a href="to+en">TO</a> keyword.
<p>
<div class="gray"><font size="-2"><b>Example</b></font></div>
<pre class="example">' You want to check the random function of a die.
' So you repeat the random function a thousand times
' and you count, how many times 1, 2, 3, 4, 5 or 6
' have been thrown.

DIM x AS Integer
DIM w AS Integer
DIM a AS Integer
DIM b AS Integer
DIM c AS Integer
DIM d AS Integer
DIM e AS Integer
DIM f AS Integer

FOR x = 1 TO 1000

  w = Int(Rnd(6) + 1)

  SELECT CASE w
    CASE 1
      a = a + 1
    CASE 2
      b = b + 1
    CASE 3
      c = c + 1
    CASE 4
      d = d + 1
    CASE 5
      e = e + 1
    CASE 6
      f = f + 1
    CASE ELSE
      PRINT &quot;This is impossible!&quot;
  END SELECT

NEXT

PRINT a, b, c, d, e, f</pre>
<p>
<hr><b>See also</b><br>
<a href="../cat/test+en">Test Control Structures &amp; Functions</a>&nbsp;

</div>
</body>
</html>

