Intro to Programming Algorithm Design Decisions Assg This

  • Slides: 90
Download presentation
Intro to Programming & Algorithm Design Decisions Assg This presentation can be viewed on

Intro to Programming & Algorithm Design Decisions Assg This presentation can be viewed on line in a file named: ch 04. Introto. Prog. ppt Copyright 2003 by Janson Industries 1

Objectives n Explain u Boolean expressions u Comparison operators u If/Then/Else logic u AND

Objectives n Explain u Boolean expressions u Comparison operators u If/Then/Else logic u AND and OR logic u Nested Ifs u Case structure 2 Copyright 2014 by Janson Industries

Decision Structure n Up till now all instructions in a module executed in sequence

Decision Structure n Up till now all instructions in a module executed in sequence u n Decision structures allow programmer to define the specific condition(s) when a statement should be executed u Copyright 2014 by Janson Industries i. e. Statements executed in order from top to bottom Conditions specified as Boolean expressions 3

Boolean Expression n Results in a value of true or false n Most basic

Boolean Expression n Results in a value of true or false n Most basic consists of u u u n Constant Value or Variable Comparison Operator Constant Value or Variable Examples u u gender = "F" hours. Worked > 40 4 Copyright 2014 by Janson Industries

Boolean Expression n Values in expression can be a variables or constants u In

Boolean Expression n Values in expression can be a variables or constants u In both examples, the first value was a variable and the second was a constant F n But it doesn't have to be that way Most languages support the following comparison operations u u Equal, Not Equal Greater Than, Less Than 5 Copyright 2014 by Janson Industries

Boolean Expression n In addition you can combine equal, greater than, and less than

Boolean Expression n In addition you can combine equal, greater than, and less than to form u u n Different languages support comparison operations with different commands u u Copyright 2014 by Janson Industries Greater Than or Equal Less Than or Equal u . equals EQ equal 6

Boolean Expression n Most support the algebraic symbols u u n > - greater

Boolean Expression n Most support the algebraic symbols u u n > - greater than < - less than >= - greater than or equal to <= - less than or equal to Equal and Not Equal are a little tricky u u Copyright 2014 by Janson Industries A = 10 in many languages, assigns the value 10 to the variable A A == 10 compares variable A to the constant value 10 7

Boolean Expression n Not Equal often expressed as u n n <> - not

Boolean Expression n Not Equal often expressed as u n n <> - not equal However Java, and the various versions of C use != u Exclamation point means not Examples u state != "FL" u is. Print = !true u !(salary > 75000) 8 Copyright 2014 by Janson Industries

Boolean Expression n Negative logic can be confusing n Try not to use n

Boolean Expression n Negative logic can be confusing n Try not to use n For instance, instead of u n Use u n if (!(salary > 75000)) if (salary <= 75000) Often, in programming languages, the Boolean expression is enclosed in () 9 Copyright 2014 by Janson Industries

Boolean Expression n n However sometimes the nonnegative logic is not viable For instance,

Boolean Expression n n However sometimes the nonnegative logic is not viable For instance, the alternative to u n Would be 49 conditions, which would be much more code and therefore u u Copyright 2014 by Janson Industries state != "FL" Less efficient More likely to have errors 10

Boolean Expression n Can perform same selection with different operators If (price > limit)

Boolean Expression n Can perform same selection with different operators If (price > limit) Then final. Cost = price * luxury. Tax Else final. Cost = price * sales. Tax Endif If (price <= limit) Then final. Cost = price * sales. Tax Else final. Cost = price * luxury. Tax Endif 11 Copyright 2014 by Janson Industries

Boolean Expression n Which one is better? u The one that is more efficient

Boolean Expression n Which one is better? u The one that is more efficient F n The one that causes the fewer number of statements to be executed How determine? u Need to know business/data F Are more than 50% of the prices over or under the limit? • If over limit, check price > limit first • If under limit, check price <= limit first 12 Copyright 2014 by Janson Industries

Boolean Expression Gotcha n n Must compare values of the same type Bad expressions:

Boolean Expression Gotcha n n Must compare values of the same type Bad expressions: u total. Cost == "Joe Smith" F u gender > 10 F u Total. Cost is a numeric variable Gender is a string variable 10 == "A" F Comparing a number to a string 13 Copyright 2014 by Janson Industries

Decision Structure n n In pseudocode, use the word If followed by a Boolean

Decision Structure n n In pseudocode, use the word If followed by a Boolean condition in parenthesis Statements to be executed if true follow Then and are indented If (month == 1) Then Display “Jan” End If If (month == 2) Then Display “Feb” Copyright 2014 by Janson Industries End If 14

Boolean Expression month == 1 False True n Display “Jan” n month == 2

Boolean Expression month == 1 False True n Display “Jan” n month == 2 False True Display “Feb” Copyright 2014 by Janson Industries In flow charts, put into diamond symbol (selection) Two logic paths out of diamond u True u False 15

Java if Statement n Syntax of an if/else statement u if(boolean expression) { statements

Java if Statement n Syntax of an if/else statement u if(boolean expression) { statements to be executed if true } if (month == 1) { System. out. println(“Jan”); } if (month == 2) { System. out. println(“Feb”); } 16 Copyright 2014 by Janson Industries

Java if Statement n When comparing a String variable must use. equals String gender;

Java if Statement n When comparing a String variable must use. equals String gender; : : if (gender. equals(“f”)) { System. out. println(“Female”); } if (gender. equals(“m”)) { System. out. println(“Male”); } 17 Copyright 2014 by Janson Industries

Dual Alternative Decision n n Can specify what should happen if condition is false

Dual Alternative Decision n n Can specify what should happen if condition is false In pseudocode, use the word Else and indent statements to be executed if false If (gender == “F”) Then Display “Female” Else Display “Male” End If Copyright 2014 by Janson Industries 18

Dual Alternative Decision n Have false leg come out the left side of the

Dual Alternative Decision n Have false leg come out the left side of the diamond False Display “Male" gender == “F" True Display “Female" 19 Copyright 2014 by Janson Industries

Java if/else Statement n if(boolean expression) { statements to be executed if true }

Java if/else Statement n if(boolean expression) { statements to be executed if true } else { statements to be executed if false } String gender; : : if (gender. equals(“f”)) { System. out. println(“Female”); } else{ System. out. println(“Male”); } 20 Copyright 2014 by Janson Industries

Nested IFs n n n Having a decision structure inside a decision structure Allows

Nested IFs n n n Having a decision structure inside a decision structure Allows checking for multiple conditions For example, insurance company assigns risk rating based on the type and color of car 21 Copyright 2014 by Janson Industries

Nested IFs If (is. Car. Type == "Sports") Then If (is. Car. Color ==

Nested IFs If (is. Car. Type == "Sports") Then If (is. Car. Color == "Red") Then ins. Type = "High. Risk" Else ins. Type = "Normal" End If 22 Copyright 2014 by Janson Industries

Nested IFs False ins. Type="Normal" True is. Car. Type == "Sports" False ins. Type="Normal"

Nested IFs False ins. Type="Normal" True is. Car. Type == "Sports" False ins. Type="Normal" Copyright 2014 by Janson Industries is. Car. Color == "Red" True ins. Type="High. Risk" 23

Java Nested ifs n Placing an if statement as one of the statements to

Java Nested ifs n Placing an if statement as one of the statements to be executed in an if/else clause if (month == 1) { System. out. println(“Jan”); } Nested if else {if (month == 2) { System. out. println(“Feb”); } else { if (month == 3) { System. out. println(“Mar”); } else { if (month == 4) {……… 24 Copyright 2014 by Janson Industries

Case Structure n n A large number of nested IFs can be difficult to

Case Structure n n A large number of nested IFs can be difficult to understand/debug The Case structure is an alternative Use the keyword Select to identify the variable to be checked Then the keyword Case followed by the value to check for 25 Copyright 2014 by Janson Industries

Case Structure Select month Case 1: Display “Jan” Case 2: Display “Feb” Case 3:

Case Structure Select month Case 1: Display “Jan” Case 2: Display “Feb” Case 3: Display “Mar” Etc. etc. End Select 26 Copyright 2014 by Janson Industries

Case Structure n Can have a default case u n Identifies what to do

Case Structure n Can have a default case u n Identifies what to do if none of the checked values is true Appears after all the Case statements Select month Etc. etc. Case 12: Display “Dec” Default: Display month, “ is not a valid value” End Select Copyright 2014 by Janson Industries 27

Case Structure light. Color “Green” Display “Maintain speed" Copyright 2014 by Janson Industries “Yellow”

Case Structure light. Color “Green” Display “Maintain speed" Copyright 2014 by Janson Industries “Yellow” Display “Punch it!" “Red” Display “Stop” 28

SFC Case Structure Copyright 2014 by Janson Industries 29

SFC Case Structure Copyright 2014 by Janson Industries 29

SFC Case Structure Click on circle to right of the Case symbol, then Edit,

SFC Case Structure Click on circle to right of the Case symbol, then Edit, Insert Specify the value to search for, then click OK Copyright 2014 by Janson Industries 30

SFC Case Structure Click on True circle and insert statements to perform if true

SFC Case Structure Click on True circle and insert statements to perform if true Copyright 2014 by Janson Industries 31

SFC Case Structure Keep inserting cases until finished Of course, need to define light.

SFC Case Structure Keep inserting cases until finished Of course, need to define light. Color and assign a value Copyright 2014 by Janson Industries 32

SFC Case Structure Copyright 2014 by Janson Industries 33

SFC Case Structure Copyright 2014 by Janson Industries 33

Flowgorithm Case Structure n Doesn’t have one, must use nested IFs 34 Copyright 2014

Flowgorithm Case Structure n Doesn’t have one, must use nested IFs 34 Copyright 2014 by Janson Industries

Java Case Structure n Uses the keyword switch u n n Very similar to

Java Case Structure n Uses the keyword switch u n n Very similar to the pseudo code in SFC The variable being checked is in parenthesis Need break statements in each case because once the condition is true, all subsequent statements are executed 35 Copyright 2014 by Janson Industries

Switch n If there were no breaks: u u The label would be set

Switch n If there were no breaks: u u The label would be set to Dec The “Not a valid month” message would be displayed switch (month) { case 1: month. Label. set. Text(“Jan”); break; case 2: month. Label. set. Text(“Feb”); break; case 3: month. Label. set. Text(“Mar”); break; : : : default: System. out. println(“Not a valid month!”); } 36 Copyright 2014 by Janson Industries

Compound Condition n Use AND or OR to connect many conditions Each AND &

Compound Condition n Use AND or OR to connect many conditions Each AND & OR links two conditions AND means both conditions must be true u n Replaces nested if OR means either one or both conditions must be true u Replaces multiple ifs 37 Copyright 2014 by Janson Industries

Compound Condition n So instead of nested if: If (is. Car. Type == "Sports")

Compound Condition n So instead of nested if: If (is. Car. Type == "Sports") Then If (is. Car. Color == "Red") Then ins. Type = "High. Risk" Else ins. Type = "Normal" Endif If (is. Car. Type == "Sports" AND is. Car. Color == "Red") Then ins. Type = "High. Risk" Else ins. Type = "Normal" Endif Copyright 2014 by Janson Industries 38

Compound Condition n So instead of multiple ifs: If (state == "WA") Then sales.

Compound Condition n So instead of multiple ifs: If (state == "WA") Then sales. Tax. Rate = 0 Endif If (state == "NJ") Then sales. Tax. Rate = 0 Endif If (state == "WA" OR state == "NJ") Then sales. Tax. Rate = 0 Endif 39 Copyright 2014 by Janson Industries

Compound Condition n If you specified the following OR condition u n n flavor

Compound Condition n If you specified the following OR condition u n n flavor == "vanilla" OR flavor == "chocolate" And the ice cream man handed you a chocolate ice cream cone Would the ice cream man have satisfied the condition? YES 40 Copyright 2014 by Janson Industries

Compound Condition n If you specified the following AND condition u n n flavor

Compound Condition n If you specified the following AND condition u n n flavor = ="vanilla" AND flavor == "chocolate" And the ice cream man handed you a chocolate ice cream cone Would the ice cream man have satisfied the condition? NO 41 Copyright 2014 by Janson Industries

Truth Table n Shows u u u 2 conditions: x and y Every possible

Truth Table n Shows u u u 2 conditions: x and y Every possible combination of T and F for x and y AND and OR compound condition Boolean value for each combination Cond x Cond y x AND y x OR y True True False True False 42 Copyright 2014 by Janson Industries

Truth Table n Two conditions: u Cond x: flavor == “Vanilla” u Cond y:

Truth Table n Two conditions: u Cond x: flavor == “Vanilla” u Cond y: cone. Type == “Sugar” flavor cone. Type x AND y x OR y Vanilla Sugar True Vanilla Waffle False True Chocolate Sugar False True Chocolate Waffle False 43 Copyright 2014 by Janson Industries

Compound Condition Efficiency n n Conditions checked from left to right Short circuit evaluation

Compound Condition Efficiency n n Conditions checked from left to right Short circuit evaluation stops checking in an u AND as soon as a false condition is found u OR as soon as a true condition is found 44 Copyright 2014 by Janson Industries

Compound Condition Efficiency n If the programming language supports short circuit evaluation u Specify

Compound Condition Efficiency n If the programming language supports short circuit evaluation u Specify the conditions so that the least amount of checks are performed u The fewer the checks, the faster the program will execute 45 Copyright 2014 by Janson Industries

OR Efficiency n n Put the condition that is most likely to be true

OR Efficiency n n Put the condition that is most likely to be true first in an OR condition For instance, if 30% of sales come from NJ and only 5% from WA If (state == "NJ" OR state == " WA") Then sales. Tax. Rate = 0 Endif n Means that only 70% of the time is the 2 nd condition checked u Alternative would result in 2 nd condition being checked 95% of the time 46 Copyright 2014 by Janson Industries

AND Efficiency n n Put the condition that is most likely to be false

AND Efficiency n n Put the condition that is most likely to be false first in an AND condition For instance, if 50% of customers are female and 90% are over 21 If (gender == "F" AND age > 21) Then admission. Type = "Free" Endif n Means that only 50% of the time is the age checked u Alternative would result in gender being checked 90% of the time 47 Copyright 2014 by Janson Industries

Compound Condition Mistakes n Most of the time specifying two values for the same

Compound Condition Mistakes n Most of the time specifying two values for the same variable and connecting them with an AND u n car. Color == "blue" AND car. Color == "red" Means the condition can never be true u How can the car color be both red and blue? 48 Copyright 2014 by Janson Industries

Compound Condition n However checking a single variable for two values with < or

Compound Condition n However checking a single variable for two values with < or > and an AND means you can search for ranges u n salary < 30000 AND salary > 20000 Means any salary from 20, 001 to 29, 999 would make the expression true 49 Copyright 2014 by Janson Industries

Compound Condition n In Flowgorithm use AND and/or OR in the diamond shape 50

Compound Condition n In Flowgorithm use AND and/or OR in the diamond shape 50 Copyright 2014 by Janson Industries

Java uses && for AND, || for OR (| is upper case ) Here's

Java uses && for AND, || for OR (| is upper case ) Here's the Java example of the compound AND condition Prove by changing salary to 44000, compile, and run 51 Copyright 2014 by Janson Industries

Condition Mistakes n n Be careful with > and < If we selected students

Condition Mistakes n n Be careful with > and < If we selected students for the dean's list with the following: u n It would be incorrect because u n Students with gpa = 3. 5 not selected Selection should be u Copyright 2014 by Janson Industries gpa > 3. 5 gpa >= 3. 5 52

Compound Condition Mistakes n Also, u salary > 30000 AND salary < 20000 F

Compound Condition Mistakes n Also, u salary > 30000 AND salary < 20000 F u salary < 30000 OR salary > 20000 F n No salary value can make this true Every salary value will make this true Another mistake, not specifying a full condition u car. Color = "blue" AND = "red" F Copyright 2014 by Janson Industries Need the variable car. Color in the secondition 53

Compound Condition Mistakes n Unnecessary checks If (salary >= 100000) Then income. Tax. Rate

Compound Condition Mistakes n Unnecessary checks If (salary >= 100000) Then income. Tax. Rate =. 28 Else If (salary < 100000 AND >= 60000) Then income. Tax. Rate =. 25 End If n Copyright 2014 by Janson Industries No need to check if < 100000 a second time 54

Combining AND’s and OR’s n n The AND is evaluated first (from left to

Combining AND’s and OR’s n n The AND is evaluated first (from left to right) then the OR So if there were a car with the following characteristics u n color = "red", price = 16000, style = "sports" And the condition was: style = "sports" OR color = "red" AND price < 15000 n It would be evaluated as follows: 55 Copyright 2014 by Janson Industries

Combining AND’s and OR’s color = "red", price = 16000, style = "sports" style

Combining AND’s and OR’s color = "red", price = 16000, style = "sports" style = “sports” OR color = “red” AND price < 15000 style = “sports” OR True AND False style = “sports” OR False True 56 Copyright 2014 by Janson Industries

Combining AND’s and OR’s n n AND forms a strong bond between conditions If

Combining AND’s and OR’s n n AND forms a strong bond between conditions If there were a file full of cars and we printed only those cars that satisfied the condition: style = "sports" OR color = "red" AND price < 15000 Would we get a list of: Sports cars and cheap red cars Or: Cheap sports cars and cheap red cars Copyright 2014 by Janson Industries 57

Combining AND’s and OR’s n AND forms a strong bond between conditions style =

Combining AND’s and OR’s n AND forms a strong bond between conditions style = "sports" OR color = "red" AND price < 15000 Means you will get a list of: Sports cars and cheap red cars Not: Cheap sports cars and cheap red cars style = "sports" OR color = "red" AND price < 15000 58 Copyright 2014 by Janson Industries

Combing AND’s and OR’s n To get a list of cheap sports cars and

Combing AND’s and OR’s n To get a list of cheap sports cars and cheap red cars could specify: style = "sports" AND price < 15000 OR color = "red" AND price < 15000 n Or (if parentheses are supported) (style = "sports" OR color = "red") AND price < 15000 59 Copyright 2014 by Janson Industries

Combining ANDs and ORs n Three ice cream variables u n Person says give

Combining ANDs and ORs n Three ice cream variables u n Person says give me u n Copyright 2014 by Janson Industries Rocky Road AND Sugar Cone OR Sprinkles OR Whip Cream You hand over a u n flavor, container, topping Dish filled with Sprinkles Did you satisfy the condition? Yes 60

Combining ANDs and ORs n Person says give me u n You hand over

Combining ANDs and ORs n Person says give me u n You hand over a u n Rocky Road AND Sugar Cone OR Sprinkles OR Whip Cream Rocky Road in a Dish Did you satisfy the condition? No 61 Copyright 2014 by Janson Industries

Combining ANDs and ORs n Person says give me u n You hand over

Combining ANDs and ORs n Person says give me u n You hand over a u n Rocky Road AND Sugar Cone OR Sprinkles OR Whip Cream Rocky Road in a Dish with Whip Cream Did you satisfy the condition? Yes 62 Copyright 2014 by Janson Industries

New Condition Check Java example of the compound AND condition and inputting data 63

New Condition Check Java example of the compound AND condition and inputting data 63 Copyright 2014 by Janson Industries

Run twice and check both true and false legs 64 Copyright 2014 by Janson

Run twice and check both true and false legs 64 Copyright 2014 by Janson Industries

Conditions n n n With many conditions, what to do can get very complicated

Conditions n n n With many conditions, what to do can get very complicated Often flowchart or pseudocode hard to read Alternative is a Decision table u u u Copyright 2014 by Janson Industries Shows conditions Every possible combination of conditional values For each combination, what the action(s) should be 65

Decision Table Example All possible combinations Conditions Actions Temp > 100 F F T

Decision Table Example All possible combinations Conditions Actions Temp > 100 F F T T Nausea F F T T Headache F T F T Go to school X X X X X Go to doctor Take aspirin Take Pepto X X X Stay home X Actions to take based on symptom combinations Copyright 2014 by Janson Industries X 66

Java example of the compound AND condition and GUI When run, dialog box pops

Java example of the compound AND condition and GUI When run, dialog box pops up Enter data, press OK Problem: info read from dialog box is text. Have to convert to numeric with the parse. Int function Copyright 2014 by Janson Industries 67

Dialog box disappears and results shown in command prompt 68 Copyright 2014 by Janson

Dialog box disappears and results shown in command prompt 68 Copyright 2014 by Janson Industries

Compound Conditions Design Ex n n Long distance charge basics: u Company charges 10

Compound Conditions Design Ex n n Long distance charge basics: u Company charges 10 cents per minute for all calls outside the customer’s area code that last over 20 minutes u All other calls are 13 cents per minute Want to create a program to calculate the cost of a phone call 69 Copyright 2014 by Janson Industries

Exercise n Further info about program: u Accepts data about one phone call from

Exercise n Further info about program: u Accepts data about one phone call from user F F F u Customer area code (three chars) Customer phone number (eight chars) Called area code (three chars) Called number (eight chars) Call time in minutes (four digits) Displays: F F All the inputted data Price for the call. 70 Copyright 2014 by Janson Industries

Compound Conditions Design Ex n What's the algorithm for this program? Algorithm Answer 71

Compound Conditions Design Ex n What's the algorithm for this program? Algorithm Answer 71 Copyright 2014 by Janson Industries

Compound Conditions Design Ex n n Long distance charge basics: u Company charges 10

Compound Conditions Design Ex n n Long distance charge basics: u Company charges 10 cents per minute for all calls outside the customer’s area code that last over 20 minutes u All other calls are 13 cents per minute What variables would a program need to handle this info? u Copyright 2014 by Janson Industries I. e. What are the important pieces of information? Answer 2 72

Exercise n Create command prompt external design (XD) of this program Answer 2. 5

Exercise n Create command prompt external design (XD) of this program Answer 2. 5 n Create a GUI external design (XD) of this program Answer 2. 6 73 Copyright 2014 by Janson Industries

Exercise n What is the pseudocode so far? u What is first command in

Exercise n What is the pseudocode so far? u What is first command in pseudocode? u Where are variables defined? u How would you specify the variables? Answer 3 74 Copyright 2014 by Janson Industries

SFC Exercise n So in flow chart what is the first symbol you enter?

SFC Exercise n So in flow chart what is the first symbol you enter? u n Rectangle What is text in rectangle? Answer 4 75 Copyright 2014 by Janson Industries

Exercise n After variables defined what's first thing that has to happen u Read

Exercise n After variables defined what's first thing that has to happen u Read data in u What flow chart symbol? Answer 5 76 Copyright 2014 by Janson Industries

Exercise n n Now what do we do? u Can we calculate price? u

Exercise n n Now what do we do? u Can we calculate price? u No, have to figure out type of call What type of symbol is that? Answer 6 77 Copyright 2014 by Janson Industries

Exercise n Now we can calculate price based on true or false u What

Exercise n Now we can calculate price based on true or false u What type of symbol is that? Answer 7 78 Copyright 2014 by Janson Industries

Exercise n Now what? u Show the results u What type of symbol is

Exercise n Now what? u Show the results u What type of symbol is that? Answer 8 n Full pseudocode Answer 8. 5 79 Copyright 2014 by Janson Industries

Java Exercise n In java, always initialize variables u Strings to null u u

Java Exercise n In java, always initialize variables u Strings to null u u Numbers to zero u n String name = null; int price = 0; To make a variable value fixed (unchangeable) define it as final u final double TAX_RATE =. 065; 80 Copyright 2014 by Janson Industries

Java Exercise n n In java, must create a lot of stuff to read

Java Exercise n n In java, must create a lot of stuff to read from command line First, before the class header, you must import the following import java. io. *; import java. util. Scanner; public class Class. Header { 81 Copyright 2014 by Janson Industries

Java Exercise n Then in the main method, create the following variables and objects

Java Exercise n Then in the main method, create the following variables and objects public static void main(String[] args) { Scanner keyboard = new Scanner(System. in); int num. Read = 0; n n Use the appropriate Scanner. next function Must assign read value to a variable num. Read = keyboard. next. Int(); 82 Copyright 2014 by Janson Industries

Java Exercise n Reads (. next(), next. Int(), etc. ) usually paired with a

Java Exercise n Reads (. next(), next. Int(), etc. ) usually paired with a prompt : : : System. out. println(“Please enter qty of purchase”); qty = keyboard. next. Int(); System. out. println(“Please enter item price”); price = keyboard. next. Double(); total = price * qty System. out. println(“The transaction total is ” + total); : : : 83 Copyright 2014 by Janson Industries

Java Exercise n n Write the java program to calculate the phone call cost

Java Exercise n n Write the java program to calculate the phone call cost and implement the command line XD Answer 9 84 Copyright 2014 by Janson Industries

Java Exercise n n Modularize the java program to calculate the phone call cost

Java Exercise n n Modularize the java program to calculate the phone call cost and implement the command line XD Answer 10 85 Copyright 2014 by Janson Industries

Comparing Strings n n Some languages allow you to use < and > to

Comparing Strings n n Some languages allow you to use < and > to compare strings Since all data stored as numbers (0’s and 1’s), some letters greater than others u n A is less than B, B less than C, … Y is less than Z, Z is less than a, a is less than b, etc. Java doesn’t allow <> with strings 86 Copyright 2014 by Janson Industries

Boolean Variables n n Besides numeric and string values there are Boolean values are

Boolean Variables n n Besides numeric and string values there are Boolean values are true or false There also Boolean variables that can hold a Boolean value Can use a Boolean variable in a condition instead of a Boolean expression 87 Copyright 2014 by Janson Industries

Boolean Variables Module main Declare Integer hours. Worked Declare Real pay. Rate, salary Declare

Boolean Variables Module main Declare Integer hours. Worked Declare Real pay. Rate, salary Declare Boolean is. Overtime Display “Enter number of hours worked ” Input hours. Worked If (hours. Worked > 40) Then is. Overtime = true Else is. Overtime = false End If Display “Enter pay rate ” Input pay. Rate If (is. Overtime) Then salary = hours. Worked * (pay. Rate * 1. 5) Else salary = hours. Worked * pay. Rate End If Display “Salary is ”, salary End Module 88 Copyright 2014 by Janson Industries

Points to Remember n n Decisions/selections require Boolean expressions or variables Boolean expressions result

Points to Remember n n Decisions/selections require Boolean expressions or variables Boolean expressions result in a value of true or false Use relational operator(s) to build Boolean expression Can create compound conditions by combining Boolean expressions with AND and/or OR operators 89 Copyright 2014 by Janson Industries

Assignments n Non-Graded u n Chap 4 labs 3. 1 -3. 4 Graded u

Assignments n Non-Graded u n Chap 4 labs 3. 1 -3. 4 Graded u Chap 4 lab 3. 5 90 Copyright 2014 by Janson Industries