版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、Introduction to Computer(C Programming)Software College , Northeastern University2007,9Chapter3 Operators and Expressions 第1頁(yè),共36頁(yè)。3.1 introduction C operators can be classified into a number of categories. They include: Arithmetic operators. Relational operators. Logical operators. Assignment opera
2、tors. Increment and decrement operators. Conditional operators. Bitwise operators. Special operators.第2頁(yè),共36頁(yè)。3.2 Arithmetic operators Operator Meaning Addition or unary plus Subtraction or unary minus Multiplication Division Modulo division Examples:a-b, a+b, a*b, a/b, a%b第3頁(yè),共36頁(yè)。Example 3.1The Pr
3、ogram shows the use of integer arithmetic to convert a given number of days into months and days.main()int months, days; printf(“Enter daysn”); scanf(“%d”, &days); months = days/30; days = days % 30; printf(“Months=%d days=%d”, months, days);OutputEnter days265Months=8 Days=25Enter days364Months=12
4、Days=4Enter days45Months=1 Days=15第4頁(yè),共36頁(yè)。3.3 Relational OperatorsC supports six relational operators in all: Operators Meaning is less than is greater than = is greater than or equal to = is equal to != is not equal toExample4.5=10 4.5=010 is complement of = = = is complement of !=We can simplify
5、an expression involving the not and the less than operators using the complements as shown below: Actual one Simplified one !(x=y !(xy) x=y !(x!=y) x=y !(xy !(x=y) x 55 & salary 1000) 2. if (number 1000) 第8頁(yè),共36頁(yè)。3.5 Assignment OperatorsC has a set of shorthand operators of the form: v op =exp; wher
6、e v is a variable, exp is an expression op is a C binary arithmetic operator op= is known as the shorthand assignment operator v op =exp; is equivalent to v =v op (exp);Examples: x+=y+1; is equivalent to x=x+(y+1); x+=3; is equivalent to x=x+3;第9頁(yè),共36頁(yè)。Shorthand Assignment Operators Statement with s
7、imple Statement with assignment operator shorthand operator a = a + 1 a += 1 a = a 1 a -= 1 a = a * (n+1) a *= n+1 a = a / (n+1) a /= n+1 a = a % b a %= b第10頁(yè),共36頁(yè)。The use of shorthand assignment operators has three advantages:What appears on the left-hand side need not be repeated and therefore it
8、becomes easier to write.The statement is more concise and easier to readThe statement is more efficientExample: value(5*j-2) = value(5*j-2) + delta; With the help of the += operator, this can be written as follows: value(5*j-2) += delta;第11頁(yè),共36頁(yè)。Example 3.2The Program prints a sequence of squares o
9、f numbers. Note the use of the shorthand operator *=.#define N 100#define A 2main()int a; a = A; while(a b) ? a : b; if (a b) x = a; else x = b;第15頁(yè),共36頁(yè)。3.8 Bitwise OperatorC has a distinction of supporting special operators known as bitwise operators for manipulation of data at bit level. These op
10、erators are used for testing the bits, or shifting them right or left. Bitwise operators may not applied to float or double. Operator Meaning & bitwise AND | bitwise OR bitwise exclusive OR shift right第16頁(yè),共36頁(yè)。3.9 Special OperatorsPointer Operators: & and *Member Selection Operators: . and -第17頁(yè),共3
11、6頁(yè)。The Comma Operator: can be used to link the related expression together. A comma-linked list of expressions are evaluated left to right and the value of right-most expression is the value of the combined expression.Example: value = (x = 10, y = 5, x + y); value = 15;Some applications of comma ope
12、rator are: In for loops: for ( n = 1, m = 10, n d)?1:0);printf(“%dn”, (cd)?1:0);Output a=16 b=10 c=6 a=16 b=11 d=26 a/b = 1 a%b = 5 a*=b =176 0 1第20頁(yè),共36頁(yè)。An arithmetic expression of variables, constants, and operators arranged as per the syntax of the language. The complex mathematical expressions
13、are expressed by C expressions as follows: Algebraic expression C expression a * b c (m+n) * (x+y) a * b / c 3 * x * x + 2 * x + 1 x / y + c 3.10 Arithmetic Expressions第21頁(yè),共36頁(yè)。3.11 Evaluation of ExpressionsExpressions are evaluated using an assignment statement of the form variable = expression; V
14、ariable is any C variable name. When the statement is encountered first and the result then replaces the previous value of the variable on the left-hand side. All variables used in the expression must be assigned values before evaluation is attempted.Example x = a * b c; y = b / c * a; z = a b / c +
15、 d;第22頁(yè),共36頁(yè)。Example 3.4The program illustrates the use of variables in expressions and their evaluation.main()float a, b, c, x, y, z; a = 9; b = 12; c = 3; x = a b / 3 + c * 2 - 1; y = a b / (3 + c) * (2 - 1); z = a (b / (3 + c) * 2) 1;printf(“x=%fn”,x); printf(“y=%fn”,y); printf(“z=%fn”,z);Output
16、x=10.000000 y=7.000000 z=4.000000第23頁(yè),共36頁(yè)。3.12 Precedence of Arithmetic OperatorsAn arithmetic expression without parentheses will be evaluated from left to right using the rules of precedence of operators.Examplex = a-b/3+c*2-1When a = 9, b = 12, and c = 3 x = 9-12/3+3*2-1;第24頁(yè),共36頁(yè)。Rules for Eval
17、uation of ExpressionFirst, parenthesized sub expression from left to right are evaluated.If parentheses are nested, the evaluation begins with the innermost sub-expression.The precedence rule is applied in determining the order of application of operators in evaluation sub-expressions.The associativ
18、ity rule is applied when two or more operators of the same precedence level appear in a sub-expression.Arithmetic expressions are evaluated from left to right using the rules of precedence.When parentheses are used, the expressions within parentheses assume highest priority.第25頁(yè),共36頁(yè)。3.13 Some Compu
19、tational ProblemsApproximate values for real numbersa = 1.0 / 3.0;b = a * 3.0;Division by zeroAvoid overflow or underflow errors第26頁(yè),共36頁(yè)。Example 3.5The program shows round-off errors that can occur in computation of floating point numbers.main()float sum, n, term; int count = 1; sum = 0; printf(“En
20、ter value of nn”);scanf(“%f”,&n); term = 1.0/n; while( count = n)sum = sum +term;count+; printf(“Sum = %fn”, sum);Output Enter value of n 99 Sum = 1.000001 Enter value of n 143 Sum = 0.999999第27頁(yè),共36頁(yè)。3.14 Type Conversions in ExpressionsImplicit Type ConversionC permits mixing of constants and varia
21、bles 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.第28頁(yè),共36頁(yè)。int i,x,y;float f;double d;long int l; x =
22、l / i + i * f - d longlongfloatfloatfloatfloatdoubledoubleint第29頁(yè),共36頁(yè)。Conversion HierarchyNote that, C uses the rule that, in all expressions except assignments, any implicit type conversions are made from a lower size type to a higher size type as shown below:shortcharint unsigned int long int uns
23、igned long intfloatdoublelong doubleConversionHierarchy第30頁(yè),共36頁(yè)。Explicit ConversionThe general form of a cast is :(type-name) expressionWhere type-name is one of the standard C data types. The expression may be a constant, variable or an expression. Examplesx = (int) 7.5 a = (int) 21.3/(int)4.5b =
24、(doublt)sum/ny = (int)(a+b)z = (int)a+bp = cos(double)x) 第31頁(yè),共36頁(yè)。Example 3.6The program shows how to use a cast to evaluate the equation sum = (1/i)main()float sum;int n; sum = 0;for( n=1; n=10; +n)sum = sum +1/(float)n;printf(“%2d %6.4fn”, n, sum); Output 1 1.0000 2 1.5000 3 1.8333 4 2.0833 5 2.2833 6 2.4500 7 2.5929 8 2.7179 9 2.8290 10 2.9290第32頁(yè),共36頁(yè)。3.15 Operator Precedence and AssociativityEach operator in C has a precedence associated with it. This precedence is used to determine how an expression involving more than one ope
溫馨提示
- 1. 本站所有資源如無(wú)特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 二零二五版?zhèn)€人二手房交易合同(含車位使用權(quán))2篇
- 銷售活動(dòng)安全管理方案
- 老舊小區(qū)水管裝修方案
- 二零二五版新型土地廠房產(chǎn)權(quán)交易合同3篇
- 二零二五版協(xié)議離婚子女撫養(yǎng)權(quán)糾紛解決合同3篇
- 二零二五年度個(gè)人信托合同范本
- 透平機(jī)拆除施工方案
- 二零二五年度二手房全款買賣合同電子版范本
- 安徽防滑塑膠地板施工方案
- 二零二五版文化產(chǎn)業(yè)股權(quán)委托投資管理合同3篇
- 2025年福建新華發(fā)行(集團(tuán))限責(zé)任公司校園招聘高頻重點(diǎn)提升(共500題)附帶答案詳解
- 人教版【初中數(shù)學(xué)】知識(shí)點(diǎn)總結(jié)-全面+九年級(jí)上冊(cè)數(shù)學(xué)全冊(cè)教案
- 四川省成都市青羊區(qū)成都市石室聯(lián)合中學(xué)2023-2024學(xué)年七上期末數(shù)學(xué)試題(解析版)
- 咨詢公司績(jī)效工資分配實(shí)施方案
- 2024-2025學(xué)年人教版七年級(jí)英語(yǔ)上冊(cè)各單元重點(diǎn)句子
- 2025新人教版英語(yǔ)七年級(jí)下單詞表
- 公司結(jié)算資金管理制度
- 2024年小學(xué)語(yǔ)文教師基本功測(cè)試卷(有答案)
- 未成年入職免責(zé)協(xié)議書
- 項(xiàng)目可行性研究報(bào)告評(píng)估咨詢管理服務(wù)方案1
- 5歲幼兒數(shù)學(xué)練習(xí)題
評(píng)論
0/150
提交評(píng)論