GOTO statement

Normally program statements are executed in sequence.
e.g. given the sequence:
x = 0
x = x + 1
x = x + 1
x = x + 1
at this point x will have had 1 added to it three times and it will contain the value 3.

The goto statement allows us to break the sequence of execution by forcing the program to continue executing from the point indicated by a label.

A label looks like a variable name but it is terminated by the colon character ':'

x = 0
goto lab1
x = 1

lab1:

// at this point x is 0 because the x = 1 statement was jumped over
goto statements are not necessary for structured programming and many programmers frown on their use. However there are two very important reasons why XCSB supports them.
  1. converting algorithms written in other languages that support gotos is greatly simplified if XCSB also supports gotos
  2. optimisation issues
e.g. (1) with goto
for j=0 while j<arr_len step j=j+1 do
	if val == arr[j] then
		goto val_found_lab
	endif
done

// at this point value not found

goto lab_end

//----------------------

val_found_lab:

//at this point value found

lab_end:
e.g. (2) without goto
for j=0 while j<arr_len step j=j+1 do
	if val == arr[j] then
		break
	endif
done

if j >= arr_len then
	// at this point value not found
else
	//at this point value found
endif
NOTE: in e.g. (1) the use of the goto statements elminated the need for the logical expression j >= arr_len to be evaluated after the loop as in e.g. (2).