1 CS161 Introduction to Computer Science Topic #6.

Slides:



Advertisements
Similar presentations
Department of Computer Science. Definition “An operator is a symbol (+,-,*,/) that directs the computer to perform certain mathematical or logical manipulations.
Advertisements

If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Boolean Types & Compound Conditionals CSC 1401: Introduction to Programming with Java Lecture 4 – Part 3 Wanda M. Kunkle.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
CS150 Introduction to Computer Science 1
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Primitive Types CSE 115 Spring 2006 April 3 &
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Operators and Expressions
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 4: Basic C Operators
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Operations Making Things Happen. Our Scuba Program #include // cin, cout, > using namespace std; int main() { const double FEET_PER_ATM = 33.0, LBS_PER_SQ_IN_PER_ATM.
CSCI 1100/1202 January 28, The switch Statement The switch statement provides another means to decide which statement to execute next The switch.
OPERATORS.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
C++ Basics Tutorial 6 Operators. What are going to see today? Assignment operator(=) Arithmetic operators(+,-,*,/,%) Compound assignment(+=,-=,*=……..)
CPS120: Introduction to Computer Science Operations Lecture 9.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
Today in CS161 Lecture #8 Learn about… Logicals: And, Or, Not Repetition: loops! Rewrite our First Program Using loops Graphics Let’s make the graphics.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
LESSON 3 Expressions, Statements, & Operators. STATEMENTS A statement Controls the sequence of execution Evaluates an expression Does nothing (the null.
Principles of Programming - NI July Chapter 4: Basic C Operators In this chapter, you will learn about: Assignment operators Arithmetic operators.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Variables, Operators, and Expressions
Operators & Expressions
Control Structures I Chapter 3
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Operators And Expressions
University of Central Florida COP 3330 Object Oriented Programming
Computing Fundamentals
University of Central Florida COP 3330 Object Oriented Programming
Chapter 7: Expressions and Assignment Statements
Chapter 5: Loops and Files.
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Unary Operators ++ and --
Chapter-3 Operators.
Introduction to Programming – 4 Operators
Algorithms computer as the tool process – algorithm
Life is Full of Alternatives
Chapter 4: Expression and Operator
Life is Full of Alternatives
Using C++ Arithmetic Operators and Control Structures
OPERATORS in C Programming
HNDIT11034 More Operators.
OPERATORS in C Programming
Presentation transcript:

1 CS161 Introduction to Computer Science Topic #6

CS161 Topic #62 Today in CS161 More Selective Execution –Logical Operations: && and || –Truth Tables –Applying logicals to if statements Operator Precedence –Increment and Decrement Operators

CS161 Topic #63 Logical Operators There are 3 logical (boolean) operators: &&And (operates on two operands) ||Or (operates on two operands) !Not (operates on a single operand) && evaluates to true if both of its operands are true; –otherwise it is false.

CS161 Topic #64 Logical Operators || evaluates to true if one or the other of its operands are true; –it evaluates to false only if both of its operands are false. ! gives the boolean complement of the operand. –If the operand was true, it results in false.

CS161 Topic #65 Logical Operators Conditional ExpressionLogical value True/False (5 == 10) && (30 < 88)0False (5 == 10) || (30 < 88)1True !(5==10) && (30 < 88)1True 40 != 441True !(40 != 44)0False

CS161 Topic #66 Expressions in C++ Every expression in C++ results in a value For example, when you call the pow function, it results in a value that consisted of raising exp to the power supplied. This value can then be used within a larger expression, eg: x = y + pow(x,3); This value is called the residual value

CS161 Topic #67 AND Truth Table op1 && op2 results in: op1op2residual value truetruetrue 1 truefalsefalse 0 falsetruefalse 0 falsefalsefalse 0

CS161 Topic #68 OR Truth Table op1 || op2 results in: op1op2residual value truetruetrue 1 truefalsetrue 1 falsetruetrue 1 falsefalsefalse 0

CS161 Topic #69 NOT Truth Table !op1 results in: op1 residual value truefalse 0 falsetrue 1

CS161 Topic #610 Logicals in if Statements Now let’s apply this to the if statements. For example, to check if our input is only an ‘m’ or an ‘i’ char selection; cin >> selection if ((‘m’ != selection ) && (‘i’ != selection) ) cout << “Error! Try again”;

CS161 Topic #611 Logicals in if Statements Why would the following be incorrect? if ((‘m’ != selection ) || (‘i’ != selection) ) cout << “Error! Try again”; Because no mater what you type in (m, i, p, q) it will never be both an m and an i ! If an m is entered, it won’t be an i !!!!!

CS161 Topic #612 Logicals in if Statements Let’s change this to check if they entered in either an m or an i: (this is correct) if ((‘m’ == selection ) || (‘i’ == selection ) ) cout << “Correct!”; else cout << “Error. Try Again!”;

CS161 Topic #613 Logicals in if Statements Now, let’s slightly change this.... if (!((‘m’ == selection ) || (‘i’ == selection ))) cout << “Error. Try Again!”; Notice the parens...you must have a set of parens around the logical expression

CS161 Topic #614 Arithmetic Expressions Let's take a look on operations that can be performed on real data: result = +realvariable <== No change result = -realvariable <== Takes the negative result = a+b <== Takes the sum result = a-b <== Takes the difference result = a*b <== Takes the product result = a/b <== Performs division

CS161 Topic #615 Arithmetic Expressions Other Interesting Operators for... –Compound Assignment *=/=+=-= result += 10result = result+10 result *= x+yresult = result *(x+y) result /= x+yresult = result/(x+y)

CS161 Topic #616 Arithmetic Expressions One more operator... –Integer division /%(remainder) int number; number = 10/3;//answer is 3 number = 10%3;//answer is

CS161 Topic #617 Operator Precedence One operator cannot follow another ( is illegal)....you can do this by using parentheses: (2.5 + (-3.6)) With parentheses, the operations within parens is performed first. When parens are nested...the innermost set of parens is performed first: 2.0+(3.0*( )) is the same as 2.0+(3.0*3.0) which is 11.0

CS161 Topic #618 Operator Precedence Watch out for ambiguous expressions when parens are not used: –What does *4.0 mean? 7.0? Or, 16.0? Since no parens are given, we go by the order of precedence of operators. *, /, % have a higher precedence....than + and -. So, the answer to above is 7.0!

CS161 Topic #619 Operator Precedence What about, 3.0* /2.0? 1. First take the highest priority (* and /)...and go left to right. 2. So, first multiply 3.0*2.0 ==>> Then divide 7.0/2.0 ===>>> So, we have so far Which is 2.5

CS161 Topic #620 Increment/Decrement Ops There are two more operators that add or subtract 1 ++i means i = i i means i = i - 1 These are used in their prefix form They can also be used in a postfix form: i++i-- Although in books you will find the postfix form very common, get in the habit of using the prefix whenever possible!

CS161 Topic #621 Postfix Increment/Decrement i++means: 1) Residual value is the current value of the variable 2) Then, increment the variable by 1 3) For example: int i = 100; cout << i++; Displays 100 not 101!

CS161 Topic #622 Increment/Decrement More examples: int i; cin >> i; i++; ++i; cout << i; inputoutput

CS161 Topic #623 Increment/Decrement More examples: int i, j; cin >> i; j = i++; j = ++j; cout << j; inputoutput