15
Decision Structure - 2 ISYS 350

Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Embed Size (px)

Citation preview

Page 1: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Decision Structure - 2

ISYS 350

Page 2: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Complex Condition with Logical Operators

• The logical AND operator (&&) and the logical OR operator (||) allow you to connect multiple Boolean expressions to create a compound expression

• The logical NOT operator (!) reverses the truth of a Boolean expression

Operator Meaning Description

&& AND Both subexpression must be true for the compound expression to be true

|| OR One or both subexpression must be true for the compound expression to be true

! NOT It negates (reverses) the value to its opposite one.

Expression Meaning

x >y && a < b Is x greater than y AND is a less than b?

x == y || x == z Is x equal to y OR is x equal to z?

! (x > y) Is the expression x > y NOT true?

Page 3: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Logical Operators: &&, ||, !

• && • Cond1 Cond2 Cond1 && Cond2

T TT FF TF F

• ||• Cond1 Cond2 Cond1 || Cond2

T TT FF TF F

• !• Cond ! Cond

TF

Page 4: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Examples

• Write a complex condition for: 12 <= Age <= 65• Use a complex condition to describe age not

between 12 and 65.• X <= 15 is equivalent to: X<15 AND X =15? (T/F)

Page 5: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

More Complex Conditions

• University admission rules: Applicants will be admitted if meet one of the following rules:– 1. Income >= 100,000– 2. GPA > 2.5 AND SAT > 900

• An applicant’s Income is 150,000, GPA is 2.9 and SAT is 800. Admitted?– Income >= 100,000 OR GPA > 2.5 AND SAT >900

• How to evaluate this complex condition?– AND has higher priority

Page 6: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

• Scholarship: Business students with GPA at least 3.2 and major in Accounting or CIS qualified to apply:– 1. GPA >= 3.2– 2. Major in Accounting OR CIS

• Is a CIS student with GPA = 2.0 qualified?– GPA >= 3.2 AND Major = “Acct” OR Major = “CIS”

• Is this complex condition correct?– Parenthesis, ( )

Page 7: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

NOTSet 1: Young: Age < 30

Set 2: Rich: Income >= 100,000

Young Rich

Page 8: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Young: Age<30Rich: Income >100000

private void button1_Click(object sender, EventArgs e) { double Age, Income; Age = double.Parse(textBox1.Text); Income = double.Parse(textBox2.Text); if (Age < 30 && Income > 100000) MessageBox.Show("You are young and rich"); else MessageBox.Show("You are not young or not rich"); }

Page 9: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Boolean (bool) Variables and Flags

• You can store the values true or false in bool variables, which are commonly used as flags

• A flag is a variable that signals when some condition exists in the program– False – indicates the condition does not exist– True – indicates the condition exists

Boolean good;// bool good;if (mydate.Year == 2011) { good = true; }else { good = false; }MessageBox.Show(good.ToString());

Page 10: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Using Boolean Variables and && private void button1_Click(object sender, EventArgs e) { bool Young=false, Rich=false; double Age, Income; Age = double.Parse(textBox1.Text); Income = double.Parse(textBox2.Text); if (Age < 30) Young = true; if (Income > 100000) Rich = true; if (Young && Rich) MessageBox.Show("You are young and rich"); else MessageBox.Show("You are not young OR not rich"); }

Page 11: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Using Boolean Variables and ||

private void button1_Click(object sender, EventArgs e) { bool Young=false, Rich=false; double Age, Income; Age = double.Parse(textBox1.Text); Income = double.Parse(textBox2.Text); if (Age < 30) Young = true; if (Income > 100000) Rich = true; if (Young || Rich) MessageBox.Show("You are young OR rich"); else MessageBox.Show("You are not young and not rich"); }

Page 12: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

The if-else-if StatementRules to determine letter grade

• Avg>=90 A• 80<=Avg<90 B• 70<=Avg<80 C• 60<=Avg<70 D• Avg<60 F

Page 13: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Compare the two programs

if (grade < 60) { MessageBox.Show("F"); }else if (grade < 70) { MessageBox.Show("D"); }else if (grade < 80) { MessageBox.Show("C"); }else if (grade < 90) { MessageBox.Show("B"); }else { MessageBox.Show("A"); }

if (grade < 60) { MessageBox.Show("F"); }if (grade >=60 && grade < 70) { MessageBox.Show("D"); }if (grade >= 70 && grade < 80) { MessageBox.Show("C"); }if (grade >= 80 && grade < 90) { MessageBox.Show("B"); }if (grade >=90 ) { MessageBox.Show("A"); }

Page 14: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Tax Rate Schedule • Rules to determine tax rate:

– Taxable Income < =3000 no tax– 3000 < taxable income <= 10000 5% tax– 10000<Taxable income <= 50000 15% tax– Taxable income>50000 25%

double taxableIncome, taxRate, tax; taxableIncome = double.Parse(textBox1.Text); if (taxableIncome <= 3000) { taxRate = 0; } else if (taxableIncome<=10000) { taxRate= .05; } else if (taxableIncome <= 50000) { taxRate = .15; } else { taxRate = .25; } tax = taxableIncome * taxRate; textBox2.Text = tax.ToString("C");

Page 15: Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to

Data Entered in Textbox1 Must Be between 10 and 30

private void textBox1_Validating(object sender, CancelEventArgs e) { double enteredData; enteredData = double.Parse(textBox1.Text); if (enteredData<10 || enteredData>30) { MessageBox.Show("Pls enter a number between 10 and 30"); e.Cancel=true; } }