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

CS320 Notes on Control Structures

   See also

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 
		shell_sequence::= command #( (";"|EOLN) command).


            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")"...")"
		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

		"if" "(" expression ")" statement "else" statement
		A statement can be compound.

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

	LISP:	"(" "IF"  expression expression expression ")"
		expression "ifTrue:" block "ifFalse:" block
	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 ")"
			pair::= "(" condition expression ")"

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

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

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`
		`while(A)B` is like `if(B) do A while(B)`
	Similarly in Pascal, Ada,.....

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

			repeat A; 
				if not C then begin 
			until C;
				exit when C;
			end loop;
		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
		       \          /
	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

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

	Edison: NONE

	Pascal: label::=number.

	SmallTalk: NONE


	LISP: Inside a PROG only.

	Java: None

	Missing APL jump
			meaning jump to the line number equal to 
			the value of the expression!
			goto line 12
			goto next line
			goto line 17 if i=1

8.6 Guarded Commands

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

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

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!