unit4: selection%statements%codeeahs.weebly.com/uploads/2/9/0/1/29018917/__cp1... · 1 unit4:...
TRANSCRIPT
1
Unit 4: Selection Statements
Computer Programming I
Mr. Kindt
Spring 2014
George Boole
• Born 1815, London to low-‐class parents
• Elementary school teacher from ages of 16-‐33.
• Self-‐taught mathematician • Published ‘The
Mathematical Analysis of Logic’ in 1848
• Became Professor of Mathematics the next year!
• Considered the forefather of formal logic
• The “Boolean Expression” is a data type named after him that led to extraordinary things.
2
Relational Operators • Familiar arithmetical operators, in Pascal. • These will always produce a value of true or false. • Any statement with these operators is called a Boolean
expression.
Examples of Boolean Expressions
3
Logical Operators: Connectives
• The “Connectives”: AND, OR • You may put these reserved words between any two relational
expressions to return a true or false value. • If AND is used, both expressions must be true to return a true.
Otherwise, it will return false. • If OR is used, at least one expression must be true to return a
true. If both are false, it will return false.
€
(8 = 8) AND (7 < 9)TRUE
€
(8 = 8) OR (7 < 9)TRUE
€
(8 <> 8) AND (7 < 9)FALSE
€
(8 <> 8) OR (7 < 9)TRUE
€
(8 <> 8) AND (7 >= 9)FALSE
€
(8 <> 8) OR (7 >= 9)FALSE
BOTH TRUE ONE TRUE, ONE FALSE BOTH FALSE
Logical Operator: Negation
• The “Negation”: NOT
• This reserved word, placed left of a relational
expression, returns the opposite true/false value.
€
NOT (14 > 22)NOT (FALSE)
TRUE
€
NOT (14 <= 22)NOT (TRUE)FALSE
NEGATING TRUE NEGATING FALSE A COMPLEX ONE!
€
NOT (6 < (12 + 3) AND 12 <> (36 / 3)[ ]NOT (6 <15 AND 12 <>12[ ]
NOT FALSE[ ]TRUE
4
Order of Operations
• AND will
precede OR
€
NOT (3 < 5) OR (21 <>18) AND (−81 > 0)[ ]
NOT (TRUE) OR (TRUE) AND (FALSE)[ ]
NOT (TRUE) OR (FALSE)[ ]
NOT [TRUE]
FALSE
ORDER OF OPERATIONS
( )NOT* / MOD DIV AND+ − OR< <= > >= = <>
A New Problem We use Boolean expressions in programming to
evaluate user input for certain conditions.
Example: You are an accountant calculating how much tax your clients owe Uncle Sam. Your program will ask them what their income is, but their tax % is dependent upon how much they earn!
5
The IF…THEN Statement You can evaluate the user input for
conditions with the following:
IF (Boolean expression)
THEN
BEGIN
executable statement;
END;
writeln(‘Please enter your income.’); readln(income); IF (income < 8375) THEN BEGIN tax := 0.10*income; END;
writeln(‘Your tax is $’, tax:1:2);
Please enter your income. 5000 Your tax is $500.00
Proper Habits of If-‐Then
• When writing an if-‐then statement, follow these coding standards and tips.
IF (Boolean argument) THEN BEGIN
executable statement; executable statement; END;
1. Place the Boolean argument in parentheses. 2. Following THEN, use a BEGIN before your executable statements (commands) and an END when finished. This is known as “blocking”. 3. Capitalize IF, THEN, BEGIN, and END. 4. Indent BEGIN and END one tab, and all executable statements contained within them two tabs. 5. The semicolons within the “block” (between BEGIN and END) refer to each executable statement. The semicolon following the END terminates the IF-THEN command.
6
AN EXAMPLE OF A PROPERLY WRITTEN IF-‐THEN STATEMENT IF (average > 92) THEN BEGIN lettergrade:=‘A’; writeln(‘Congrats!’); writeln(‘You got an ‘, lettergrade); END;
AN EXAMPLE OF A POORLY WRITTEN IF-‐THEN STATEMENT
If average > 92 then; lettergrade:=‘A’; writeln(‘Congrats!’); writeln(‘You got an ‘, lettergrade); End;
IF…THEN with Connectives
You can use complex Boolean
expressions with AND, OR,
or NOT where needed.
IF (income >= 8375) AND (income < 34000) THEN ���
BEGIN tax := 837.50 + 0.15*(income – 8375); END;
writeln(‘Your tax is $’, tax:1:2);