Controlling program flow &Built-in functions : Controlling program flow &Built-in functions Prepared by
D.Sakthivel M.Sc CS,MBA
Lecturer,Dept CT & IT
KGCAS Slide 2: Computer derive part of their amazing power from their ability to repeat operations any number of times
Repeats an operation is a loop
Repeating an operation a fixed number of times is called a determinate loop
To write the code that tells the computer to continue repeating an operation until it reaches a target or while some condition is true or false is called a indeterminate loops Slide 3: Determinate loops
It repeats an operation a fixed number of times
for index/counter = start to end
next index/counter Slide 4: Example:
To print numbers from 1 to 10
Start no: 1 ; ending no: 10(fixed number)
Dim I as integer
for I =1 to 10
next I (increment) O/P
10 Slide 5: Set Counter variable to
Starting value Is
Ending value? Body of loop Increment
Counter variable Move to statements
after body of loop
(after Next keyword) No Yes Using Step Keyword : Using Step Keyword The Step keyword tells vb to change the computer by specified amount rather by 1.
Dim I as Integer
For I = 10 to 1 step -1
print ” It’s Minus” &I& “and Counting “
Next I Nested for-Next loops : Nested for-Next loops Example:
For J% = 2 to 12
For I% = 2 to 12
O/P : Multiplication tables from 2 to 12 Indeterminate Loops : Indeterminate Loops Do loops:
To write the code that tells the computer to continue repeating an operation until it reaches a target or while some condition is true or false
Until Condition is met Slide 9: Is
met? Body of loop Move to statements
after body of loop
(after loop keyword) No Yes Slide 10: Private Sub Form_Load()
STR$ = InputBox("ENTER ANY NAME")
If STR$ <> “KGCAS" Then
MsgBox "ENTER VALID NAME"
Loop Until STR$ = “KGCAS"
End Sub Slide 11: Do Until
As an alternative approach to nesting an If-Statement inside the loop, Visual Basic provides a Do Until statement. Its syntax is the following:Do Until (Expression) (Code to execute)Loop
(Expression) can be any legal logical expression that we wish to evaluate to determine whether or not to exit the loop. Each time the program reaches Loop it will evaluate this expression. If the expression is True, it will exit the loop for us, but otherwise it will continue looping.. So let's try rewriting our Fibonacci program to use a Do-Until loop instead of Exit Do. Slide 12: Do While
In the place of Do Until, you can also use Do While. Its syntax is the following:Do While (Expression) (Code to execute)Loop Dim cnt As Integer 'Our counter
cnt = 1
Do Until cnt >= 8
cnt = cnt + 1 Slide 13: Is
met? Body of loop Move to statements
after body of loop
(after loop keyword) No Yes Flowchart for both while and Do-while loops While loop : While loop Syntax:
It first check the condition ;then executes the statements until the condition becomes satisfied or true.
wend Dim temp1 As Integer Dim temp2 As Integer temp1 = 6 temp2 = 0 While temp1 > temp2 temp2 = temp2 + 1 MsgBox ("temp1 is more then temp2") Wend Decision branching : Decision branching The If Statement
To check the condition if its true executes true statements.
If Condition Then One or more Visual Basic statements End If Dim a As Integer Dim b As Integer a = 3 b = 4 If a > b Then MsgBox ("a is greater then b") Else MsgBox ("b is greater then a") End If Slide 16: If Statement's Else
Whereas If executes code based on the condition's true condition, the Else statement executes code based on the condition's false condition. Else is an optional part of the If statement. Else specifies the code that executes if the condition is false. Syntax:
If condition Then One or more Visual Basic statementsElse One or more Visual Basic statementsEnd If num = Int(Text1.Text) 'InputIf (num > 0) Then strReply = "You have a car!"Else strReply = "You don't have a car!"End IfIf (num > 1) Then strReply = "Are you rich!"'display our commentLabel1.Caption = strReply Slide 17: Select Case
If you have a lot of conditional statements, using If..Then..Else could be very messy. For multiple conditional statements, it is better to use Select Case the format is :
Select Case expression Case value1 Block of one or more VB statements Case value2 Block of one or more VB Statements Case value3 Block of one or more VB statements Case value4 . . . Case Else Block of one or more VB Statements End Select Slide 18: Dim grade As String Private Sub Compute_Click( ) grade=txtgrade.Text Select Case grade Case "A" Label1.Caption="High Distinction" Case "A-" Label1.Caption="Distinction" Case "B" Label1.Caption="Credit" Case "C" Label1.Caption="Pass" Case Else Label1.Caption="Fail" End Select End Sub Built In Function : Built In Function String Functions : String Functions we will learn how to use some of the string manipulation function such as Len, Right, Left, Mid, Trim, Ltrim, Rtrim, Ucase, Lcase, Instr, Val, Str ,Chr and Asc.
(i)The Len Function
The length function returns an integer value which is the length of a phrase or a sentence, including the empty spaces. The format is
Len (VisualBasic) = 11 and Len (welcome to VB tutorial) = 22 Slide 21: (ii) The Right Function
The Right function extracts the right portion of a phrase. The format is
Right (“Phrase”, n)
Where n is the starting position from the right of the phase where the portion of the phrase is going to be extracted. For example,
Right(“Visual Basic”, 4) = asic
(iii)The Left Function
The Left$ function extract the left portion of a phrase. The format is
Where n is the starting position from the left of the phase where the portion of the phrase is going to be extracted. For example,
Left (“Visual Basic”, 4) = Visu
(iv) The Ltrim Function
The Ltrim function trims the empty spaces of the left portion of the phrase. The format is
Ltrim (“ Visual Basic”, 4)= Visual basic Slide 22: (v) The Rtrim Function
The Rtrim function trims the empty spaces of the right portion of the phrase. The format is
Rtrim (“Visual Basic ”, 4) = Visual basic
(vi) The Trim function
The Ttrim function trims the empty spaces on both side of the phrase. The format is
Trim (“ Visual Basic ”) = Visual basic
(viii) The Mid Function
The Mid function extracts a substring from the original phrase or string. It takes the following format:
Mid(phrase, position, n)
Where position is the starting position of the phrase from which the extraction process will start and n is the number of characters to be extracted. For example,
Mid(“Visual Basic”, 3, 6) = ual Bas Slide 23: The Ucase and the Lcase functions
The Ucase function converts all the characters of a string to capital letters. On the other hand, the Lcase function converts all the characters of a string to small letters. For example,
Ucase(“Visual Basic”) =VISUAL BASiC
Lcase(“Visual Basic”) =visual basic
The InStr function
The InStr function looks for a phrase that is embedded within the original phrase and returns the starting position of the embedded phrase. The format is
Instr (n, original phase, embedded phrase)
Where n is the position where the Instr function will begin to look for the embedded phrase. For example
Instr(1, “Visual Basic”,” Basic”)=8 RND Functions : RND Functions Rnd is very useful when we deal with the concept of chance and probability. The Rnd function returns a random value between 0 and 1. In Example 1. When you run the program, you will get an output of 10 random numbers between 0 and 1. Randomize Timer is a vital statement here as it will randomize the process.
Private Sub Form_Activate
For x=1 to 10
End Sub Slide 25: The Output for example 1 is shown below:
The Numeric Functions
The numeric functions are Int, Sqr, Abs, Exp, Fix, Round and Log.
a) Int is the function that converts a number into an integer by truncating its decimal part and the resulting integer is the largest integer that is smaller than the number. For example, Int(2.4)=2, Int(4.8)=4, Int(-4.6)= -5, Int(0.032)=0 and so on.
b) Sqr is the function that computes the square root of a number. For example, Sqr(4)=2, Sqr(9)=2 and etc.
c) Abs is the function that returns the absolute value of a number. So Abs(-8) = 8 and Abs(8)= 8. Slide 26: d) Exp of a number x is the value of ex. For example, Exp(1)=e1 = 2.7182818284590
e) Fix and Int are the same if the number is a positive number as both truncate the decimal part of the number and return an integer. However, when the number is negative, it will return the smallest integer that is larger than the number. For example, Fix(-6.34)= -6 while Int(-6.34)=-7.
f) Round is the function that rounds up a number to a certain number of decimal places. The Format is Round (n, m) which means to round a number n to m decimal places. For example, Round (7.2567, 2) =7.26
g) Log is the function that returns the natural Logarithm of a number. For example,
Log 10= 2.302585 Date Functions : Date Functions Format(36715.5784, "general date")....... 7/8/00 1:52:54 PM
Format(36715.5784, "short date") ....... 7/8/00 Format(36715.5784, "medium date")....... 08-Jul-00
Format(36715.5784, "long date") ....... Saturday, July 08, 2000
Format(36715.5784, "short time") ....... 13:52 Format(36715.5784, "medium time") ....... 01:52 PM Slide 28: Now Returns the current date and time together
Date Returns the current date
Time Returns the current time
For the examples that follow, assume that the current Date/Time (Now) is Friday, August 31, 2001 at 9:15:20 PM.
DateValue: Returns the date portion of a Date/Time value, with the time portion "zeroed out". (Note: When the time portion of a date/time variable is "zeroed out", the time would be interpreted as 12:00 AM.)
Example: Dim dtmTest As Date
dtmTest = DateValue(Now)
At this point, the date portion of dtmTest is 8/31/2001, with a time portion of 0 (12:00 AM midnight).
TimeValue: Returns the time portion of a Date/Time value, with the date portion "zeroed out". (Note: When a date/time variable is "zeroed out", the date will actually be interpreted as December 30, 1899.)
Example: Dim dtmTest As Date
dtmTest = TimeValue(Now)
At this point, the time portion of dtmTest is 9:15:20 PM, with a date portion of 0 (12/30/1899). Slide 29: Weekday: Returns a number from 1 to 7 indicating the day of the week for a given date, where 1 is Sunday and 7 is Saturday. Example:intDOW = Weekday(Now)
WeekdayName: Returns a string containing the weekday name ("Sunday" thru "Saturday"), given a numeric argument with the value 1 through 7. Example:strDOW = WeekdayName(6) ' strDOW = "Friday"
Month:Returns a number from 1 to 12 indicating the month portion of a given date. Example: Int Month = Month(Now) ' intMonth = 8
MonthName: Returns a string containing the month name ("January" thru "December"), given a numeric argument with the value 1 through 12. Example: strMoName = MonthName(8) ' strMoName = "August"
Day: Returns a number from 1 to 31 indicating the day portion of a given date. Example: int Day = Day(Now) ' intDay = 31
Year: Returns a number from 100 to 9999 indicating the year portion of a given date. Example:intYear = Year(Now) ' intYear = 2001 Slide 30: Hour: Returns an integer specifying a whole number between 0 and 23 representing the hour of the day. Example: int Hour = Hour(Now) ' intHour = 21 (for 9 PM)
Minute: Returns an integer specifying a whole number between 0 and 59 representing the minute of the hour. Example: int Minute = Minute(Now) ' intMinute = 15
Second: Returns an integer specifying a whole number between 0 and 59 representing the second of the minute. Example: int Second = Second(Now) ' intSecond = 20 Slide 31: Thanking You