The If…Then Statement

Download Report

Transcript The If…Then Statement

Chapter 3

Control Structures

The If…Then Statement

 The If…Then statement is a 

Decision statement =

that executes a set of statements when a condition is

True.

 Takes the form: If condition Then

statements

End If

If…Then cont…

Condition =

What the computer evaluates to be either True or False 

Statements =

What the computer does if the condition is True  Example:

If

intGuess = 7

Then Me.

lblMessage.Text = “You guessed it!”

End If

Relational Operators

< <= > >= <>  Relational Operators can be used to form Boolean expressions (remember Boolean is either True or False)

Operator Meaning

= Equal to Less than Less than or equal to Greater than Greater than or equal to Not equal to

If…Then…Else Statement

  Includes an optional

Else

clause that is executed when the

If

condition evaluates to False.

Takes the form: If condition Then

statements

Else

statements

End If

Nested If…Then…Else Statement

 An If…Then…Else statement can contain another If…Then…Else or If…Then statement, which is said to be nested.

 Nested statements should be indented for good programming style.

 Continued on next page..

Nested If…Then…Else Statement cont…

 Takes the form: If condition Then

statements

Else

If condition Then

statements

Else End If End If

statements

The If…Then…ElseIf Statement

  Used to decide among three or more actions Takes the form: If condition Then

statements

ElseIf condition Then

statements

… Else

statements

End If

The If…Then…ElseIf Statement cont…

 There can be multiple

ElseIf

clauses, and the last

Else

clause is optional.

 Comments are a must to keep complicated code much more readable. This is especially important for the last branch of a decision structure which usually does not include an explicit condition.

The Select…Case Statement

 The

Select…Case

Statement is a decision structure that uses the result of an expression to determine which block of code to execute.

 The

Select…Case

Statement is sometimes preferable to the

If…Then…ElseIf

statement because code may be easier to read.

Select…Case Statement cont…

Select…Case

takes the form: Select expression Case value

statements …

Case Else

statements

End Select

Example of Select…Case

Example:

Select Case

intScore

Case

0, 10

statements

Case

20

To

25

statements

Case Else

statements

End Select

‘Score is 0 or 10 ‘Score is 20,21,22,23,24 or 25 ‘Score other than 0,10,20, 21,22,23,24 or 25

Select…Case Statement cont…

 The

Select…Case

must evaluate to a built-in data type. Ex.

Integer, single, …

 Their can be multiple

Case

clauses, and the

Case Else

clause is optional.

IMPORTANT:

The

value

match the

expression

type should type and can be a single value, a list separated by commas, of a range separated by the keyword

To.

The Select…Case Is Statement

  Compares the result of an expression to a range of values when a relational operator is part of the

value.

Example:

Select Case

intScore

Case Is

< 10

statements

Case Is

< 25

statements

Case Is

>= 25

statements

End Select

Generating Random Numbers

 The phrase

Randomize()

must be included at the beginning of the procedure that is using the random number.

 To generate a random number use the built in

Rnd()

function  The formula for a random number is:

(High Number – Low Number) * Rnd() + Low Number

Which is set equal to some variable.

Message Box

 Can be displayed to alert the user of invalid data or as a reminder of options required for an application to continue.

 How to code a message box?

 MessageBox.Show(“message”,”title”)  The “title” is optional.

Counter Variables

 A

counter

is a variable storing a number that is incremented by a constant value.

 Counters are useful for keeping track of the number of times a user clicks a button, enters a guess, or types a password.

Updating

a counter takes the form:

counter = counter + constant

Counter Variables cont…

Counter

is the numeric variable that is updated.

Constant

is the number that is added to the current value of

counter.

 A counter should be initialized when it is declared and then updated by an unchanging amount.

Static Variables

 Anytime a counter variable is declared it should be declared as a static variable instead of dimensional.

 A variable declared as a local variable is redeclared every time through the event procedure unless it is declared as a static.

Ex:

Static

intCounter

As Integer

Static Variables cont…

 Extends the lifetime of a local variable.

 The value of a static variable is not redeclared and reinitialized each time the Click event occurs.

 Useful when assigning random numbers to variables.

The CheckBox Control

    Name = prefix = chk

Text =

text displayed next to the box

Checked =

can be set to either True or False to display the check box with or without a check, respectively.

Related check boxes are sometimes placed together in a GroupBox object. As with radio buttons, a group box should be added to the form before adding check boxes.

Line-Continuation Character

 Statements that are long can be typed onto two or more lines when the line continuation character is used.

 The underscore ( _ ) is the line continuation character.

 Must have a space before it and nothing after it and cannot occur within quotation marks.

Logical Operators

 A Boolean expression can be formed using the logical operators

And

and

Or.

 A

logical operator

joins two expressions to create an expression that evaluates to either True or False.

 A third logical operator used is

Not.

Not

changes an expression either from True to False or from False to True.

Logical Operators cont…

Logical Operators And

Expression1 Expression2 Result True True True True False False False True False False False False

Or

Expression1 Expression2 Result True True True True False True False True True False False False

Not

Expression Result True False False True

String Concatenation

  Two or more strings can be joined together in a process call

concatenation.

The & operator can be used to concatenate string. The & operator is used in an expression similar to the following:

newString = string1 & string2

newString

is a String variable that will store the result of the expression.

string1

and

string2

are String variables or string enclosed in quotation marks.

& cont…

 Example Dim strFirstName As String Dim strLastName As String Dim strFullName as String strFirstName = “Al” strLastName = “Bundy” strFullName = strFirstName & “ “ & strLastName StrFullName will = Al Bundy

& cont…

 Concatenation can also be used to combine two labels into one.

 Ex:  Me.lblMessage.text = “Your answer is “ & sngAnswer & “ inches”