330 likes | 443 Views
CSCI 142 Decision Logic. Arithmetic Operators. Multiplication * Division / Remainder % Addition + Subtraction -. These have precedence!. Use parentheses to change precedence or make your intentions clear. Division and Remainder.
E N D
Arithmetic Operators Multiplication* Division / Remainder % Addition + Subtraction - These haveprecedence! Use parentheses to change precedence or make your intentions clear
Division and Remainder • If both operands to the division operator (/) are integers, the result is an integer • The fractional part is discarded 14 / 3equals 8 / 12equals • The remainder operator (%) returns the remainder after dividingthe second operand into the first 14 % 3equals 8 % 12equals
Practice! • 10 + 3 • 3 * 5 • 5 - 2 * 3 • 3 / 2 • 5 % 3 • 2 * 3 + 4 / 5 • 3 * 4 / (6 - 5 % 2) • 1 + 2 * 3 / 4 • 5 % 6 - 2 • 3 + 2 * (2 – 5) / 4 All expressions containing a relational operator will result in either a true or false answer only.
Relational Operators Equal to Greater than Greater than or equal to Less than Less than or equal to Not equal to == > >= < <= !=
Understanding Precedence Use parentheses to change precedence or make your intentions clear
Practice! • 10 + 3 < 5 * 2 • 3 * 5 == 12 • 5 - 2 * 3 > 6 == false • 7 > 3 * 4 / 2 • 1 + 2 * 3 / 4 < 5 % 6 - 2 • 5 != 10 / 2 • ‘b’ <= ‘a’ • 3 + 2 * 2 >= 10 • 7 % 3 != 4 • false == true All expressions containing a relational operator will result in either a true or false answer only.
Logical Operators Not ! And && Or || • Reverses the truth value of a condition; false becomes true and true becomes false • All conditions must be true for the compound condition to be true • Only one of the conditions needs to be true for the compound condition to be true.
Using the NOT Operator • Negates the result of any booleanexpression • !true • !(2 < 3) • !(2 >= 1 && 3 == 4) • Should be avoided when possible if (!(x > 5)) { x++;} if (x<=5) { x++;} How could you rewrite this so that the NOT operator is avoided?
Understanding Precedence You can always use parentheses to change precedence or make your intentions clear
Practice! • 3 > 2 && 6 <= 5 • !(5 <= 3) • 8 == 4 * 2 || 7 < 5 • 10 < 25 && 6 > 5 + 1 • 3 >= -6 || 1 > -1 && 9 < 8
Writing Boolean Conditions • To pass a course, a student must have an average test score of at least 75. • Write the condition using a variable test. test >= 75
Writing Boolean Conditions • To pass a course, a student must have an average test score of at least 75 and an average project score of at least 35. • Write the condition using the variables test and proj. test >= 75 && proj >= 35
Writing Boolean Conditions • Only people with insurance code 150 who are over 65 years old receive a discount. • Write the condition using the variables code and age. code == 150 && age > 65
Writing Boolean Conditions • Employees with job codes 34 and 67 will receive a raise. • Write the condition using the variable code. code == 34 || code == 67
Programming Structures • Sequence • Program instructions are processed in the order in which they appear in the program • Decision (Selection) • Allows the program to make a decision and then select the appropriate action • Loop (Iteration) • One or more instructions is repeated a specified number of times or until a condition is met
Selection Structure • Used to make a decision • Evaluates condition to determine path • Condition results in true (yes) or false (no) • If the condition is true, the program performs one set of tasks • If the condition is false, the program may or may not perform a different set of tasks
Design tools • Pseudocode - Programmers use a list of tasks that must be accomplished to help them plan a program’s logic • Flowchart - The programmer writes the steps in diagram form as a series of shapes connected by arrows
Flowchart Symbols • start/stop oval • Process/calculation • input/output • Decision or loop condition
if Statement Algorithm If condition is true then Perform task A Perform task B F T Cond A B Flowchart Pseudocode
if Statement Syntax if (condition) { instructions when the condition is true } other statements…
If it’s sunny, I will go to the park. Later, I will study. If it’s sunny then go to park study F T Sunny? Go to park Study Flowchart Pseudocode
method name Printing Strings • The ConsoleProgram class has methods for printing text. • The println method accepts a String argument println ("Whatever you are, be a good one."); information provided to the method (argument)
Example if (isSunny) { println(“Go to park”); } println(“Study”);
if…else Algorithm if condition is true then perform task A else perform task B perform task C F T Cond B A C Flowchart Pseudocode
if…else Syntax if (condition) { instructions when the condition is true } else { instructions when the condition is false } other instructions…
If it’s sunny, I’ll go to the park. If it’s not, I’ll play a game. Later, I’ll study. if sunny is true then go to park else play a game study F T Sunny? Play a game Go to park Study Flowchart Pseudocode
if…else Example if (isSunny) { println(“Go to park”); } else { println(“Play a game”); } println(“Study”);
Nesting if statements • “Nested if” • if statement inside another if statement • Useful when multiple conditions must be met before some action can occur
If it’s sunny, I’ll go to the park. If the grass is dry, I’ll have a picnic. If not, I’ll sit sadly in my car. If it’s not sunny, I’ll stay home and play a game. if sunny is true then go to park if grass dry is true then have a picnic else sit sadly in car else stay home play a game F T Sunny? Go to park Stay home Grass dry? Play a game F T Have picnic Sit sadly in car Pseudocode Flowchart
Nested If Example if (isSunny) { if (grassIsDry) { println(“Picnic!"); } else { println(“Sit in car"); }} else { println(“Play a game"); }
Nested If Example 2 A salesperson inputs their code and their sales amount. Write a selection structure that assigns a bonus based on the following logic: If the code is 1 and the salesperson sold at least $10,000, then the bonus is $500; otherwise these salespeople receive $200. If the code is 2 or 3 and the salesperson sold at least $20,000, then the bonus is $600; otherwise these salespeople receive $550. All others receive $150.
if (code == 1) { if (sales >= 10000) bonus = 500; else bonus = 200; } else if (code == 2 || code == 3) { if (sales >= 20000) bonus = 600; else bonus = 550; } else { bonus = 150; }