Operators and Expressions

Slides:



Advertisements
Similar presentations
Exercise 1 EECS, Peking University Exercise in Query Processing.
Advertisements

第七课:电脑和网络. 生词 上网 vs. 网上 我上网看天气预报。 今天早上看了网上的天气预报。 正式 zhèngshì (报告,会议,纪录) 他被这所学校正式录取 大桥已经落成,日内就可以正式通车 落伍 luòw ǔ 迟到 chídào 他怕迟到,六点就起床了.
Ch7 人口成長與一胎化家庭 國經所 袁國軒 潘鵬升.
Chapter 17 數位革命與全球電子市場 Global Marketing Warren J. Keegan Mark C. Green.
如何在Elsevier期刊上发表文章 china.elsevier.com
Performance Evaluation
控制流程實作 胡百敬 精誠公司恆逸資訊 二○一七年三月十九日
第 10 章 更多的查詢技巧.
本章大綱 2-1、程式、程式語言、程式架構 2-2、變數與資料型態 2-3、常數 2-4、 運算式、運算子和運算元
饮食治疗篇.
雅思大作文的结构 Presented by: 总统秘书王富贵.
-Artificial Neural Network- Hopfield Neural Network(HNN) 朝陽科技大學 資訊管理系 李麗華 教授.
Chapter 8 Liner Regression and Correlation 第八章 直线回归和相关
Chapter 5 Relational Algebra
Figure Interpreting. Introduction In recording an English figure, its three digits make one subsection, while in Chinese, its four digits make one subsection.
Understanding Interest Rates
Euler’s method of construction of the Exponential function
Population proportion and sample proportion
實 驗 研 究 法 多因子實驗設計 指導老師:黃萬居教授 學生:陳志鴻 m
Differential Equations (DE)
樹狀結構 陳怡芬 2018/11/16 北一女中資訊專題研究.
非線性規劃 Nonlinear Programming
第五讲 数据的分组、合并与转换.
C 程式設計— 控制敘述 台大資訊工程學系 資訊系統訓練班.
第 17 章 數位革命與 全球電子市場 © 2005 Prentice Hall.
普通物理 General Physics 27 - Circuit Theory
Fundamentals of Physics 8/e 27 - Circuit Theory
创建型设计模式.
组合逻辑3 Combinational Logic
C 語言簡介 - 2.
第14章 其它DSP设计库 14.1 总线控制库 14.2 复数信号库 14.3 Gates库 14.4 状态机函数库
變數命名 保留字(Reserved Word)
Interval Estimation區間估計
消費者偏好與效用概念.
本章中將會更詳細地考慮有關重複的概念,並且會 介紹for和do…while等兩種用來控制重複的敘述 式。 也將會介紹switch多重選擇敘述式。 我們會討論直接和迅速離開某種控制敘述式的 break敘述式,以及用來跳過重複敘述式本體剩餘 部份的continue敘述式。 本章會討論用來組合控制條件的邏輯運算子,最後.
樹 2 Michael Tsai 2013/3/26.
Programming Languages
Customer Expectations of Service
義守大學電機工程學系 陳慶瀚 第3章 VHDL Concurrent語法 義守大學電機工程學系 陳慶瀚
句子成分的省略(1).
進階 WWW 程式設計 -- PHP 語言結構 靜宜大學資訊管理學系 蔡奇偉副教授 2003
IBM SWG Overall Introduction
二、雅思学术类阅读题型 10种题型 5种大题型+5种小题型.
研究技巧與論文撰寫方法 中央大學資管系 陳彥良.
Dept. of Information Management OCIT February, 2002
第3章 Java語法的JSP程式 3-1 Java語言的基礎 3-2 JSP程式的基本架構 3-3 Java的變數與資料型態
Mechanics Exercise Class Ⅰ
Guide to a successful PowerPoint design – simple is best
在Microsoft Access 下 建立資料庫
中央社新聞— <LTTC:台灣學生英語聽說提升 讀寫相對下降>
关联词 Writing.
線性規劃模式 Linear Programming Models
Lab 4 買房負擔 著重: 不動產計算 是否可承擔起買房 (lab 4) 使用”分析藍本管理員” Excel : IF 函數/功能.
Expressions & Statements
從 ER 到 Logical Schema ──兼談Schema Integration
软件测试技术-白盒测试 张志强 2006.
高考应试作文写作训练 5. 正反观点对比.
第6章 運算式與運算子 [算術與多功能計算機]
计算机问题求解 – 论题1-5 - 数据与数据结构 2018年10月16日.
國立清華大學 National Tsing Hua University
進階 WWW 程式設計 -- PHP Array 靜宜大學資訊管理學系 蔡奇偉副教授
5. Combinational Logic Analysis
何正斌 博士 國立屏東科技大學工業管理研究所 教授
2 Number Systems, Operations, and Codes
怎樣把同一評估 給與在不同班級的學生 How to administer the Same assessment to students from Different classes and groups.
Race Conditions and Semaphore
第6章 PHP基本語法介紹.
Principle and application of optical information technology
Significant Figures 有效數字
Gaussian Process Ruohua Shi Meeting
Presentation transcript:

Operators and Expressions BY Nandini N.Gaikwad Assistant Professor Department of IT

Contents Objective Introduction Arithmetic operations Arithmetic expressions Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Type conversions in expressions

To be able to construct and evaluate expressions. Objectives To be able to construct and evaluate expressions. To master operator precedence and associativity To understand implicit type conversion and explicit type conversion. 能够书写表达式,计算表达式的值; 掌握运算符的优先级和结合性; 掌握类型的隐式转换和显式转换

3.1 Introduction An operator is a symbol that tells the computer to perform certain manipulations. An expression is a sequence of operands and operators that reduces to a single value. C operators can be classified into a number of categories. Arithmetic operators Relational operators Logical operators Assignment operators Increment and decrement operators Conditional operators Bitwise operators Special operators

3.2 Arithmetic operators The arithmetic operators in C Operator meaning + Addition or unary plus - Subtraction or unary minus * Multiplication / Division % modulo division

3.2 Arithmetic operators Note:, Integer division truncates remainder The % operator cannot be applied to a float or double. The precedence of arithmetic operators Unary + or - * / % + - 注意: 整数除法舍去余数; %运算符只能用于整数 算术运算符的优先级

3.10 Arithmetic expressions An arithmetic expression is a combination of variables, constants, and operators. For example, a*b-c  a*b-c (m+n)(x+y)  (m+n)*(x+y) ax2+bx+c  a*x*x+b*x+c

3.3 Relational Operators The relational operators in C are : Operator Meaning < less that <= less than or equal to > greater than >= greater than or equal to == equal to != not equal to

Relational Operators A relational expression yields a value of 1 or 0. 5 < 6 1 -34 + 8 > 23 - 5 0 if a=3, b=2, c =1; then a > b > c is ? the associativity of relational operators is left  right 关系表达式的结果为1或0 注意计算关系表达式的值与判断关系表达式值的真假在表示上的差别 计算时,若关系成立,结果为1,否则结果为0; 判断关系表达式值的真假时,只要表达式的值为非0,就表示关系成立;为0则表示关系不成立。 经常采用简化写法,如n%2!=0可写为n%2 关系运算符的结合型:左-〉右

3.4 Logical operators C has the following three logical operators && meaning logical and || meaning logical or ! meaning logical not ( unary operator ) Expressions connected by && or || are evaluated left to right, and evaluation stops as soon as the truth or falsehood of the result is known.

3.5 Assignment operators The use of shorthand assignment operators has three advantages: 1. What appears on the left-hand side need not be repeated and therefore it becomes easier to write. 2. The statement is more concise and easier to read. 3. The statement is more efficient. 使用复合的赋值运算符有三点好处: 容易书写 语句更简洁、易读 语句效率高

3.6 Increment and decrement operators C provides two unusual operators for incrementing and decrementing variables. The increment operator ++ adds 1 to its operand, while the decrement operator -- subtracts 1. The unusual aspect is that ++ and -- may be used either as prefix operators (before the variable, as in ++n), or postfix operators (after the variable: n++). In both cases, the effect is to increment n. But the expression ++n increments n before its value is used, while n++ increments n after its value has been used. 自增自减运算符 自增自减运算符“前置”和“后置”的区别 “前置” 变量先增值(或先减值),后被引用 “后置” 变量先被引用,后再增值(或后减值) 两种情况下,变量的值都加1

Consider the expression m = - n++ ; The increment and decrement operators can be used in complex statements. Example: m=n++ -j +10; Consider the expression m = - n++ ; The precedence of ++ and – operators are the same as those of unary + and -. The associatively of them is right to left. m = - n++; is equivalent to m = - (n++) ++和—的优先级同单目的+,- 相同,右结合

3.7 Conditional operator a ternary operator pair “? : ” is available in C to construct conditional expressions of the form expr1 ? expr2 : expr3 the expression expr1 is evaluated first. If it is non-zero (true), then the expression expr2 is evaluated, and that is the value of the conditional expression. Otherwise expr3 is evaluated, and that is the value. Only one of expr2 and expr3 is evaluated. 条件运算符是C语言中唯一的三目运算符

z = (a > b) ? a : b; /* z = max(a, b) */ 相当于 if ( a>b) z=a; else z=b;

3.9 Special operators 1. The Comma Operator The comma operator can be used to link the related expressions together. A comma-linked list of expressions is evaluated left to right and the value of right-most expression is the value of the combined expression. For example, the statement value = (x=10, y=5, x+y); first assigns the value 10 to x, then assigns 5 to y, and finally assigns 15 to value. Since comma operator has the lowest precedence of all operators, the parentheses are necessary. 逗号运算符的运算顺序是从左到右,表达式的值为最右端表达式的值 逗号运算符的优先级最低

3.14 Type conversions in expressions 1. Implicit Type Conversion C permits mixing of constants and variables of different types in an expression. C automatically converts any intermediate values to the proper type so that the expression can be evaluated without loosing any significance. This automatic conversion is known as implicit type conversion. The rule of type conversion: the lower type is automatically converted to the higher type. 类型的隐式转换:如果一个表达使用有多种类型,C自动由低类型向高类型转换

3.13 Some Computational Problems When expressions include real values, then it is important to take necessary precautions to guard against certain computational errors. For example, consider the following statements: a = 1.0 / 3.0; b = a * 3.0; There is no guarantee that the value of b will equal 1. Another problem is division by zero. The third problem is to avoid overflow and underflow errors. 表达式运算过程中可能出现一些运算错误 浮点数运算 0作除数 数据溢出

3.15 Operator precedence and Associativity Rules of Precedence and Associativity (1)Precedence rules decides the order in which different operators are applied. (2)Associativity rule decide the order in which multiple occurrences of the same level operator are applied. Table3.8 on page71 shows the summary of C Operators. for example, a = i +1== j || k and 3 != x 优先级和结合性的应用: 优先级决定了同一表达式中不同操作符的运算次序; 结合性决定表达式中同一优先级操作符的运算次序

THANKS