ppt lesson 08
TRANSCRIPT
Microsoft Visual Basic 2005 BASICS
Lesson 8
If Statements
Microsoft Visual Basic 2005 BASICS 2
Objectives
Explain how decisions are made in a computer.
Use the conditional operators. Use If statements to make decisions in
programs. Create and read basic flowcharts.
Microsoft Visual Basic 2005 BASICS 3
Objectives (cont.)
Use If…Else statements. Use check box controls. Use the logical operators.
Microsoft Visual Basic 2005 BASICS 4
The Building Blocks of Decision Making
Programs are limited without the ability to make decisions.
Programs must have a means to make decisions.
Decision making in computers Generally done in terms of a comparison
that returns a True or False response
Microsoft Visual Basic 2005 BASICS 5
The Building Blocks of Decision Making (cont.)
Microsoft Visual Basic 2005 BASICS 6
Using the Conditional Operators
The first step to making a decision in a program is to make a comparison.
Comparisons are made using the conditional operators.
Boolean variables Can be used to store the results of a
conditional expression
Microsoft Visual Basic 2005 BASICS 7
Using the Conditional Operators (cont.)
Microsoft Visual Basic 2005 BASICS 8
Using the Conditional Operators (cont.)
Microsoft Visual Basic 2005 BASICS 9
Using If Statements
If statement Allows you to execute specified code when
the result of a conditional expression is True
The End If statement marks the end of the If statement.
Microsoft Visual Basic 2005 BASICS 10
Using If Statements (cont.)
Code in between the If statement and End If statement is executed if the conditions specified in the If statement are true. Otherwise, the code is skipped.
Microsoft Visual Basic 2005 BASICS 11
Creating and Reading Flowcharts
Flowcharts Help you to plan and document program
code There are many symbols used in creating
flowcharts. The rectangle represents processing data. Use the diamond for making decisions. Use the parallelogram to represent input and
output.
Microsoft Visual Basic 2005 BASICS 12
Creating and Reading Flowcharts (cont.)
Microsoft Visual Basic 2005 BASICS 13
Creating and Reading Flowcharts (cont.)
Microsoft Visual Basic 2005 BASICS 14
Using If…Else Statements
A one-way selection structure Program structure in which the decision is
to go “one way” The If…Else statement
Allows you to choose between two paths
Microsoft Visual Basic 2005 BASICS 15
Using If…Else Statements (cont.)
In an If…Else statement One block of code is executed if the result
of an expression is True. Another block is executed if the result is
False.
Microsoft Visual Basic 2005 BASICS 16
Using If…Else Statements (cont.)
Microsoft Visual Basic 2005 BASICS 17
Using If…Else Statements (cont.)
Microsoft Visual Basic 2005 BASICS 18
Using Check Boxes
Check boxes are an important part of the Windows interface.
Check boxes allow the programmer to Ask the user a Yes or No question Turn an option on or off
Microsoft Visual Basic 2005 BASICS 19
Using Check Boxes (cont.)
Microsoft Visual Basic 2005 BASICS 20
Checked Property
Each check box has a Checked property. Set to True if the box is checked Set to False if the box is not checked
A check box has an integrated label. Appears beside it Text property specifies its label
Microsoft Visual Basic 2005 BASICS 21
Checked Property (cont.)
Microsoft Visual Basic 2005 BASICS 22
Setting the Default Checked Property
Check boxes can be set to be either checked or unchecked by default. Use the
Checked property
Microsoft Visual Basic 2005 BASICS 23
Setting the Checked Property with Code
The Checked property can be set and/or read from code.
Example chkHeart.Checked = True
Microsoft Visual Basic 2005 BASICS 24
Setting the Checked Property with Code (cont.)
Microsoft Visual Basic 2005 BASICS 25
Using the Logical Operators
Logical operators Can be used to combine several
comparisons into one statement Used with True and False values
Microsoft Visual Basic 2005 BASICS 26
Using the Logical Operators (cont.)
Microsoft Visual Basic 2005 BASICS 27
Order of Logical Operators
Logical operators Processed after the mathematical and
conditional operators Logical operators are processed in this
order Not And Or
Microsoft Visual Basic 2005 BASICS 28
Order of Logical Operators (cont.)
Microsoft Visual Basic 2005 BASICS 29
Summary
Decisions are reached by making comparisons. Comparisons in a computer generally return either a True or False value.
The conditional operators compare two values and return either True of False, depending on whether the expression is True or False.
Microsoft Visual Basic 2005 BASICS 30
Summary (cont.)
A Boolean variable can be used to store the results of an expression that includes conditional operators.
The If statement is the most common way to make a decision in a program, and it is a one-way selection structure. The code between the If and the End If is executed if the conditions in the If statement are met.
Microsoft Visual Basic 2005 BASICS 31
Summary (cont.)
Flowcharts allow programmers to plan and document program code using symbols connected by lines.
An If…Else statement makes a decision between two paths, and it is a two-way selection structure.
Microsoft Visual Basic 2005 BASICS 32
Summary (cont.)
Check boxes allow your program to ask the user Yes or No questions or to turn an option on or off. The Checked property of a check box is set to True when the check box is checked and False when the check box is not checked. A check box can be set to be checked or unchecked by default.
Microsoft Visual Basic 2005 BASICS 33
Summary (cont.)
Logical operators can be used to combine several comparisons into one statement. They are used with True and False values.
The Not operator reverses the value of a Boolean variable or expression.
The And operator returns True if the values connected by the And operator are both True.
Microsoft Visual Basic 2005 BASICS 34
Summary (cont.)
The Or operator returns True if either value connected by the Or operator is True.
The logical operators are last in the order of operations. Of the logical operators, Not comes first, then And, then Or.