Programming Techniques Programming Constructs Sequence, Iteration and Branching (Selection)
Sequence A sequence construct tells the processor which statement is to be executed next. By default this is the statement following the current statement (or the first statement in the program). If we wish to "jump" to some other statement we can use a goto statement.
Disadvantages Goto statements make it difficult to "trace" a program's execution and to determine the state of variables at a given point during processing. As a result errors are often obscure and difficult to locate. For this reason some more modern (post mid 1960's) imperative languages, for example Modula-2 and Pascal, have abandoned the goto statement all together. However, there are some legitimate reasons why a goto may be desirable, for example to facilitate error handling or to terminate a deeply nested sequence of loops.
Repetition or iteration A repetition construct causes a group of one or more program statements to be invoked repeatedly until some end condition is met. Typically such constructs are used to step through arrays or linked lists. We can identify two main forms of repetition: Fixed count loops - repeat a predefine number of times. Variable count loops - repeat an unspecified number of times. To which we could add recursion (routine calls itself). Recursion is not used so much in imperative languages, although it is the principal program construct used to achieve repetition in logic and functional languages, thus we will confine ourselves in the following discussion to fixed and variable count loops.
Pretest and posttest loops Both fixed and variable count loops can be further classified according to whether they are pretest or posttest loops. In the case of a pretest loop (also referred to as an entrance-controlled loop) the end condition is tested for prior to each repetition, while in the case of a posttest loop (also referred to as an exit-controlled loop) the end condition is tested for after each repetition.
Statements A selection statement provides for selection between alternatives. We can identify three types of selection construct: If statements Case statements Pattern matching
IF STATEMENT An if statement, sometimes referred to as a conditional, can be used in two forms: If condition then action1 If condition then action1 else action2
Terminating a Loop Sometimes there is a need to terminate a loop somewhere in the middle. Many languages therefore support a break (C) or exit (Ada) statement. Ada actually supplies two types of exit statement, exit and exit when.
NESTING Nesting may refer to inserting a graphic image into a word processor. 2. With computer programming, A nested function is a function which is contained inside of another function within the source code of a program.