Homeworks academic service


The conditional and iterative structures of programming languages

The general structure of a loop is the following: The body contains the executable statements to be repeated. There are two extreme cases when executing loops. One extreme is when the body is never executed, which is called an empty loop. The other extreme is if loop never ends, which is called an infinite loop.

  • In some cases, there have been errors in the underlying tables, most famously in the table the Intel Pentium processor chip used to perform floating-point division;
  • Based on their semantics, count-controlled loops take either the test before or test after approach;
  • In other cases, it is not so easy to tell;
  • The header line of the if statement begins with the keyword if followed by a boolean expression and ends with a colon:

Programming languages distinguish between the following kinds of loops: The following subsections describe these loop types in detail. The condition may be placed in the head or in the tail. Based on their semantics, we distinguish between pre-conditional and post-conditional loops. The condition is specified in the head. First, the condition is evaluated. If it is true, the body of the loop is executed once.

Then the condition is evaluated again, and the body is executed again, until the condition turns false. There must be one or more statements in the body that contribute to changing the value of the condition. A pre-conditional loop may be an empty loop if the condition evaluates to false when the control reaches the loop; the pre-conditional loop is an infinite loop if the condition is true when the control reaches the loop, and remains true.

The post-condition generally belongs to the tail, but some languages put the post-condition in the head. In post-conditional loops the body is executed first, and then the condition is evaluated. Generally, if the condition is false the body is executed repeatedly. The iteration lasts until the condition turns true. Note that there are languages which repeat under a true condition.

Apparently it is necessary to ensure that the value of the condition is altered in the body. Post-conditional loops can never be empty loops, since the body is always executed at least once.

They can, however, turn into infinite loops if the value of the condition never changes. Loop parameters always include a special variable, the so-called index or loop variable which determines the number of repetitions. The body is executed for every value taken by the variable. The variable may take on values from the range specified in the loop head with lower bound and upper bound. The loop variable may take on all the items from the specified range including the lower bound and the upper bound valuesor just certain values at regular distances equidistant values.

In the latter case the programmer must specify the step size, which determines the distance between neighboring index values. The variable may take the domain values in ascending or descending order; the order in which the variable takes the values from the domain is determined by the direction. Languages take different sides in following issues raised by count-controlled loops: What types may loop variables have?

Every language allows the integer type. Certain languages support enumeration types. Few languages allow the real type. The type of the lower bound value, the upper bound value and the step size must be of the same type as the loop variable, or compatible with it. How are the lower bound, upper bound and the step size specified?

  • So, you can simplify the condition in the second IF statement, as follows;
  • In other cases, it is not so easy to tell;
  • As the next example shows, the sequence of statements within the loop is not executed and control passes to the next statement;
  • If there is no such value the loop terminates regular termination.

Every language allows these parameters to be specified with a literal, a variable or a named constant. More recent languages support the use of expressions.

How can direction be defined? In languages which support only numeric type loop variables the direction depends on the sign of the step size. If the step size is positive the direction is ascending; a negative step size indicates descending direction. How many times are loop parameters evaluated?

Usually once, when the control first reaches the loop, and the parameters remain unchanged while the loop is being executed. Loop parameters are evaluated each time before the body is executed. How does the loop terminate? Regular execution as defined by the loop parameters; with a specific statement in the loop body. With a GOTO statement, considered an irregular way of terminating the loop.

What is the value of the loop variable after the loop terminates? If control has been transferred as a result of a GOTO statement, the value of the loop variable will be the last value it was assigned. If the loop has terminated regularly, the value of the variable depends on what the reference language claims. Certain languages do not state anything about the issue, while other languages consider the value of the loop variable undefined.

Implementations, on the other hand, claim the followings: Based on their semantics, count-controlled loops take either the test before or test after approach. Since few reference languages specify their approach, it is implementation-dependent in most cases. Most implementations support a variant of the test before approach. The semantics of the test before approach: First the loop parameters are evaluated, after which the run time system examines whether the specified range is empty or not in the specified direction.

If the range is empty e.

Otherwise the loop variable is assigned the lower bound as initial value, and the body is executed. Then the run time system examines whether the specified range includes the next value in the specified direction at the given step size, compared to the present value of the loop variable.

If such a value is found the variable accepts it as the next value, and the body is executed. If there is no such value the loop terminates regular termination. The semantics of the test after approach: First the loop parameters are evaluated, after which the loop variable is assigned the lower bound as initial value and the body is executed. If there is no such value the loop terminates. Procedural languages usually allow the programmer to assign a new value to the loop variable in the body of the loop.

Test-before count-controlled loops may be empty loops, but test-after loops cannot. However, it is possible for both test-before and test-after loops to turn into infinite loops in certain languages.

Basic Control Structures

Enumeration-controlled loops have a loop variable whose values are defined explicitly. The body is executed for each specified value. The loop variable and its values are defined in the head; the values are specified with expressions. The loop variable may be of any type. Enumeration-controlled loops can never be empty or infinite.

  • If the condition is false or null, the IF statement does nothing;
  • The iteration structure executes a sequence of statements repeatedly as long as a condition holds true;
  • Repeated execution of a set of statements is called iteration;
  • We can use the computers to generate the tables, so there will be no errors.

Considering its semantics, an infinite loop can never be an empty loop. Infinite loops must contain a statement which helps terminate the loop. Infinite loops are particularly effective for implementing event-controlled applications. Their head may include any amount of information concerning the number of repetitions, and the semantics of such loops is accordingly intricate. Composite loops allow the programmer to design loops of arbitrary complexity.

Certain languages support statements that transfer control to another part of the program. When used in the body of loops, such statements cause the regular termination of the loop, and are the only way of regularly terminating an infinite loop.