Decision Structures

Event-Driven Programming

Event programming puts the user in charge. It waits patiently for a response, and then processes the input predictably.

These events-specific blocks of code process input, calculate new values, display output, and handle other tasks.

Each object in VB has a predefined set of events it can respond to.

 

Events in VB Objects

The events that each object can respond to are listed for each object in the Proc (Procedure) drop-down list box in the Code window.

We can write event procedures for any of these events.

If that event occurs, VB will execute the event procedure associated with it.

 

Conditional Expressions

One of the most useful tools for processing information in an event procedure is a conditional expression.

A conditional expression is a part of a program statement that asks a True-or-False question about a property, a variable, or another piece of data in the program code.

Conditional Expressions cont...

For example: Price < 100 evaluates to True if he Price variable contains a value that is less than 100, and it evaluates to False if Price contains a value that is greater than or equal to 100.

Additional Examples:

10 < > 20 True (10 is not equal to 20)

Score < 20 True if Score is less than 20

Score=Label1.Caption True if the Caption property on the Label 1 object contains the same value as the Score variable; otherwise, False

Text1.Text="Bill" True if the word Bill is in the first text box.

 

If…Then Decision Structures

An If…Then decision structure lets you evaluate a condition in the program and take a course of action based on the result.

For example: If Score >=20 then Label1.Caption = "You Win!" is an If…Then decision structure that uses the conditional expression Score >=20 to determine if the Label1.Caption box should display "You Win!"

If…Then Decision Structures

If the Score variable contains a value that is greater than or equal to 20, VB sets the Caption property; otherwise, it skips the assignment statement and executes the next line in the event procedure.

This type of comparison always results in a True or False value.

A conditional expression never results in maybe.

 

Testing Several Conditions in an If…Then Decision Structure

VB supports decision structures that allow you rot include several conditional expressions.

The keywords you will use for this type of expression are ElseIf, Else, and End If

The syntax

If conditiona1 Then

statements executed if condition 1 is True

ElseIf condition2 Then

statements executed if condition2 is True

[Additional ElseIF clauses and statements can be placed here]

Else

statements executed if none of the conditions is True

End If

 

Conditional Expressions cont...

Be sure to be careful about the order of your conditional expressions.

As soon as VB encounters a true statement it executes.

When you use more than one conditional expression, watch their order carefully.

 

Program Example

Let’s use If…Then statements to evaluate the input of a user and print a welcome statement.

What we need"

A command button asking for login

An InputBox asking the user to input their name

Conditional expression evaluating that input

A label box that displays a welcome message

A MsgBox that displays an error message if the person does not enter the right message

 

Logical Operators in Conditional Expressions

VB lets you test more than one conditional expression in If…Then and ElseIf clauses if you want to include more than one selection criterion in your decision structure.

The extra conditions are linked by using one of the following logical operators:

And--If both conditional expressions are True, then the result is True

Or--If either conditional expression is True, then the result is True

Not--If the conditional expression is False, then the result is True. If the conditional expression is True, then the result is False.

Xor--If one and only one of the conditional expressions is True, then the result is True. If both are True or both are False, then the result is False.

Program Modifications

Let’s add to our login program the "And" operator to also text for a correct password along with the correct login.

Therefore, in this program the login AND the password needs to be correct before the message is executed.