background image

Podstawy programowania 

(2)

dr Jerzy Bartoszek

jbartoszek@wskiz.edu

jerzy.bartoszek@put.poznan.p

l

background image

Instrukcja if

If condition Then

[statements]

[ElseIf condition-n Then

[elseifstatements] …]

[Else [elsestatements]]
End If 

background image

Przykład instrukcji if

Module Module1
 Sub Main()
  Dim intInput As Integer

System.Console.WriteLine("Enter an integer…")
intInput = 

Val(System.Console.ReadLine())

If intInput = 1 Then
   System.Console.WriteLine("Thank you.")
ElseIf intInput = 2 Then
    System.Console.WriteLine("That's fine.")
ElseIf intInput = 3 Then
    System.Console.WriteLine("Too big.")
Else System.Console.WriteLine("Not a number I know.")
End If

 End Sub
End Module 

background image

Instrukcja Select Case

Select Case testexpression

[Case expressionlist-n 
[statements-n
]]
… 
[Case Else [elsestatements
]]

End Select 

background image

Przykład Select Case

Module Module1
 Sub Main()

Dim intInput As Integer System.Console.WriteLine("Enter an integer…")
intInput = Val(System.Console.ReadLine())

Select Case intInput

 Case 1

 System.Console.WriteLine("Thank you.")

 Case 2 

System.Console.WriteLine("That's fine.")

 Case 3 

System.Console.WriteLine("OK.")

 Case 4 To 7 

System.Console.WriteLine("In the range 4 to 7.")

 Case Is > 7 

System.Console.WriteLine("Definitely too big.")

 Case Else 

System.Console.WriteLine("Not a number I know.")

End Select

 End Sub
 End Module
 

background image

Instrukcja For

For index = start To end [Step 

step] [statements]

[Exit For]

[statements]

Next [index]

 

background image

Przykład For

Module Module1
 Sub Main()

 Dim intLoopIndex As Integer
 

For intLoopIndex = 0 To 3 

System.Console.WriteLine _

("Hello from Visual Basic")

 Next intLoopIndex

 End Sub
End Module 

background image

Instrukcja Do…Loop

Z warunkiem na początku:

Do [{While | Until} condition ]

 [statements]

 [Exit Do]

 [statements

Loop

 

background image

Instrukcja Do…Loop

Z warunkiem na końcu:

Do

 [statements]

 [Exit Do]

 [statements

Loop

 

[{While | Until} condition ]

background image

Przykład Do…Loop

Module Module1
 Sub Main() 

Dim strInput As String 

Do Until 

UCase(strInput)

 = "STOP" 

System.Console.WriteLine _
("What should I do?")
strInput = System.Console.ReadLine() 
Loop

 End Sub 
End Module 

background image

Instrukcja While

While condition 

[statements]

End While

 
Przykład:

Dim i As Integer = 0
While i<10

 

System.Console.WriteLine (i)

 i +=1

End While

background image

Imports

Imports

 

System.Console

Module Module1
 Sub Main() 

WriteLine

("Hello from Visual 

Basic")

 End Sub 
End Module

 

background image

Operacje na łańcuchach

Module Module1
 Sub Main()
  Dim strText1 As String = "welcome to visual 

basic"

  Dim strText2 As String 
  Dim strText3 As String

 strText2 = 

UCase

(strText1)

 strText3 = strText1.

ToUpper

 

System.Console.WriteLine(strText2) 

System.Console.WriteLine(strText3)

 End Sub
End Module 

background image

Operacje na łańcuchach cd.

Module Module1
 Sub Main()
  Dim strText1 As String = "Hey, look here!„
  Dim strText2 As String
  Dim strText3 As String
  strText2 = 

Mid(strText1, 6, 4)

strText3 = 

strText1.Substring(5, 4)

 

System.Console.WriteLine(strText2) 

System.Console.WriteLine(strText3)

 End Sub 
End Module
 

background image

Łańcuchy ↔ liczby

Module Module1
 Sub Main()
  Dim strText1, strText2 As String = 

"1234„

  Dim intValue1 As Integer
  intValue1 = 

Val(strText1)

strText2 = 

Str(intValue1)

 

System.Console.WriteLine(strText2)

 End Sub
End Module

 

background image

Znaki ↔ liczby

• Asc(

"A"

) returns 65

• Chr(65) returns 

"A"

Dim OneCharacter As Char = 

"

a

pple"

 

background image

Funkcje matematyczne

Imports System.Math

Module Module1 
 Sub Main()

 
System.Console.WriteLine(

Sin

(1.046)

)

 End Sub 
End Module

 

Abs, Cos, Exp, Log, Sqr, Round, …

background image

Daty i czas

Imports System.Math
Module Module1
 Sub Main()
  Dim FirstDate As Date FirstDate = 

#12/31/2007#

System.Console.WriteLine("New date: " & 

DateAdd(DateInterval.Month, 22, FirstDate)

)

While 

TimeString

 < 

"15:45:00"

End While
Beep() 

 End Sub 
End Module 

background image

Wyliczenia

Imports System.Console
Module Module1
 

Enum Days 

Sunday = 1 
Monday = 2 

Friday = 6 
Saturday = 7 

End Enum 

Sub Main() 

WriteLine("Friday is day " & 

Days.Friday

)

 End Sub 
End Module
 

Friday is day 6

 

background image

Tablice jednowymiarowe

Dim name (max_ind) As type

Przykład:

Dim persons(

4

) As 

String

persons(1) = "Bobby"

' persons(

5

) = "David"  Throws 

' System.IndexOutOfRange

Exception

 

Dim Data() = {10, 3, 2}

ReDim

 

Preserve

 persons(10) 

background image

Tablice dwuwymiarowe

Dim name (max_ind1, max_ind2) As type

Przykład:

Dim tab(

2, 3

) As 

Integer

tab(1, 1) = 15
Dim tabx()() As Integer = { _

New

 Integer(4) {},_

New

 Integer(1) {},_

New

 Integer(2) {} } 

tabx(0)(3) = 5

 


Document Outline