Chapter 4: Control Structures


Introduction

Statements define algorithmic actions within a program. Simple statements - like assignments and procedure calls - can be combined to form loops, conditional statements, and other structured statements.

Simple statement does not contain any other statements. Simple statements include assignments, and calls to procedures and functions.

Structured statements are constructed from other statements. Use a structured statement when you want to execute other statements sequentially, conditionally, or repeatedly.

4.1 Conditional Statements

Conditional statements are used for change the flow of the program execution upon meeting a certain condition. The BASIC instruction of branching in BASIC language is the IF instruction, with several variations that provide the necessary flexibility.

4.1.1 IF..THEN Statement – conditional program branching

Syntax
if expression then
  statements1
[ else
  statements2 ]
end if
Description

Instruction selects one of two possible program paths. Instruction IF..THEN is the fundamental instruction of program branching in PIC BASIC and it can be used in several ways to allow flexibility necessary for realization of decision making logic.

Expression returns a True or False value. If expression is True, then statements1 are executed; otherwise statements2 are executed, if the else clause is present. Statements1 and statements2 can be statements of any type.

Example

The simplest form of the instruction is shown in the figure below. Our example tests the button connected to RB0 - when the button is pressed, program jumps to the label "Add" where value of variable "w" is increased. If the button is not pressed, program jumps back to the label "Main".

dim j as byte

Main:

  if PORTB.0 = 0 then

    goto Add

  end if

  goto Main

Add: j = j + 1

end.

More complex form of instruction is program branching with the ELSE clause:

dim j as byte

Main:

  if PORTB.0 = 0 then
    j = j + 1

  else
    j = j - 1

  endif

  goto Main

end.

4.1.2 SELECT..CASE Statement – Conditional multiple program branching

Syntax
select case Selector
   case Value_1
      Statements_1
   case Value_2
      Statements_2
   ...
   case Value_N
      Statements_n
 [ case else
      Statements_else ]
end select
Description

Select Case statement is used for selecting one of several available branches in the program course. It consists of a selector variable as a switch condition, and a list of possible values. These values can be constants, numerals, or expressions.

Eventually, there can be an else statement which is executed if none of the labels corresponds to the value of the selector.

As soon as the Select Case statement is executed, at most one of the statements statements_1 .. statements_n will be executed. The Value which matches the Selector determines the statements to be executed.

If none of the Value items matches the Selector, then the statements_else in the else clause (if there is one) are executed.

Example
select case W
    case 0
       B = 1
      PORTB = B
    case 1
       A = 1
       PORTA = A
    case else
       PORTB = 0
end select

...

select case Ident
  case testA
     PORTB = 6
     Res = T mod 23
  case teB + teC
     T = 1313
  case else
     T = 0
end select

4.1.3 GOTO Statement – Unconditional jump to the specified label

Syntax
goto Label
Description

Goto statement jumps to the specified label unconditionally, and the program execution continues normally from that point on.

Avoid using GOTO too often, because over-labeled programs tend to be less intelligible.

Example
program test

main:

 ' some instructions ...

goto myLabel

 ' some instructions...

myLabel:

 ' some instructions...

end.

4.2 Loops

Loop statements allow repeating one or more instructions for a number of times. The conducting expression determines the number of iterations loop will go through.

4.2.1 FOR Statement – Repeating of a program segment

Syntax
for counter = initialValue to finalValue [step step_value]
  statement_1
  statement_2
  ...
  statement_N
next counter
Description

For statement requires you to specify the number of iterations you want the loop to go through.

Counter is variable; initialValue and finalValue are expressions compatible with counter; statement is any statement that does not change the value of counter; step_value is value that is added to the counter in each iteration. Step_value is optional, and defaults to 1 if not stated otherwise. Be careful when using large values for step_value, as overflow may occur.

Every statement between for and next will be executed once per iteration.

Example

Here is a simple example of a FOR loop used for emitting hex code on PORTB for 7-segment display with common cathode. Nine digits should be printed with one second delay.

for i = 1 to 9
    portb = i
    delay_ms(1000)
next i

4.2.2 DO..LOOP Statement – Loop until condition is fulfilled

Syntax
do
  statement_1
  ...
  statement_N
loop until expression
Description

Expression returns a True or False value. The do..loop statement executes statement_1; ...; statement_N continually, checking the expression after each iteration. Eventually, when expression returns True, the do..loop statement terminates.

The sequence is executed at least once because the check takes place in the end.

Example
I = 0

do
    I = I + 1    ' execute these 2 statements
    PORTB = I    '     until i equals 10 (ten)
loop until I = 10

4.2.3 WHILE Statement – Loop while condition is fulfilled

Syntax
while expression
  statement_0
  statement_1
  ...
  statement_N
wend
Description

Expression is tested first. If it returns True, all the following statements enclosed by while and wend will be executed (or only one statement, alternatively). It will keep on executing statements until the expression returns False.

Eventually, as expression returns False, while will be terminated without executing statements.

While is similar to do..loop, except the check is performed at the beginning of the loop. If expression returns False upon first test, statements will not be executed.

Example
while I < 90
  I = I + 1
wend

 ...

while I > 0
  I = I div 3
  PORTA = I
wend

4.3 ASM Statement – Embeds assembly instruction block

Syntax
asm
  statementList
end asm
Description

Sometimes it can be useful to write part of the program in assembly. ASM statement can be used to embed PIC assembly instructions into BASIC code.

Note that you cannot use numerals as absolute addresses for SFR or GPR variables in assembly instructions. You may use symbolic names instead (listing will display these names as well as addresses).

Be careful when embedding assembly code - BASIC will not check if assembly instruction changed memory locations already used by BASIC variables.

Example
asm
  movlw 67
  movwf TMR0
end asm