Herbert G. Mayer, PSU CS Status 7/19/2015

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

IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
CS 3850 Lecture 5 Operators. 5.1 Binary Arithmetic Operators Binary arithmetic operators operate on two operands. Register and net (wire) operands are.
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Simple Data Types and Statements. Namespaces namespace MyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx.
LESSON 6 – Arithmetic Operators
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
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.
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.
Operators & Expressions
Department of Electronic & Electrical Engineering Expressions operators operands precedence associativity types.
Copyright Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
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.
Expression and Operator. Expressions and Operators u Examples: 3 + 5; x; x=0; x=x+1; printf("%d",x); u Two types: –Function calls –The expressions formed.
CS 161 Introduction to Programming and Problem Solving Chapter 12 C++ Statements Herbert G. Mayer, PSU Status 10/8/2014 Initial content copied verbatim.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
Department of Software.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Operators & Expressions
CompSci 230 S Programming Techniques
C++ Programming Language Lecture 4 C++ Basics – Part II
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
Chapter 12 Variables and Operators
Chap. 2. Types, Operators, and Expressions
University of Central Florida COP 3330 Object Oriented Programming
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Selections Java.
University of Central Florida COP 3330 Object Oriented Programming
Relational Operations
Data Types, Identifiers, and Expressions
Chapter 7: Expressions and Assignment Statements
ECE 103 Engineering Programming Chapter 3 Numbers
Variable Declaration, Data types, Expressions
CISC/CMPE320 - Prof. McLeod
Precedence and Associativity
Operators and Expressions
Expressions and Assignment Statements
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
All the Operators 22-Nov-18.
Relational Operators Operator Meaning < Less than > Greater than
More about Numerical Computation
Chapter 7 Additional Control Structures
All the Operators 4-Dec-18.
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Expressions and Assignment Statements
Associativity and Prescedence
CISC/CMPE320 - Prof. McLeod
Introduction to Programming – 4 Operators
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Expressions.
Chapter 3 Operators and Expressions
All the Operators 6-Apr-19.
All the Operators 13-Apr-19.
ECE 103 Engineering Programming Chapter 35 C Pointers, Part 1
ECE 103 Engineering Programming Chapter 38 C Pointers, Part 2
C++ Programming Language Lecture 4 C++ Basics – Part II
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Review of Previous Lesson
OPERATORS in C Programming
Operator King Saud University
Expressions and Assignment Statements
OPERATORS in C Programming
Presentation transcript:

ECE 103 Engineering Programming Chapter 17 Relational and Logical Operators Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

Syllabus Expressions Relational Operators Logical Operators Examples

Expressions An expression is any combination of: Constants and variables parentheses function calls operators that, when evaluated, produces a value An expression can be tested to make one selection out of several possible choices expr choice 1 choice 2

Table 1: C Operator Precedence Table in Decreasing Order Description Operator Associativity 1 Function call ( ) left-to-right 5 Bitwise left shift << Array subscript [ ] Bitwise right shift >> Struct member . 6 Less than < Struct dereference -> Greater than > Increment (post) expr++ LT or equal to <= Decrement (post) expr-- GT or equal to >= 2 Indirection * right-to-left 7 Equal to == Reference (addr) & Not equal to != Unary plus + 8 Bitwise AND Unary minus - 9 Bitwise XOR ^ Logical negation ! 10 Bitwise OR | Bitwise NOT ~ 11 Logical AND && Increment (pre) ++expr 12 Logical OR || Decrement (pre) --expr 13 Conditional ? : Cast ( type ) 14 Assignment = += -= *= /= %= >>= <<= &= ^= |= Size in bytes sizeof 3 Multiplication 15 Comma , Division / Highest precedence is Level 1. Lowest precedence is Level 15. Modulo % 4 Addition Subtraction 3

Relational Operators Relational operators compare one value against another value, and yield boolean result Operator Description > Greater than >= Greater than or equal to < Less than <= Less than or equal to == Equal to != Not equal to 4

the expression value is Important: == (equal to) is not the same as = (assignment) The value of a relational expression is true or false Be careful when comparing: Numeric values of differing data types Calculated floating point numbers if expr evaluates to the expression value is true 1 false 5

 Expr Initial values Evaluates to Value 5 >= 1 - 'A' > 'B' m < 3 m ← 2 m ← 5 sin(x) > w+1 x ← 0, w ← 0 x ← 0, w ← -2 k == 100 k ← 75 k ← 100 k != 100 1 < u <= 3 u ← 5 true 1 false true 1 false false true 1 false true 1 true 1 false true 1

Logical operators are used to perform Boolean logic evaluations A value of zero is interpreted as false A non-zero value is interpreted as true Operator Description && Logical AND || Logical OR ! Unary negation 7

Suppose A and B are general expressions: C uses short-circuit evaluation; not all languages do! Short-circuit evaluation  Evaluation stops as soon as the true or false status is determined That means, some subexpressions may not get evaluated during run time A !A false 1 true A B A && B A || B false true 1 8

Example: Expr Initial values Evaluates to Value (5 >= 1) && 0 - 'A'<'B' && 'B'<'C' (k<1&&w==2)||n!=3 k← -2, w←2, n←3 m >= 0 && m <= 5 m ← -2 m ← 2 m ← 6 m < 0 || m > 5 false true 1 true 1 false true 1 false true 1 false true 1 9

Right: -1 <= x && x <= 7 Better: ( -1 <= x ) && ( x <= 7 ) Example: Write an expression to check if a variable x is between -1 and 7, inclusive. Wrong: -1 <= x <= 7 Right: -1 <= x && x <= 7 Better: ( -1 <= x ) && ( x <= 7 ) -- the “Golden Rule”: When in -- doubt: Parenthesize!! 10

Example: Expr Initial values Evaluates to Value sum sum ← 0 sum ← 1 false true 1 true 645 true 1 false false 11

Relational and logical expressions can be assigned Example: int x = -3; bool state = ( x>5 ) || ( x<0 ); // true int a, b=0, c=3; a = (b==(c==(c++)));//( c==(c++) ) 1 // ( b==1 )  0 // a=0  0 12

C does not directly support set membership: Inclusion or exclusion tests are done individually, or, if the set is ordered, by using a range test Example: Check if c is one of these characters: 'A', 'B', 'C', or 'D' c=='A' || c=='B' || c=='C' || c=='D' Alternative: c >= 'A' && c <= 'D’ Better: ( c >= 'A' ) && ( c <= 'D’ ) Check if x is specifically one of these values: 5, 10, 120 x == 5 || x == 10 || x == 120 Why would ( x >= 5 ) && ( x <= 120 ) not be correct? 13