[CSUSB] >> [CompSci] >> /pool/faculty/dick/cs320/sebesta/08

CS320 Notes on Control Structures

	http://www.csci.csusb.edu/dick/cs320/sebesta/08.html
   See also
	http://www.csci.csusb.edu/cgi-bin/dick/lookup320?loop
	http://www.csci.csusb.edu/cgi-bin/dick/lookup320?selection


8.1 Introduction

	control statements
	control structures

8.2 Compound statements

   One semantics: Execute each in turn.
   Many Syntaxes:
	    ALGOL 60: "begin" statement #(";" statement) "end" comment.
            ALGOL 68: "begin"..."end"    OR  "(" ... ")"
	    Pascal:   "begin" statement #(";" statement) "end"
            COBOL - out-of-line and named.
                Any paragraph
                Any set of adjacent paragraphs
                Any SECTION
                Any set of adjacent SECTIONS.

	   CPL used the printer's Section sign to start a block and
		a crossed out one to end it.  Writable, readable,
		but UNUSABLE!  So BCPL (Basic CPL) introduced {...}

            BCPL/C/C++/Java: "{" #statement "}"
	    LISP:	"(" "PROGN" #expression ")"
	    SmallTalk:	"[" expression "." expression "." ... "]"
	    Prolog:	"(" predicate "," predicate "," ....  ")"
	    Shell - two forms
		"{" command #(";" command) "}"  -- syntactic grouping
		"(" command #(";" command) ")"  -- call subshell

	      In the UNIX Shell a sequence of statements  can be separated
		by either a semicolon (as shown above) OR by an 
		end_of_line.
		shell_sequence::= command #( (";"|EOLN) command).

Blocks

            block = `compound statement with declarations`
		Sometimes the declaration must come first,
		Sometimes they can appear anywhere.
		ONE declaration makes a block!

	C:	block::="{" #declaration #statement "}".
	C++:    block::="{" #(declaration | statement) "}".
	Ada:	block::="declare" declarations "begin" statements "end"...
	LISP:	block::="(" "LET" "(" "("var val")" "("var val")"...")"
					#expression
			")".
		Notice: no types in LISP! Variables are given values that
						indicate their type.

	Pascal:  Declarations are part of subprograms, NOT compound statements.

	Java: Has the same syntax as C++, but a different semantics!
	      A variable in a block must be unique to the whole function.
		The following is ok C++ but bad Java:
		void example(int a){
			if(a>0) { int r= a; return r;}
			else { int r= -a; return r; }
		}//end example

8.3 selections


	 Two Way Selections
            research on best syntax
		    Best is not in ANY high level language

		Humans make fewer mistakes if the end of an if
		explitly indicates its begining.

            The book does not mention the COBOL conditional sentence. See
	    http://www.csci.csusb.edu/dick/samples/cobol.syntax.html#If_Verb

	C/C++/Java/Javascript:
		"if" "(" expression ")" statement "else" statement
		A statement can be compound.

	Shell:  "if" command ";" "then" shell_sequence";" 
		"else" shell_sequence";" 
		"fi"
		Based on Algol 68 if-then-else-fi

	LISP:	"(" "IF"  expression expression expression ")"
	SmallTalk:
		expression "ifTrue:" block "ifFalse:" block
		Etc.
	Prolog:	Predicate -> Predicate | Predicate
			(Roughly speaking)


	MULTIWAY Selections
            Algol 60 switch

            C/C++/Java trap  - don't forget the "break"
		"switch" "(" expression ")" switch-block, ...

	LISP:	"(" "COND" pair pair ... pair ")"
		where
			pair::= "(" condition expression ")"

		Our XLISP has a (CASE ....) function as well.

	Algol 68, Shell: case ... esac 
	UNIX Shell:
		"case" expression "in"
			#( patterns ")" commands ";;" )
		"esac"

8.4 Iterations


            Design issues

            FORTRAN DO - non-orthogonal

            post-test vs pre-test

	Make sure you can simulate pre-tests using post-tests and vice versa.
	For example
		`do A while (B)` in C behaves like  `A; while(B)A`
	and
		`while(A)B` is like `if(B) do A while(B)`
	Similarly in Pascal, Ada,.....

            n+0.5 loops -  
		Pascal
			A; 
			while C do begin B; A; 
			end;

			repeat A; 
				if not C then begin 
					B; 
				end;
			until C;
		Ada
			loop
				A;
				exit when C;
				B;
			end loop;
		C/C++/Java/Javascript
			while(1){
				A
				if(C)break;
				B
			}
		C  (when A is an expression...)
			while( A, C){B}

	 Shell: "while" command ";" "do" shell_sequence ";" "done"
		"for" variable "in" list ";" "do" shell_sequence ";" "done"
		...`

	C/C++/Java: Three forms of loop - while, for, do-while.
		break + continue.
		BUG broke NY NY telephone system
		programmer forgot that break is also used in a switch.

	Notice history of 'for' in
		Algol 60--------->Pascal---->Ada
		       \          /
			--->Algol68------->C
	At each step it was simplified.

	For loops in C++.
		TWO versions: pre1995 and the 1997 Standard C++.
		The book describes the pre-1995 C++ on page 310.
		These rules applied to Java 1.1 and perhaps to 1.2 as well.

		ANSI 1997 C++ they do not apply.
		Our Gnu CC compilers allow 1997 version but give you a
			warning about it not fitting older versions.
		

8.4.4 Iterators

	Common LISP, PERL, UNIX Shells, ...
	C technique
	C++ Standard Library has iterators and functions that use them.

8.5 Unconditional Branches


		Dangerous in the hands of amateurs

	C/C++:	"goto" label ";"
		label ":" statement
		label::=identifier.

	Ada:	label::= "<<" identifier ">>".

	Edison: NONE

	Pascal: label::=number.

	SmallTalk: NONE

	Prolog:NONE

	LISP: Inside a PROG only.

	Java: None

	Missing APL jump
		(expression)->
			meaning jump to the line number equal to 
			the value of the expression!
		(12)->
			goto line 12
		(0)->
			goto next line
		(17><(i=1))->
			goto line 17 if i=1


8.6 Guarded Commands


		"if" guard "->" commands 
		#( "[]" guard "->" commands)
		"fi"

		"do" guard "->" commands
		#( "[]" guard "-" commands)
		"od"

Note:  Prolog has UNGUARDED commands. Given a set of alternatives:
		( a  ; b ; c)
each is tried in turn: a, then b, and then c.  First it tries a and if this
	solves the problem that is OK.  But if something fails (inside or 
	after a) then
Prolog tries again with b.  Again if b fails for some reason then
Prolog forgets what it did in b and tries out c instead.

8.7 Conclusions


	E Pluribus Confusion:-)

	Evidence that we don't know what we are doing yet!