1 / 12

运算符与表达式

运算符与表达式. 一 . 算术运算符与算术表达式: 1. 算术运算符: 加 + 减 ( 负号) - 乘 * 除 / 乘方 ^ 整除     取余 mod 字符串连接 & ( 或 +) 如: 86 为 1; 8 mod 6 为 2; “visual” & “basic” 为 “ visualbasic ” 2^3 为 8 。

anahid
Download Presentation

运算符与表达式

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 运算符与表达式 一. 算术运算符与算术表达式: 1. 算术运算符: 加 + 减(负号) - 乘 * 除 / 乘方 ^ 整除 \     取余 mod 字符串连接 & (或 +) 如:8\6 为 1; 8 mod 6 为 2; “visual” & “basic” 为 “visualbasic” 2^3 为 8。 * 优先次序: ^, -(负号), *和/, \, mod, +和-, &

  2. 运算符优先级举例: ^ * / \ (整除) Mod(求余) + -

  3. 2.算术表达式:把常量、变量等运算元素用算术运算符连起来的式子。2.算术表达式:把常量、变量等运算元素用算术运算符连起来的式子。 如:a*b/(c+d) 3.1415926*r^2 二. 关系运算符与关系表达式: 1. 关系运算符: 大于 > 大于或者等于 >= 小于 < 小于或者等于 <= 不等于 <> 等于 = 比较两个对象变量 Is 2. 关系表达式: 关系表达式运算结果为逻辑型数值true或false 如:5<=6 为 true object1 Is object2 若两对象相同则true,不同则false

  4. 见下例

  5. 三. 逻辑运算符与逻辑表达式: 1. 逻辑运算符: 逻辑非 not 逻辑与 and 逻辑或 or 逻辑异或 xor * 优先次序:not, and, or, xor 2. 逻辑表达式: 逻辑表达式运算结果也是逻辑型数值true或false 如:若a=true 则not a = false 若a和b都为true,则a and b = true,否则 a and b = false 若a和b有一个为true,则a or b = true,否则 a or b = false 若a和b一个为true一个为false,则a xor b = true, 否则 a xor b = false

  6. 关系表达式结果计算举例

  7. 四. 多运算符表达式的运算规则: 1. 优先次序:先处理括号内表达式 然后处理算术运算符 然后处理关系运算符 最后处理逻辑运算符 2. 表达式返回值类型: a. 若两个运算对象的类型相同,则返回同一类型结果 注意:dim x as integer, y as integer, z as integer x=400 : y=400 z=x*y —— 将出错!因为整型范围是-32768~32767 b. 若类型不一样,返回的类型为存储长度较长的那个对象的 类型。 c. 除法运算,无论除数和被除数为何类型,结果都是双精度实数。

  8. 运算符的优先级举例

  9. 表达式的书写 虽然Visual Basic中的表达式与代数中的算式很相似,但二者却是两个完全不同的概念。在书写Visual Basic程序中的表达式时,不要受代数知识的影响。有一些字符是不可在Visual Basic的程序代码中出现的(字符串中除外),比如:≥、≠、±、×、÷、和上下标(如乘方)

  10. 表达式的书写规则 (1)运算符不能相邻。例a+-b是错误的。 (2)表达式中的每个字符没有高低、大小的区别(即不能出现下标、分子及分母)。 (3)只能使用圆括号,可以多重使用,圆括号必须成对出现。 (4)VB表达式中的乘号“*”不能省略。 (5)能用内部函数的地方尽量使用内部函数。 例如:数学式 写成VB表达式为: (-b+sqr(b^2-4*a*c))/(2*a)

More Related