Chapter 5- Problem-Solving with Decisions


Decision Logic Structure

It is used by a programmer to decide whether the set of instructions is to be executed based on the condition whether it is TRUE or FALSE.

Algorithm of Decision Logic

The decision logic structure uses the If/Then/Else instruction. It tells the computer that If a condition is true, Then execute a set of instructions, or Else execute another set of instructions. The Else part is optional, as there is not always a set of instructions if the conditions are false. When there are no instructions for true, a Continue statement must be used.

Flowchart Diagram of Decision Logic Structure


There are to TWO (2) types of condition, as follow:

  1. Single condition: It has only two possible actions or set of instructions
  2. Multiple conditions

Multiple Conditions

It has THREE (3) kinds of multiple conditions such as straight-through logic, positive logic, and negative logic.

Straight-through logic means that all of the decisions are processed sequentially, one after the other.straight-through logic is required when all the decisions have to be processed, and when they are independent of each other.

Problem :   to find the amount to charge people of varying ages for a concert ticket. When the person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10.  

Positive logic allows the flow of the processing to continue through the module instead of processing succeeding decisions. In general, when you use positive logic, you are telling the computer to follow a set of instructions and continue processing if the condition is True; If the condition is not True, then the computer processes another decision. When you use this logic, no more decisions are processed once the resultant of a condition is True.

Problem: To find the amount to charge people of varying ages for a concert ticket. When the person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10.  The conditions of that problem are the following:

Negative logic is similar to positive logic except that the flow of the processing continues through the module when the resultant of decision is FALSE. In general, when you use negative logic you are telling the computer to process another decision when the resultant of the condition is True; If the resultant is False, then the computer processes a consequent set of instructions and continues processing the module.  he hardest to use and understand

Problem: To find the amount to charge people of varying ages for a concert ticket. When the person is under 16, the charge is $7; when the person is 65 or over, the charge is $5; all others are charged $10.

Exercise

Develop the flowchart in Positive Logic and Negative Logic to determine whether a temperature is below or above the freezing point. The algorithm as follow:

  1. Start
  2. Read temperature
  3. If the temperature is less than 32. then print “below freezing point”, otherwise print “above freezing point”
  4. End

References:

  • Sprankle, Maureen (2001), Problem Solving and Programming Concepts, 5th edition, Prentice-Hall.
  • Jeri R. Hanly, Elliot B. Koffman (2003), Problem Solving and Program Design in C, 4th edition Addison Wesley
  • H.M. Deital, P.J. Deital (2001). C: How to Program. 3rd edition, Prentice-Hall.
  • Alice Ficher, Stephen M. Rass(2001), Applied C (An Introduction and Move) Mc Graw Hill, New Haven