---
title: "Control construction"
canonical: "https://docs.cuesystem.com/space/CVCHELP/508985352/Control%20construction"
format: markdown
---
> Macro (toc)

---

# If

The command conditionally executes a group of statements, depending on the value of a condition. The statements after the first condition, which have been evaluated as True, are executed. If none of the conditions are evaluated as True, the Else statements will be executed. The multiple Elself blocks may appear. The blocks Elself and Else are optional.

#### Syntax

```visualbasic
If BooleanExpression Then
  [Statements]
[ElseIf Condition1 Then
  Statements]
[ElseIf Condition2 Then
  Statements]
[ElseIf ConditionN Then
  Statements]
[Else
  Statements]
End If
```

#### Example

```visualbasic
Var b As Boolean
Var i As Long := 1

If b Then
  i := 0
End If
```

#### Example

```visualbasic
Var i As Long
Var s As Text[100]

If i < 0 Then
  s := "Negative"
Else
  s := "Non-negative"
End If
```

# Select

The command executes one of several groups of statements, where a value of SelectExpression first corresponds to the expression. If SelectExpression does not correspond to any expression, the Case Else statements are executed.

The command is analogical to the If command. In a contrast to the If command, it evaluates only one SelectExpression and then compares it to other expressions. It is suggested to use the Select command when such construction is more transparent than the If command.

#### Syntax

```visualbasic
Select SelectExpression
  Case Expression [To ToExpression] [, NextExpression]
    [Statements]
  [Case Else
    Statements]
End Select
```

#### Example

```visualbasic
Var i As Long
Var s As Text[100]

Select i
  Case 0
    s := "Zero"
  Case 1, 3, 5, 7, 9
    s := "Small odd"
  Case 10 To 20
    s := "From 10 to 20"
  Case 50 To 100, 500
    s := "From 50 to 100 and 500"
  Case Else
    s := "Error"
End Select
```

# Do-While

The cycle is a conditional cycle with the evaluation at the beginning. At the beginning of each cycle, the expression is evaluated and tested. If the expression is True, all the cycle statements will be executed. If the expression is False, the cycle will end.

If the expression is not already fulfilled at the beginning, the cycle will not execute any. The optional Exit Do command evokes an immediate cycle exit.

#### Syntax

```visualbasic
Do While BooleanExpression
  [Statements]
  [Exit Do]
End Do
```

#### Example

```visualbasic
Var a[100] As Long
Var i As Long := 1

Do While (a[i] <> 0) And (i < 100)
  i += 1
End Do
```

# Do-Loop

The cycle is a conditional cycle with the evaluation at the end. At the end of each cycle, the expression is evaluated and tested. If the expression is True, all the cycle statements will be executed again. If the expression is False, the cycle will end.

Since the condition is evaluated at the end of the cycle, the cycle statements are executed at least once. The optional Exit Do command evokes an immediate cycle exit.

#### Syntax

```visualbasic
Do
  [Statements]
  [Exit Do]
Loop While BooleanExpression
```

#### Example

```visualbasic
Var a[100] As Long
Var i As Long := 1
Var sum As Long := 0

Do
  sum += a[i]
  i += 1
Loop While (sum < 1000000) And (i <= 100)
```

# Do

The cycle is nonconditional. The cycle is executed permanently. It can be terminated only by the optional Exit Do command.

#### Syntax

```visualbasic
Do
  [Statements]
  [Exit Do]
End Do
```

#### Example

```visualbasic
Process ReadData()
  Do
    // read data
    // wait
  End Do
End Process
```

#### Example

```visualbasic
Process ReadData()
  Var Err As Long := 0

  // ReadData started
  Do
    // read data, Err := error status
    // wait
    If Err <> 0 Then
      Exit Do
    End If
  End Do
  // ReadData finished
End Process
```

# For

This cycle repeats a group of statements a specified number of times.

It works in followed steps:

1. Values of StartExpression, EndExpression, and StepExpression are evaluated. They are evaluated only once, before the first cycle loop.
2. The cycle statements are executed.
3. The value of the StepExpression is added to the Counter.
4. 
  1. If StepExpresssion > 0  - if a control condition (Counter <= EndExpression) is true program continues in the step 2, otherwise the cycle will end
  2. If StepExpression <  0  - if a control condition (Counter >= EndExpression) is true program continues in the step 2, otherwise the cycle will end
  3. If StepExpression  = 0  - the cycle will end

It means that steps 2, 3 and 4 are periodically executed while the control condition is true.

> ⚠️ The cycle is executed once at least. If you want to check the control condition **before** the loop starts, use the Do-While cycle.

> ⚠️ Values of StartExpression, EndExpression, and StepExpression are evaluated only once, before the first cycle loop. This is in contrast to the Do-While/Do-Loop loop, where the expression for the control condition is evaluated in each loop.

> 📝 If StepExpression is equal to 0, the cycle is executed only once.

> ℹ️ If StepExpression is not entered it will equal one by default.

The optional Exit For command can be used for the early exit. Counter, StartExpression, EndExpression, and StepExpression are the Byte, Long, Double, Char, and Time data types.

#### Syntax

```visualbasic
For Counter := StartExpression To EndExpression [Step StepExpression]
  [Statements]
  [Exit For]
End For
```

#### Example

```visualbasic
// array initialization
Var a[100] As Long
Var i As Long

For i := 1 To 100
  a[i] := 2 * i
End For
```

#### Example

```visualbasic
// more dimensional array initialization
Var a[MAX_X, MAX_Y] As Long
Var x As Long
Var y As Long

For x := 1 To MAX_X
  For y := 1 To MAX_Y
  a[x, y] := 0
  End For
End For
```

#### Example

```visualbasic
// arrays multiplication
Var a[100] As Long
Var b[100] As Long
Var c[100] As Long
Var i As Long

For i := 1 To 100
  c[i] := a[i] * b[i]
End For
```

# GoTo

The command is a jump to a specified program position. The command enables to jump within the same program block or into the parent block. It is only possible to jump to a line marked with a label. The label is an identifier that is entered at the beginning of the line and is followed by a colon. The name of the label must not collide with another label identifier.

#### Syntax

```visualbasic
GoTo Label
```

#### Example

```visualbasic
If a = 0 Then
  GoTo DivisionByZero
End If

DivisionByZero:
```