| This section describes the control flow constructions provided by
Bartels User Language. Control flow statements specify the order in which computations are processed. According to the principles of structured programming
Bartels User Language distinguishes between concatenation (sequential program element), alternation and repetition (CAR - Concatenation, Alternation, Repetition). 2.5.1	ConcatenationsStatementsA statement consists of an expression (see
chapter 2.4) followed by a semicolon
(;), as in tabulator = '\t' ;
distance = sqrt(a*a+b*b) ;
filename += extension = ".ddb" ;
++ary[i] ;
printf("Part %s ;\n",partname) ;
 The semicolon is a statement terminator. An empty statement is encountered by ;
 where the expression at the left of the semicolon is omitted. Empty statements can be used to define dependent (dummy) statements (e.g., inside loops). A statement is indicated as dependent statement, if it is context-dependent to an alternation or a repetition (see below). 
Bartels User Language allows the specification of statements without side-effect as in 27+16.3;
++11;
 Statements without side-effect are worthless since they neither change any variable value by assignment nor do they activate any function. I.e.,
User Language Compiler issues a warning message if a statement without side-effects is encountered. BlocksA block consists of a sequence of declarations (see
chapter 2.3.2) and statements and is enclosed with braces
({and}). I.e., the braces apply for grouping declarations and statements together into a compound statement or block, which then is syntactically equivalent to a single statement. Compound statements are most commonly used at the definition of functions or for grouping multiple dependent statements of an alternation or repetition. 2.5.2	AlternationsAlternations make decisions according to a special expression value in order to branch to the execution of corresponding dependent (compound) statements. if- and if-else StatementThe formal syntax of the
ifstatement is if (expression)
      statement
 where the dependent statement of the
ifstatement is only executed if the expression value is nonzero (i.e., a value different from 0 or the empty string onstringexpressions). The formal syntax of theif-elsestatement is if (expression)
      statement1
else
      statement2
 where the
ifexpression is evaluated, andstatement1is executed if the expression value is nonzero or elsestatement2is executed if the expression value is zero. Dependent statements of anifand/orif-elsestatement can beiforif-elsestatements again, i.e.,ifandif-elsestatements can nest as in if (expression)
      statement
else if (expression) {
      if (expression)
              statement
      }
else if (expression)
      statement
:
else
      statement
 Since the
elsepart of anif-elsestatement is optional, there is an ambiguity when anelseis omitted from a nestedifsequence. This is resolved in a way that theelseis associated with the closest previouselse-lessif. Theifand/orif-elsestatement can be utilized as in string classname="SCM ";
if (bae_planddbclass()==800)
      classname+="Sheet";
else if (bae_planddbclass()==801 || bae_planddbclass()==803)
      classname+="Symbol/Label";
else if (bae_planddbclass()==802)
      classname+="Marker";
else {
      classname="***INVALID***";
      printf("No valid element loaded!\n");
      }
 where the class of the currently loaded SCM element is determined and the value of the variable
classnameis set accordingly. switch StatementThe
switchstatement is a special multi-way decision maker that tests whether an expression matches one of a number of other expressions, and branches accordingly. The formal syntax of theswitchstatement is switch (expression)
      statement
 Each dependent statement of the
switchstatement can be preceded with an arbitrary number ofcaselabels of the form case expression :
 or default :
 The statements between the
caselabels are strictly dependent to the closest previouscaselabel. The dependent statements of acaselabel are only executed if the value of thecaseexpression matches the value of theswitchexpression. Thedefaultlabel specifies an arbitrary value, i.e., the statements following to thedefaultlabel is always executed.caselabels do not have any effect on the sequence of computing (the execution continues as if there is nocaselabel). Thebreakstatement (see also
chapter 2.5.4) can be used in acasesegment to leave theswitchcontrol structure. Theswitchstatement can be utilized as in string classname="SCM ";
switch (bae_planddbclass()) {
      case 800 :
      classname+="Sheet";
      break;
      case 801 :
      case 803 :
      classname+="Symbol/Label";
      break;
      case 802 :
      classname+="Marker";
      break;
      default :
      classname="***INVALID***";
      printf("No valid element loaded!\n");
      }
 where the class of the currently loaded SCM element is determined and the value of the variable
classnameis set accordingly. 2.5.3	RepetitionsRepetitions are control structures forming a loop for the repetitive computing of certain parts of a program. Each repetitive statement provides a method for testing a certain condition in order to end the processing of the loop. If a program runs into a loop, where the loop-end condition is never reached, then the control flow cannot be passed back to the caller of the program (i.e., the program runs "forever"). This is a fatal programming error, which the
User Language Compiler can recognize under certain conditions. while StatementThe formal syntax of the
whilestatement is while (expression)
      statement
 where the dependent statement is repeated until the value of the
whileexpression is zero (0 or empty string forstringexpressions). Thewhilestatement can be utilized as in // ASCII file view
main()
{
      string fname;           // File name
      int fh;                 // File handle
      string curstr="";       // Current input string
      // Set the file error handle mode
      fseterrmode(0);
      // Print the program banner
      printf("ASCII FILE VIEWER STARTED\n");
      // Repeatedly ask for the input file name
      while (fname=askstr("File Name (press RETURN to exit) : ",40)) {
              // Open the input file
              printf("\n");
              if ((fh=fopen(fname,0))==(-1)) {
                      printf("File open failure!\n");
                      continue;
                      }
              // Get the current input string
              while (fgets(curstr,128,fh)==0)
                      // Print the current input string
                      puts(curstr);
              // Test on read errors; close the file
              if (!feof(fh) || fclose(fh))
                      // Read or close error
                      break;
              }
}
 where the contents of user-selectable files are listed to the terminal. The
continuestatement (see also
chapter 2.5.4) causes the next iteration of thewhileloop to begin immediately. Thebreakstatement (see also
chapter 2.5.4) provides an immediate exit from thewhileloop. do-while StatementThe formal syntax of the
do-whilestatement is do
      statement
while (expression);
 where the dependent statement is repeated until the value of the
do-whileexpression is zero (0 or empty string forstringexpressions). The dependent statement always is executed at least once (contrary towhile). for StatementThe formal syntax of the
forstatement is for (expression1; expression2; expression3)
      statement
 which is equivalent to experession1;
while (expression2) {
      statement;
      expression3;
      }
 where
expression1is evaluated, and then the dependent statement is executed andexpression3is evaluated untilexpression2is zero. I.e.,expression1typically is used for initialization,expression2applies the loop-end test andexpression3performs something like an increment. Any of the three expressions can be omitted, although the semicolons must remain. Theforstatement can be utilized as in void strwords(string s)
{
      string strlist[];
      int strcount,i,j;
      char c;
      for ( strcount=0,j=0,i=0 ; c=s[i++] ; ) {
              if (c==' ' || c=='\t') {
                      if (j>0) {
                              strcount++;
                              j=0;
                              }
                      continue;
                      }
              strlist[strcount][j++]=c;
              }
      for ( i=strcount ; i>=0 ; i-- )
              printf("%s\n",strlist[i]);
}
 where the function
strwordsseparates the givenstringparameter into words to be stored to a list and printed in reverse order afterwards. forall StatementThe
forallstatement applies for automatic sequential processing of the currently available elements of anindexdata type. The formal syntax of theforallstatement is forall (identifier1 of identifier2 where expression)
      statement
 where identifier1 must refer to an
indexvariable type specifying the type of theforallindex to be processed; theforallstatement automatically initializes and "increments" this variable. Theforallloop is terminated after the last element of theindexlist has been processed. Theofstatement of theforallstatement restricts access to those elements of a currently validindexelement of the next higher hierarchy level; i.e., theofindex must refer to anindextype which allows for the processing of theforallindextype. Thewhereexpression determines whether the dependent statement should be processed for the currentforallindex (ifwhereexpression nonzero) or not (ifwhereexpression zero). Both theofstatement and thewherestatement are optional; thus the shortest possible form of aforallstatement is forall (identifier1)
      statement
 The
forallstatement can be utilized as in index L_CPART part;
index L_CPIN pin;
forall (part where part.USED) {
      forall (pin of part where pin.NET.NAME=="vcc")
              printf("Part %s, Pin %s ;\n",part.NAME,pin.NAME);
 where the list of part pins connected to the net
vccis printed to the terminal. See
appendix B of this manual for a description of theindexvariable types. 2.5.4	Program Flow ControlBesides the previously described control flow statements
Bartels User Language provides some additional structures for controlling the program flow. break StatementThe formal syntax of the
breakstatement is break;
 The
breakstatement must be dependent to a repetitive statement
(while,do-while,fororforall) or to aswitchstatement (otherwise the Compiler issues an error message). Thebreakstatement provides an early exit from a repetition, just as fromswitch; i.e.,breakstatements cause the innermost enclosing loop (orswitch) to be exited immediately. continue StatementThe formal syntax of the
continuestatement is continue;
 The
continuestatement must be dependent to a repetitive statement
(while,do-while,fororforall), or otherwise the Compiler issues an error message. Thecontinuestatement causes the innermost enclosing loop to restart immediately at the beginning of the loop. I.e., inwhileanddo-whileloops,continuecauses the end-condition to be tested immediately; inforloops,continuepasses control to the execution of the "increment" statement. Function Call and return StatementBoth the function call facilities and the
returnstatement have been introduced already. These features are so important for controlling the program flow that they are worth a brief separate discussion at this place. A function call is an expression which performs a jump to the first statement of the corresponding function definition. The
returnstatement can only be used in functions; it terminates the execution of the currently active function and jumps back to the instruction immediately following to the previous function call (i.e., it passes the control flow back to the caller of the function). The general formal syntax of thereturnstatement is return;
 or return expression;
 If the
returnstatement does not contain any expression, then the return value of the corresponding function is undefined. Otherwise the function return value is set to thereturnexpression, which must be compatible to the function data type (otherwise, the Compiler issues an error message). If the end of a function block is encountered, and the last statement has not been areturnstatement, then the Compiler automatically produces code corresponding to a validreturnstatement with a function-compatible default (zero) return value. The generation of a default return value is only possible for basic function data types I.e., a function definition as in struct structname functionname() { }
 causes a Compiler error message since the
Bartels User Language Interpreter would encounter a memory protection fault when trying to access any of the elements of this return value. Control Structures© 1985-2025 Oliver Bartels F+E • Updated: 02 October 2007, 11:22 [UTC]
 |