Lecture 2 - Conditions and Structures

34
Basics of Program Design Conditions and Structures Rahul Desai

description

Basics of Program Design - Lecture 2

Transcript of Lecture 2 - Conditions and Structures

Basics of Program Design

Basics of Program DesignConditions and StructuresRahul DesaiBooleansWhat are Booleans?Booleans are variables that can only hold one of two valuesThe most common values a Boolean holds areTrueFalseConditionsWhat is a condition?

A condition is a statement that is executed if Booleans associated with the statement evaluate to either true or falseTypes of ConditionsIn programming, conditions are of a mathematical varietyThere are 5 types of mathematical conditions usedThey are< - Less Than> - Greater Than= - Equal To= Greater Than OR Equal ToEvaluate the following conditions (and (> 4 3) ( 4 3) (= 10 100))True(not(=23))True

Evaluate the following conditions for x = 427/2(> x 3)For x = 4, TrueFor x = 2, FalseFor x = 7/2, True(and (> 4 x) (> x 3))For x = 4, FalseFor x = 2, FalseFor x = 7/2, True(= (* x x) x)For x = 4, FalseFor x = 2, FalseFor x = 7/2, False

Translate the following five intervals on the real line into Racket functions that accept a number and return true if the number is in the interval and false if it is outside:An interval boundary marked with ( or ) is excluded from the interval; an interval boundary marked with [ or ] is included.the interval (3,7]:the interval [3,7]:the interval [3,9):the union of (1,3) and (9,11):and the range of numbers outside of [1,3].

ConditionalsWhat is a conditional expression or conditional?A conditional is an expression that determines which condition out of several is true for any given inputThe general shape of a conditional expression is

(cond [condition return-value] ... [condition return-value])

OR

(cond [condition return-value] ... [else return-value])

Designing a ConditionalInspect the problem statement for distinct situations and enumerate all possible situationsChoose at least one example per situation, for intervals or enumerations, the examples must include borderline casesWrite down the skeleton of a cond expression, with one clause per situation. Formulate one condition per situation, using the parameters. Ensure that the conditions distinguish the examples appropriatelyDeal with each cond-line separately. Assume the condition holds and develop a Racket expression that computes the appropriate answer for this case

Suppose the bank pays 4% for deposits of up to $1,000 (inclusive), 4.5% for deposits of up to $5,000 (inclusive), and 5% for deposits of more than $5,000. Create a Racket function that takes in the deposit amount and returns the interest rate for the given deposit

The 3 conditions are If the deposit is up to $1000, the interest rate is 4%If the deposit is up to $5000, the interest rate is 4.5%If the deposit is more than $5000, the interest rate is 5%

Example:

For $900, the interest rate is 4For $4500, the interest rate is 4.5For $6000, the interest rate is 5

Expression:(cond [(