Selection Statements Make a decision based on conditions Allows the computer to be intelligent.

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

If Statements, Try Catch and Validation. The main statement used in C# for making decisions depending on different conditions is called the If statement.
The conditional statement
What are the x- and y-components of the vector
What IF?.
Lesson 5 - Decision Structure By: Dan Lunney
DECISION MAKING STRUCTURES Computer Programming 2.
More on Decision Trees. Numerical attributes Tests in nodes can be of the form x j > constant Divides the space into rectangles.
Basic Building Blocks of Programming. Variables and Assignment Think of a variable as an empty container Assignment symbol (=) means putting a value into.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Programming with MATLAB. Relational Operators The arithmetic operators has precedence over relational operators.
Course 2: Inequalities Objectives:
Algorithms In general algorithms is a name given to a defined set of steps used to complete a task. For example to make a cup of tea you would fill the.
Python – Making Decisions Lecture 02. Control Structures A program that only has one flow is useful but limited. We can use if statements to make these.
computer
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
Chapter 21 Variable Costing
Notes Over 6.3 Writing Compound Inequalities Write an inequality that represents the statement and graph the inequality. l l l l l l l
Conditions in Java. First…Boolean Operators A boolean data type is always true or false. Boolean operators always return true or false For example: (x.
In the Real World You must be at least 42 inches to ride the bumper cars.
3.1 Inequalities and their graphs
ABSOLUTE VALUE INEQUALITIES.  Just like absolute value equations, inequalities will have two solutions: |3x - 2| ≤ 7 3x – 2 ≤ x ≤ 9 x ≤ 3 -5/3.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Basic Conditions. Challenge: ● Ask the user his/her name ● If it’s “Wally,” jeer him ● Pause video and try on your own.
ITK 168 Decisions Dr. Doug Twitchell September 20, 2005.
3.4 Solving Equations with Variables on Both Sides Objective: Solve equations that have variables on both sides.
Inequality Signs < means “is less than”  means “is less than or equal to” > means “is greater than”  means ”is greater than or equal to” Reading Inequalities.
Intro to Inequalities Unit 4 Section 4.1. Definition A statement that a mathematical expression is greater than or less than another expression.
Do-Now: 1) What times can you park if there is NO PARKING from 1pm- 3pm? 2) How can we write this using an inequality?
By Mrs. Tise Oakland Elementary. a. 62 cents b. 57 cents c. 87 cents d. 97 cents.
Chapter 11: Graphing. Bar Charts Pie Charts Line Graphs.
Prime Numbers and composite numbers
AP Computer Science A – Healdsburg High School 1 Unit 7 - Conditional statements - Logical operators in Java - Example.
PYTHON PROGRAMMING Year 9. Objective and Outcome Teaching Objective Today we will look at conditional statements in order to understand how programs can.
Exercise : Write a program that print the final price of purchase at a store where everything costs exactly one dollar. Ask for the number of items purchased.
Hypothesis Testing I The One-sample Case
Sequence, Selection, Iteration The IF Statement
Comparison Operators Relational Operators.
Absolute Value The distance from a number to zero (on a number line).
Compare Unlike denominators
Graphing Linear Inequalities
Introduction To Robot Decision Making
ME 123 Computer Applications I
Counting & Comparing Money 2 $ $ $ $.
Counting & Comparing Money $ $ $ $.
Selection (IF Statements)
What is an equation? An equation is a mathematical statement that two expressions are equal. For example, = 7 is an equation. Note: An equation.
Associative Property of addition Commutative Property of addition
Introduction To Robot Decision Making
Conditional Logic Presentation Name Course Name
Inequalities Friday, 22 February 2019.
Inequalities and their Graphs
Understanding Conditions
JavaScript conditional
Relational Operators.
Notes Over 1.7 Solving Inequalities
Every number has its place!
Notes Over 1.7 Solving Inequalities
4.1 Inequalities and Their Graphs
Section 2.3 Systems of Linear Equations:
Inequalities and their Graphs
7.5 Write and Graph Inequalities
Course 2: Inequalities Objectives:
x ≤ 4 x = 4 x = 4 Describe the unshaded region.
If-Statements and If/Else Statements
Solving inequalities Chapter 3.
Chapter 2 Sets Active Learning Lecture Slides
Example Challenge Question
Inequalities x > a Means x is greater than a
Newtons' Laws A summary.
Presentation transcript:

Selection Statements Make a decision based on conditions Allows the computer to be intelligent

Testing for Conditions Compare two numbers for equality, less than, or greater than x > 0greater than x < yless than x >= 2greater than or equal to x <= 3less than or equal to x == 4equals x != ynot equals

Boolean Values Type bool has two possible values, true or false In C++, bool is the same as int, and true is really 1 or any positive value whereas false is really 0 The names bool, true, and false exist just for the programmers convenience

Evaluating a Comparison In C++, comparisons evaluate to 1 or 0, meaning true or false Comparisons are also called Boolean expressions cout << 5 < 10; // Displays 1 cout << 10 == 10;// Displays 1 cout << 10 != 10;// Displays 0

= and == In C++, == means equals whereas = means assignment // Assume x has the value 10 cout << x == 10; // Displays 1 cout << x = 10;// Sets the value of // x to 10 and displays // 10

Priority of Operators x + y < 3 + z// First arithmetic, then comparison (x + y) < (3 + z)// Add parentheses for clarity

Selection Statements // absolute value if (x < 0) x = -x; // Guard against division by 0 if (x == 0) cout << Error: attempt to divide by 0; else cout << y / x;

Syntax of Selection Statements if ( ) // One-way decision if ( ) // Two-way decision else

With Compound Statements if ( ) {. } if ( ) else {. }

Behavior of Selection Statements ? statement true ? statement false true false statement if ( ) {. } if ( ) else {. }

Logical Operators and Compound Boolean Expressions // see if number is within a range if (x >= 1 && x <= 6) cout << Number is between 1 and 6; // see if number is not within a range if (x 6) cout << Number is not between 1 and 6; &&and ||or !not All Boolean expressions return 1 or 0 in C++

Truth Tables for Logical Operators

Priority of the Operators

Multiway Selection // Run a drawing function based on a character command if (letter == C || letter == c) drawCube(); else if (letter == T || letter == t) drawTriangle(); else if (letter == R || letter == r) drawRectangle(); else cout << Unrecognized commmand;

Switch Statements // Run a drawing function based on a character command switch (letter) { case C: case c: drawCube(); break; case T: case t: drawTriangle(); break; case R: case r: drawRectangle(); break; default: cout << Unrecognized commmand; }