Thursday, July 9, 2009

Lectures in PGS Programming

Computer Programming
Loops and Iterations

Fundamentals of Looping
• Iteration – The repetition of the loop.

When the given problem says that we are to print the numbers 1 to 10, this would mean that we will need a repetition of instruction PRINT 10 times. The number or repetitions the loop has taken to perform to satisfy the problem is the number of iterations.

• Accumulator – a temporary storage location in a central processing unit that holds values during a process.

• Counter – a temporary storage location that counts the number of iteration/process in a program.

Types of Loops
• Infinite Loop – A loop that runs without end. It is due to mistakes in the logical operation or expression or variables used to control the termination of the loop.Others call this logical error. Often this type of error is hardest type to debug.

• Index Loop – A loop that executes when the number of iteration is explicit in the problem. The printing of the 1 to 10 is an example of this type of loop.

• Conditional Loop – A loop that executes only in a given satisfied condition. The number of iterations in this type of loop is not in the problem and the iteration is dependent on the conditions given. Decision Symbol is used as a loop terminator in the Conditional loop.

o Pre-condition Approach
It is a conditional loop that checks the condition first before the instruction is executed. A classic looping constructs under this type of approach is the While Loop. This loop checks if the condition given in the decision symbol is true. If it is true, the program performs the instruction. The rule thumb for this approach in programming is:

“While the condition is true, Do *statement/s”
*Statements may be a single instruction (e.g., c = c+1 ) or series of instructions (e.g., j =j+1;, followed by x =x*j, and print (x)).

o Post-condition Approach
It is a conditional loop that executes the instructions first before checking the condition. A classic looping construct this type of approach is the Do While Loop. The Do While Loop performs the instruction before checking the condition given in the decision symbol. In addition, do while loop performs the instruction at least once if the condition set in the decision symbol is not true. The loop continues while the condition is true. The rule of thumb in programming under this approach is :

“Always executes its loop at least once.”
“Do statement and iterates while the condition is true”

Exercises:
1.Accept a number and print the product of the integers. Use pre-condition approach.
2.Accept a number and print the integers and the sum of the integers of the number. Use pre-condition approach.
3.Use post-condition approach to the problem in number 1.
4.Use post-condition approach to the problem in number 2.

4 comments:

  1. Accept a number and print the product of the integers. Use Pre-Condition Approach

    please give me the answer . please

    THANK YOU GODBLESS !

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete