Principles of programming languages Maarit Harsu / Matti Rintala / Henri Hansen TUT Pervasive Computing 1 Statements (control structures) • Primitive statements – assignment – subprogram call – empty statement • Compound statement • Control structures – selection statements – iterative statements • How to separate statements from each other – carriage return terminates each statement (Fortran) – a special character separates statements (Pascal) – a special character termina- tes each statement (Ada, C) Generators (Python)
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Principles of programming languagesMaarit Harsu / Matti Rintala / Henri Hansen
• Many languages have more control structures– to improve readability and writeability of programs– however, programmers tend to learn only a subset of a
large language
IF expr GOTO label
Principles of programming languagesMaarit Harsu / Matti Rintala / Henri Hansen
TUT Pervasive Computing 4
Compoundstatement
• Statement sequence surrounded by matchedbraces– begin ... end or { ... }– braces are not necessarily needed
• e.g. Modula-2 (braces integrated tocontrol structures)
• Block– variable and other declarations allowed inside a
compound statement
WHILE expr DO...
END
IF expr THEN ...ELSIF ...ELSE ...END
Modula-2:
IF A < B GOTO 20... else-branchGOTO 30
20:... then-branch
30:
Selection withoutcompound statement(Basic-code):
Principles of programming languagesMaarit Harsu / Matti Rintala / Henri Hansen
• two code blocks as parameters representing the”then” and ”else” brances
fact 0 = 1fact n = n * fact ( n – 1 )
total = 0ifTrue: [ average <- 0 ]ifFalse: [ average <- sum // total ]
Principles of programming languagesMaarit Harsu / Matti Rintala / Henri Hansen
TUT Pervasive Computing 13
Iterative statement
• Definite iteration [counter-controlled loop]– number of iterations is known before-hand
• Indefinite iteration [logically controlled loop]– number of iterations is computed during the iteration– loop completion test either before loop body or after it
loop-- statementsexit when expr;-- statements
end loop;
generalized loopcompletion test (Ada):
Principles of programming languagesMaarit Harsu / Matti Rintala / Henri Hansen
TUT Pervasive Computing 14
Definite iteration
• Includes a loop variable (counter, index)
• Semantic variations in programming languages:– What is the value of the loop variable after the loop?– Can the loop variable be changed within the loop?– Are the upper and lower bounds (expr1 and expr2) be
evaluated only once, or once for every iteration?
Passingthroughan array
for index from expr1 to expr2 do stmt end
Principles of programming languagesMaarit Harsu / Matti Rintala / Henri Hansen
TUT Pervasive Computing 15
Definite iteration in languagesfor index := init_value ( to | downto ) final_value do stmtPascal:
for index in [ reverse ] discrete_range loopstmts
end loop; i: Float := 3.14;for i in 1..10 loop
sum := sum + i;end loop;
Ada:
The scope of theloop counter in Ada:
FOR index := init_value TO final_value [ BY step_value ] DOstmts
END
Modula-2:
Principles of programming languagesMaarit Harsu / Matti Rintala / Henri Hansen
TUT Pervasive Computing 16
for-statement in Cfor ( index = 0; index <= 10; index++ )
sum += list [ index ];
• No actual loop counter– the values of variables in the expressions can be
changed in the loop• Includes three expressions
– each expression can be missing– each expression can be compound