1 / 16

第四章 常量和变量

第四章 常量和变量. 李红玲. 变量的声明. 在 VB 中,对每个变量必须先定义,后使用 。. 1 )隐式声明 在使用一个变量的之前并不专门声明这个变量而是直接使用. 例如: ADOU =3.14. 双精度. BINT =789. 整型. CSTR =“789”. 字符串. Ddate=#05/01/1999#. 日期. 显示声明. Dim 变量名 [ 类型符| as 类型词 ]. Dim bint As Integer. Dim bint%. Dim clong as long. Dim clong&. Dim adou#.

lita
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. 第四章 常量和变量 李红玲

  2. 变量的声明 在VB中,对每个变量必须先定义,后使用。 1)隐式声明 在使用一个变量的之前并不专门声明这个变量而是直接使用 例如:ADOU =3.14 双精度 BINT =789 整型 CSTR =“789” 字符串 Ddate=#05/01/1999# 日期

  3. 显示声明 • Dim 变量名 [类型符|as 类型词] Dim bint As Integer Dim bint% Dim clong as long Dim clong& Dim adou# Dim Adou as double Dim dsing! Dim dsing as single Dim fcu@ Dim fcu as currency Dim XA As String Dim xa$ DimB As String *20 Dim cb as boolean

  4. 一条语句中可以声明多外变量。 • Dim a as integer,b as integer,c as long • Dim a ,b as integer ,c as long

  5. 给变量赋值 • “=”为赋值运算符 • 将=右边的表达式值赋给它左边的变量 • 例如:A=123 C=156.3 D=“SDF” • E=D+1 F=f+3 g=FALSE

  6. 练习一 • 显示声明如下变量(变量名自定) • 整型 长整型 定长字符串 (6) • 货币型 变长字符串 单精度 双精度 • 布尔型 日期型

  7. 练习二 求它的周长和面积。其中π用符号常量PI来表示,半径和周长、面积用变量来表示 文件名为yuan.frm yuan.vbp

  8. 用Deftype定义一组变量 • Deftype 字母范围 • 定义以字母开头的变量均为此型。 • 例如:Defint a-b • 类型说明符总是比Defint语句优先起作用

  9. 变体类型 • 在变量声明时没有说明变量的数据类型,则该变量被默认为变体类型。 • 变体类型可以自动转换为定义的标准类型 • 例:DIM A • A=“123” • B=A+78 • C=B+”W” Dim a ,b ,c as integer 其中只有C是整型,其他均为变体

  10. 变量类型的选择 • (1)如运行时总是整数,则尽量选择long,精确度高,速度快 • (2)有小数点时,要根据其精度来确定,单精<双精<货币 • (3)尽量选择占用内存较少的 • (4)将实数赋给整型时,可以进行四舍五入。

  11. 练习 • 显示声明如下 变量 • 整型A=123 长整型 B=32896 • 双精度 C=3.1415926 单精度 D=1.23 • 货币型 S=123.5698 字节型 E=12 • 定义串name(8) 变长串 address • 布尔型 aux 日期型 borndate

  12. 练习二 求它的周长和面积。其中π用符号常量PI来表示,半径和周长、面积用变量来表示 文件名为yuan.frm yuan.vbp

  13. Private Sub Form_Click() • Const PI# = 3.14 • Dim r As Double, l As Double, s As Double • r = 5 • l = 2 * PI * r • s = PI * r ^ 2 • Print l, s • End Sub

  14. 方法Print 格式: [对象名.]print [表达式][,|;] • (1)对象名称可以是窗体、图片框、打印机,也可以是立即窗口。省略对象时默认为窗体。 • 例如: • form1.print “a” Picture1.print “a” print.print “a” debug.print “a” • (2)表达式表是一个或多个表达式,可以是数值表达式或字符串

  15. 3)当输出多个表达式或字符串时,各表达式用分隔符, 或;隔开,;是紧凑格式,,是下一个区,14个字符的间隔。 • (4)Print 可以先计算后输出。 • (5)一般情况下,每执行一次Print语句要自动换行,如果要在同一行输出,可以加“,”或“; “

  16. 静态变量和动态变量 • 静态变量每次会保留原来的值 • 动态变量会重新被赋值 • 例如: • private Sub form1_click( ) • Static value as integer • Value=value+1 • Print value • End sub • Private Sub Form_Click() • Dim value As Integer • value = value + 1 • Print value • End Sub

More Related