![]() |
![]() |
![]() ![]() |
Baumeister Mediasoft Engineering |
|
![]() |
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
( 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
( 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
if (expression) statement where the dependent statement of the
if (expression) statement1 else statement2 where the
if (expression) statement else if (expression) { if (expression) statement } else if (expression) statement : else statement Since the
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
switch StatementThe
switch (expression) statement Each dependent statement of the
case expression : or default : The statements between the
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
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
while (expression) statement where the dependent statement is repeated until the value of the
// 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
do-while StatementThe formal syntax of the
do statement while (expression); where the dependent statement is repeated until the value of the
for StatementThe formal syntax of the
for (expression1; expression2; expression3) statement which is equivalent to experession1; while (expression2) { statement; expression3; } where
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
forall StatementThe
forall (identifier1 of identifier2 where expression) statement where identifier1 must refer to an
forall (identifier1) statement The
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
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
break; The
continue StatementThe formal syntax of the
continue; The
Function Call and return StatementBoth the function call facilities and the
A function call is an expression which performs a jump to the first statement of the corresponding function definition. The
return; or return expression; If the
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-2023 Oliver Bartels F+E • Updated: 29 October 2008, 12:56 [UTC] |
Baumeister Mediasoft Engineering, Clontarf, Dublin 3, D03 HA22, Ireland © 2023 Manfred Baumeister |
![]() ![]() |