Statements should describe actions to take (this defines imperative programming)From this note the following points
The text should describe the structure of the program (thought the use of begin, end, { }. etc.)
The language should allow the underlieing assignment-oriented CPU to be used effeciently.
Almost all stetments in C or Pascal either describe actions to be taken or control the flow of execution.Invarients
Structured programming language are inappropriate for stack oriented machines.
These are statements that are ALWAYS true. For example, "At this part of the loop the highest numbered element is at the topof the list". Invarients come in two types. Some are assertions enforced by the code (which can help catch bugs but can be hard to write). Others are statements of fact written in comments, which can elp programmers understand code.Conditionals and LoopsSome invarients check that a precondition is correct, others that a postcondition has been satisfied, and the last the some internal process is working correctly.
These effect the flow of control. Traditional structures includeThe Case Statement
case ( a ) ofThe Dangling Else
begin
3: xxxx
4: yyyy
6: xxxxx
default: aaaaa
end
Questions: What happens if the cases overlap? Should a default be allowed? Do you need a break after each statement?Implementing case statements: There are two general ways, First, you can translate the case statement into a series of if's, which works best if there are a small number of choices. Second, you can make a jump table, which works best if the range of choices (max_choice - min_choice) is small.
What does this code do?Semicolons
a = 10;
if (a == 5) if (a == 10) cout << "hi"; else cout << "bye";It's not clear which if the else modifies. Some languages make a 'fi' or 'end' part of the if syntax, then it's obvious. Others declare the rule that each else modifies the closest possible if.
Do semicolons seperate statements of end statements? The difference is the last statement of a block. Should it get a semicolon? Finally, should you be able to add extra semicolons?Loops
There are two general loops
while ( a>1 ) { a--;}
repeat { a--;} while (a > 1);
Loops can be modified by adding either a 'continue' or a 'break'.
repeat { } until ( );for i = 1 to x { }
// what if x changes, or i is reassigned??for(i = 1; i < 10; i++) { }