How to Design Programs: An Introduction to Computing and Programming [Go to first, previous, next page; contents; index]Section 4Conditional Expressions and FunctionsFor 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 xand yare numbers, we state these three claims about xand ywith 1. x= y: ``xis 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:
has intentionally blurred sections.
Sign up to view the full version.