Conditional Logic October 6, 2017.

Slides:



Advertisements
Similar presentations
Chapter 4 - Control Statements
Advertisements

Strings Testing for equality with strings.
Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 4 Making Decisions in a Program.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
Geography 465 Assignments, Conditionals, and Loops.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
CPS120: Introduction to Computer Science
CSD 340 (Blum)1 Ifs. CSD 340 (Blum)2 Page that asks user for background color and changes fore color from black if user selects black as background color.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 51 Decisions Relational and Logical Operators If Blocks Select Case Blocks.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Random Functions Selection Structure Comparison Operators Logical Operator
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
 Type Called bool  Bool has only two possible values: True and False.
APS105 Deciding 1. Comparing 2 Relational Expressions Relational expression: –Compare two values (or values of variables) Examples: x > 5 income < expenses.
JavaScript: Conditionals contd.
Bill Tucker Austin Community College COSC 1315
C++ LANGUAGE MULTIPLE CHOICE QUESTION
Discussion 4 eecs 183 Hannah Westra.
Relational Operator and Operations
The Ohio State University
Basic concepts of C++ Presented by Prof. Satyajit De
If/Else Statements.
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Sequence, Selection, Iteration The IF Statement
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Chapter 4: Making Decisions.
CMPT 201 if-else statement
EGR 2261 Unit 4 Control Structures I: Selection
Debugging and Random Numbers
IF statements.
Introduction to C++ October 2, 2017.
Decision Making in Code Logical Tests & Truth Logical Expressions
Chapter 4: Making Decisions.
Control Structures – Selection
Prime, Composite, FACTORS, and Multiples
Lesson 8: Boolean Expressions and "if" Statements
Decision Structures, String Comparison, Nested Structures
Relational Operators Operator Meaning < Less than > Greater than
Objectives After studying this chapter, you should be able to:
Logical assertions assertion: A statement that is either true or false. Examples: Java was created in The sky is purple. 23 is a prime number. 10.
Chapter 7 Conditional Statements
Flow Control Statements
Logical Operations In Matlab.
Selection Control Structure
Selection Statements.
If Statements.
Chapter 4: Control Structures I (Selection)
Conditional Logic Presentation Name Course Name
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
CS2011 Introduction to Programming I Selections (I)
The Data Element.
Chapter 5 Decisions.
Chapter 3: Selection Structures: Making Decisions
CS 101 First Exam Review.
The Data Element.
Welcome back! October 11, 2018.
Selection Control Structure
Boolean Expressions September 1, 2019 ICS102: The course.
Conditionals.
Programming Fundamental-1
String Methods Strings have actions known as method. We will review a few of the methods associated with strings that are a part of the built in Java.
Presentation transcript:

Conditional Logic October 6, 2017

New vocab!! Selection – A generic term for a type of programming statement that uses a Boolean condition to determine (i.e., select) whether or not to run a certain block of code. This generally, but not always, refers to an if statement. Know this for the AP!

Comparison Operators == “is equal to” != “is not equal to” > “is (strictly) greater than” < “is (strictly) less than” >= “is greater than or equal to” <= “is less than or equal to” The AP exam will use > and <.

Comparison Operators (2 + 2) == 4 evaluates to TRUE. "Joe Perry" == "joe perry" evaluates to FALSE (strings are case- sensitive). "Ringo" <= "Ringo" evaluates to TRUE. "TGOD" != "tgod" evaluates to TRUE. N.B. Some languages will allow you to compare a string to a number (e.g., "3" == 3). C++ will give you an error if you try to do this!

Comparing Strings Inequalities such as <= and >= can be used with strings—these perform a comparison in lexicographical order. In English: which would come first in a dictionary? Capital letters are "less than" lowercase letters since they have lower ASCII values. "A" < "a" "Dog" < "dog" "dog" < "dogwood"

Logical Operators && AND || OR ! NOT This is the character above \ (itself above Enter) on the keyboard. ! NOT

Logical Operators TRUE && TRUE evaluates to TRUE. TRUE && FALSE evaluates to FALSE. FALSE && TRUE evaluates to FALSE. FALSE && FALSE evaluates to FALSE.

Logical Operators TRUE || TRUE evaluates to TRUE. TRUE || FALSE evaluates to TRUE. FALSE || TRUE evaluates to TRUE. FALSE || FALSE evaluates to FALSE.

Logical Operators !TRUE evaluates to FALSE. !FALSE evaluates to TRUE.

Comparison Operators N.B. Do NOT use a string of inequalities, such as the following: if(x < z < y) { cout << "z is between x and y.\n"; } The proper way to do this is with the "and" operator: if( (x < z) && (z < y) )

Logical Operators Consider the following code: int cohen_age = 29; cohen_age >= 21 && cohen_age < 30; The bolded statement evaluates to TRUE.

Logical Operators Consider the following code: int cohen_age = 29; cohen_age >= 21 && cohen_age < 25; The bolded statement evaluates to FALSE. The following would evaluate to TRUE: cohen_age >= 21 || cohen_age < 25;

Logical Operators Be careful with parentheses! int cohen_age = 29; string day = "Friday"; !((cohen_age == 29) && !(day == "Friday")) The bolded code evaluates to TRUE. cohen_age == 29 is TRUE. day == "Friday" is TRUE—so !(day == "Friday") is FALSE. TRUE && FALSE evaluates to FALSE. !(FALSE) evaluates to TRUE.

Comparison Operators Generally, comparison operators are used as part of if statements: string band; cout << "What is your favorite band?\n"; cin >> band; if(band == "Aerosmith" || band == "aerosmith") { cout << "Good answer.\n"; } else cout << "Try again.\n";

Comparison Operators Generally, comparison operators are used as part of if statements: int age; cout << "What is your age?"; cin >> age; if(age >= 21 && age <= 29) { cout << "You're in the prime of your life.\n"; } else cout << "That's unfortunate.\n";

Logical Operators OR has a slightly different meaning in logic than in ordinary usage. Consider: “Is this elevator going up or down?” We would normally answer “up” or “down.” The logical answer is “yes”: either it is going up or it is going down.