Section 4

How to Design Programs: An Introduction to Programming and Computing

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
How to Design Programs: An Introduction to Computing and Programming [Go to first , previous , next page; contents ; index ] Section 4 Conditional Expressions and Functions For many problems, computer programs must deal with different situations in different ways. A game program may have to determine whether an object's speed is in some range or whether it is located in some specific area of the screen. For an engine control program, a condition may describe whether or when a valve is to be opened. To deal with conditions, we need to have a way of saying a condition is true or false; we need a new class of values, which, by convention, are called BOOLEAN (or truth) values. This section introduces booleans, expressions that evaluate to Booleans, and expressions that compute values depending on the boolean result of some evaluation. 4.1 Booleans and Relations [../icons/plt.gif] Boolean Operations Consider the following problem statement: Company XYZ & Co. pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work, if the number is within the proper range . The italic words highlight the new part (compared to section 2.3 ). They imply that the program must deal with its input in one way if it is in the legitimate range, and in a different way if it is not. In short, just as people need to reason about conditions, programs must compute in a conditional manner. Conditions are nothing new. In mathematics we talk of true and false claims, which are conditions. For example, a number may be equal to, less than, or greater than some other number. If x and y are numbers, we state these three claims about x and y with 1. x = y : `` x is equal to y ''; 43:21 PM] file:///C|/Documents%20and%20Settings/Linda%20Graue. ../How%20to%20Design%20Programs/curriculum-Z-H-7.html (1 of 19) [2/5/2008 4:
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
How to Design Programs: An Introduction to Computing and Programming 2. x < y : `` x is strictly less than y ''; 3. x > y : `` x is strictly greater than y ''. For any specific pair of (real) numbers, exactly one of these claims holds. If x = 4 and y = 5, the second claim is a true statement, and the others are false. If x = 5 and y = 4, however, the third claim is true, and the others are false. In general, a claim is true for some values of the variables and false for others. In addition to determining whether an atomic claim holds in a given situation, it is sometimes important to determine whether combinations of claims hold. Consider the three claims above, which we can combine in several ways: 1. x = y and x < y and x > y 2. x = y or x < y or x > y 3. x = y or x < y . The first compound claim is false because no matter what numbers we pick for
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 19

Section 4 - How to Design Programs An Introduction to...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online