1 / 23

第三章 Visual Basic 運算子

第三章 Visual Basic 運算子. 第三章 Visual Basic 運算子. 運算式 (Expression) 是由運算元 (Operand) 及運算子 (Operator) 組合而成。 例如: y=x+5 ,其中 x 與 5 稱為運算元, + 稱為運算子, y=x+5 稱為運算式。 運算子可分成算術運算子、比較運算子、布林運算子及串連運算子。. 算術運算子計有 加法 (+) 減法 (-) 乘法 (*) 除法 (/) 負 (-) 次方 (^) 整數除法 () 餘數 (Mod). 例如: x=22 , y=5 ,則 x+y=22+5=27 。

ketan
Download Presentation

第三章 Visual Basic 運算子

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. 第三章 Visual Basic運算子

  2. 第三章 Visual Basic運算子 • 運算式(Expression)是由運算元(Operand)及運算子(Operator)組合而成。 • 例如:y=x+5,其中x與5稱為運算元,+稱為運算子,y=x+5稱為運算式。 • 運算子可分成算術運算子、比較運算子、布林運算子及串連運算子。

  3. 算術運算子計有 加法(+) 減法(-) 乘法(*) 除法(/) 負(-) 次方(^) 整數除法(\) 餘數(Mod) 例如:x=22,y=5,則 x+y=22+5=27。 x-y=22-5=17。 x*y=22*5=110。 x/y=22/5=4.4。 x*(-y)=22*(-5)=-110。 x^y=22^5=5153632。 x\y=22\5=4。 x Mod y=22 Mod 5=2。 算術運算子

  4. 算術運算子使用時的優先順序如下

  5. Dim x, y, b, d As Integer Dim a, c As Single x = 17 y = 4 a = x / y b = x \ y c = x ^ y d = x Mod y TextBox1.Text = a TextBox2.Text = b TextBox3.Text = c TextBox4.Text = d

  6. 比較運算式: • 若比較運算式運算結果為真,在Visual BASIC.NET語言以「True」表示;若運算結果為假,以「False」表示

  7. 比較運算式: 例如:x=10,y=5,則 x>y(=10>5)=True。 x>=y(=10>=5)=True。 x<y(=10<5)=False。 x<=y(=10<=5)=False。 x=y(=10=5)=False。 x<>y(=10<>5)=True

  8. 比較運算式: • Like運算子用於比較兩個字串。 • Is運算子用於比較兩個物件參考變數。 • 例如: Dim Flag As Boolean Flag=“A” Like “a”, 則Flag=False。 Flag=“aVBa” Like “a*a”,則Flag=True。

  9. 比較運算式: 例如: Dim apple,egg As New Object Dim one,two,three As Object Dim Flag As Boolean one =apple two =apple three =egg Flag =one Is two,則Flag=True。 Flag = two Is three,則Flag=False。

  10. 布林運算子: • 布林運算子有 • Not • And • And Also • Or • OrElse • Xor

  11. 布林運算子 • NOT • NOT表示”非”的意思,用於描述否定的敘述。例如,「8>4」的敘述為True ,則「NOT (8>4)」的敘述為False。 • NOT真值表(True Table)為

  12. 布林運算子 例題ko3_2: 4 Dim a As Boolean 5 a = "台北市" Like "台?市" 6 TextBox1.Text = a 7 TextBox2.Text = Not a

  13. 布林運算子 • And • And表示”且”的意思,通常,使用And連接兩個運算,當兩個運算都為True時,邏輯運算結果為True (-1) • And真值表

  14. 布林運算子 • AndAlso • AndAlso連接兩個運算,當兩個運算都為True時,邏輯運算結果為True (-1)。若第一個運算為False,不必評估第二個運算,就可以得知結果為False (0)。 • AndAlso真值表

  15. 布林運算子 • Or • Or表示”或”的意思,使用Or連接兩個運算,兩個運算只要有一個為True,邏輯運算結果為True(-1)。 • Or真值表為

  16. 布林運算子 • OrElse • OrElse連接兩個運算,兩個運算只要有一個為True,邏輯運算結果為True(-1)。若第一個運算為True,不必評估第二個運算,就可以得知結果為True(-1) • OrElse真值表為

  17. Xor • Xor表示”互斥或”的意思,當兩個運算相異(一True一False)時,其結果為True(-1),其餘情況為False(0)。 • Xor真值表為

  18. 例題ko3_3: Dim x,y As Integer x = 13 y = 6 TextBox1.Text = x Or y TextBox2.Text = x And y TextBox3.Text = x Xor y

  19. 布林運算子使用時的優先順序

  20. 運算子使用時的優先順序

  21. 串連運算子 • 串連運算子包括&及+兩個運算子。 • &運算子用於將兩個字串串連起來 • 例如: Dim ko As String ko= “Visual” & “ Basic”, 則ko=” Visual Basic"

  22. 例題ko3_4: 4 Dim str1, str2, str3 As String 5 Dim n, m, s As Integer 6 str1 = TextBox1.Text 7 s = Len(str1) 8 n = Val(TextBox2.Text) 9 m = Val(TextBox3.Text) 10 str2 = Microsoft.VisualBasic.Left(str1, n - 1) 11 str3 = Microsoft.VisualBasic.Right(str1, s - m) 12 TextBox4.Text = str2 & str3

More Related