com148x1 interactive programming lecture 3. topics today string functions using timer looping

27
COM148X1 Interactive Programming Lecture 3

Upload: darrell-obrien

Post on 28-Dec-2015

224 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

COM148X1Interactive Programming

Lecture 3

Page 2: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Topics Today

String Functions Using Timer Looping

Page 3: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

String Functions

Page 4: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

String Class

Visual Basic provides many useful string functions and they can be used through all String object

Position of characters in string is starting from 0

All string functions will not alter the original string, another string variable is required to hold the result

Page 5: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Commonly Use String Functions

Function Description Example

ToUpper() change the content to upper case s2 = s.ToUpper()

ToLower() change the content to lower case s2 = s.ToLower()

Length() length of string len = s.Length()

Substring(pos, len)

return substring from pos with length len

s2 = s.Substring(2, 5)

IndexOf( substring)

return position of substring, return -1 if substring not found

pos = s.IndexOf(“hello”)

Page 6: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Commonly Use String Functions (cont’)

Function Description Example

Trim() remove leading and trailing space s2 = s.Trim()

Remove (pos, len)

remove a number of characters from string starting from pos

S2 = s.Remove(3, 5)

Insert( pos, string)

insert a string to pos s2 = s.Insert(3, “Hello”)

Page 7: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Using Timer

Page 8: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

What is Timer

Timer is an invisible stopwatchto do count down from present time

Once timer finished count down, corresponding code set by programmer will be executed

Page 9: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Locate Timer from Toolbox

Page 10: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Timer Properties

Enabled Count down will start if it is True

Interval Number of milliseconds for count down, once

finished count down, call-back function (written by programmer himself) will be executed

Page 11: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Looping

Page 12: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

What is Looping

Looping is a program control structure which allows computer executes part of program multiple times

In Visual Basic, looping can be done by For Next loop or Do While loop

Page 13: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

For Next Loop

For Next loop mainly used to do countable repetitions, for example, calculate the total income for a year (12 months)

A counter variable (a variable that is used for count) is required for the loop to count the number of repetition

Page 14: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

For Next Loop 1

i = v1

Action

i = i + 1 i v2?

yes

no

Page 15: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

For Next Loop 1 in VB

For variable = v1 To v2action

Next variable Example

For month = 1 to 12income = InputBox(“Income “ & mo

nth)sum = sum + income

Next month

Page 16: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

For Next Loop 2

i = v1

Action

i = i + c i v2?

yes

no

Page 17: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

For Next Loop 2 in VB

For variable = v1 To v2 Step c actionNext variable

Example For id = 1 to max_id step 2

‘print student name with odd id‘…

Next id

Page 18: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

For Next Loop 3

i = v1

Action

i = i + c i v2?

yes

no

exit before the loop complete

Page 19: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

For Next Loop 3 in VB For variable = v1 To v2 Step c

action part 1If (condition) Then

Exit ForEnd Ifaction part 2

Next variable Example

For month = 1 to 12income = InputBox(“Income “ & month)If (income < 0) Then

MsgBox(“No more income, stop input”);

Exit ForEnd ifsum = sum + income

Next month

Page 20: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Do While Loop

Unlike For Next loop, Do While loop mainly used to do uncountable repetitions, for example, supermarket calculator (different customers will buy different number of products)

Do While loop can always be used to replace For Next loop but not the reverse in Visual Basic

Page 21: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Do While Loop 1

Action

Condition

yes

no

Page 22: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Do While Loop 1 in VB

Do While conditionaction

Loop Example

Do While price > 0sum = sum + priceprice = InputBox(“Enter price”)

Loop

Page 23: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Do While Loop 2

Action

Condition yes

no

Page 24: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Do While Loop 2 in VB

Doaction

Loop While condition Example

Do password = InputBox(“Password”)

Loop While password <> “##21#”

Page 25: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Exit Do While Loop Before Completion

Similar to For Next loop, Do While loop can be exit before completion by using Exit Do statement

Page 26: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Infinity Loop

Loop that will never ends Actions that may cause infinity loop

Modify counter variable during the execution of For Next loop

Use wrong condition for Do While loop Forget to update variable which used in the

condition of Do While loop Intentionally make it as infinity loop

Page 27: COM148X1 Interactive Programming Lecture 3. Topics Today String Functions Using Timer Looping

Nested Loop

Loop exist inside another loop The loop inside will be executed multiple times Example

For row = 1 to 10For column = 1 to 5

name = InputBox(“Student name at row = “ & row & “, col = “ & column)

‘ do something with the name‘...

next columnnext row