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

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Complex Conditions. Logical Operators: AND, OR, NOT AND Cond1Cond2Cond1 AND Cond2T TF FTF OR Cond1Cond2Cond1 OR Cond2T TF FTF NOT CondNOT Cond T F.
Work with Data and Decision Structure. Murach’s Java SE 6, C3© 2007, Mike Murach & Associates, Inc. Slide 2.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
James Tam Making Decisions In Pascal In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
Decisions (Conditional Programming) Chapter 5 (Sec. 5.1 & 5.2)
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Nested IF and Complex Condition. Nested IF Example: –Rules to determine bonus: JobCode = 1, Bonus=500 JobCode = 2, Bonus = 700 JobCode = 3, Bonus = 1000.
VB.Net Introduction - 2. Counter Example: Keep track the number of times a user clicks a button Need to declare a variable: Dim Counter As Integer Need.
Complex Conditions. Logical Operators: AND, OR, NOT AND Cond1Cond2Cond1 AND Cond2T TF FTF OR Cond1Cond2Cond1 OR Cond2T TF FTF NOT CondNOT Cond T F.
1 Selection in C. 2 If / else if statement:  The else part of an if statement can be another if statement. if (condition) … else if (condition) … else.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Making Decisions In Python
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
Copyright © 2012 Pearson Education, Inc. Chapter 4 Making Decisions.
1 Selection Structures. 2 Making Decisions Sample assignment statements to figure worker pay with possible overtime PayAmount = Hours * Rate PayAmount.
C# Introduction ISYS 512. Major Differences Between VB Project and C# Project The execution starts from the Main method which is found in the Program.cs.
VB.Net Decisions. The If … Then Statement If condition Then Statements End If If condition Then Statements Else Statements End If Condition: –Simple condition:
Work with Data and Decision Structure. Slide 2 Note: String and Date are classes.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Computer Science Selection Structures.
C# Introduction ISYS 512. Visual Studio 2013 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Chapter 5 Decisions. Outline and Objectives Relational and Logical Operators If Blocks Select Case Blocks.
C# Introduction ISYS 350. Visual Studio 2013 Demo Start page: New project/ Open project/Recent projects Starting project: File/New Project/ –C# –Windows.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
MS3304: Week 6 Conditional statements. Overview The flow of control through a script Boolean Logic Conditional & logical operators Basic decision making.
Decision Structure - 2 ISYS 350. Complex Condition with Logical Operators The logical AND operator (&&) and the logical OR operator (||) allow you to.
Chapter 3:Decision Structures.  3.1 The if Statement  3.2 The if-else Statement  3.3 The if-else-if Statement  3.4 Nested if Statements  3.5 Logical.
Expression and Decision Structure ISYS 350. Performing Calculations Basic calculations such as arithmetic calculation can be performed by math operators.
BACS 287 Programming Logic 2. BACS 287 Sequence Construct The sequence construct is the default execution mode for the CPU. The instructions are executed.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
James Tam Making Decisions In Python In this section of notes you will learn how to have your programs choose between alternative courses of action.
Decisions Action based on condition. Examples Simple condition: –If total sales exceeds $300 then applies 5% discount; otherwise, no discount. More than.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Decision Structure - 1 ISYS 350. Decision: Action based on condition Examples Simple condition: – If total sales exceeds $300 then applies 5% discount;
Introduction to Methods ISYS 350. Methods Methods can be used to break a complex program into small, manageable pieces – This approach is known as divide.
A First Book of C++ Chapter 4 Selection.
Decision Structure - 2 ISYS 350.
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Decision Structure ISYS 350.
Decision Structure - 1 ISYS 350.
Classwork/Homework Classwork – Page 90 (11 – 49 odd)
Topics The if Statement The if-else Statement Comparing Strings
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Decision Structure - 2 ISYS 350.
Decision Structure - 1 ISYS 350.
Topics The if Statement The if-else Statement Comparing Strings
Working with DateTime Data
Work with Data and Decision Structure
Decision Structure - 2 ISYS 350.
Decision Structures ISYS 350.
Decision Structure - 2 ISYS 350.
Decision Structure - 2 ISYS 350.
Flow Control Statements
Comparing Strings Strings can be compared using the == and != operators String comparisons are case sensitive Strings can be compared using >, =, and.
Decision Structure - 1 ISYS 350.
Decision Structure - 2 ISYS 350.
Chapter 3: Selection Structures: Making Decisions
Loops ISYS 350.
Decision Structure - 1 ISYS 350.
Truth tables Mrs. Palmer.
Chapter 3: Selection Structures: Making Decisions
The boolean type and boolean operators
Presentation transcript:

Decision Structure - 2 ISYS 350

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 OperatorMeaningDescription &&ANDBoth subexpression must be true for the compound expression to be true ||OROne or both subexpression must be true for the compound expression to be true !NOTIt negates (reverses) the value to its opposite one. ExpressionMeaning x >y && a < bIs x greater than y AND is a less than b? x == y || x == zIs x equal to y OR is x equal to z? ! (x > y)Is the expression x > y NOT true?

Logical Operators: &&, ||, ! && Cond1Cond2Cond1 && Cond2T TF FTF || Cond1Cond2Cond1 || Cond2T TF FTF ! Cond! Cond T F

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)

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

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, ( )

NOT Set 1: Young: Age < 30 Set 2: Rich: Income >= 100,000 YoungRich

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

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());

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 > ) Rich = true; if (Young && Rich) MessageBox.Show("You are young and rich"); else MessageBox.Show("You are not young OR not rich"); }

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 > ) Rich = true; if (Young || Rich) MessageBox.Show("You are young OR rich"); else MessageBox.Show("You are not young and not rich"); }

The if-else-if Statement Rules to determine letter grade Avg>=90 A 80<=Avg<90 B 70<=Avg<80C 60<=Avg<70D Avg<60F

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"); }

Tax Rate Schedule Rules to determine tax rate: – Taxable Income < =3000 no tax – 3000 < taxable income <= % tax – 10000<Taxable income <= % tax – Taxable income> % 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");

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 30) { MessageBox.Show("Pls enter a number between 10 and 30"); e.Cancel=true; }

Working with DateTime Get current date: – DateTime thisDate=DateTime.Today; Get current date and time: – DateTime thisDate=DateTime.Now; Calculate # of days between two dates: – myDate = DateTime.Parse(textBox1.Text); – DaysBetween = thisDate.Subtract(myDate).Days;

Compute Age Given DOB: Age =65, Old DateTime myDate, thisDate=DateTime.Today; double Age; myDate = DateTime.Parse(textBox1.Text); Age = (thisDate.Subtract(myDate).Days / 365); MessageBox.Show(Age.ToString()); If (Age<30) MessageBox.Show(“Young”); elseif (Age<65) MessageBox.Show(“Middle”); else MessageBox.Show(“Old”);

Date Comparison DateTime thisDate, currentDate = DateTime.Today; thisDate = DateTime.Parse(textBox1.Text); if (currentDate>thisDate) MessageBox.Show("after"); else MessageBox.Show("before");

Using TimeSpan to calculate the number of days between two dates TimeSpan class: TimeSpan represents a length of time. Define a TimeSpan variable: TimeSpan ts; We may use a TimeSpan class variable to represent the length between two dates: ts = laterDate-earlierDate;

Code Example DateTime earlierDate, laterDate; double daysBetween; TimeSpan ts; earlierDate = DateTime.Parse(textBox1.Text); laterDate = DateTime.Parse(textBox2.Text); ts = laterDate-earlierDate; daysBetween = ts.Days; MessageBox.Show("There are " + daysBetween.ToString() + " days between " + earlierDate.ToShortDateString() + " and " + laterDate.ToShortDateString()); Note: Pay attention to how we create the output string.