ppt lesson 08

34
Microsoft Visual Basic 2005 BASICS Lesson 8 If Statements

Upload: linda-bodrie

Post on 19-May-2015

896 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS

Lesson 8

If Statements

Page 2: Ppt lesson 08

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.

Page 3: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 3

Objectives (cont.)

Use If…Else statements. Use check box controls. Use the logical operators.

Page 4: Ppt lesson 08

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

Page 5: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 5

The Building Blocks of Decision Making (cont.)

Page 6: Ppt lesson 08

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

Page 7: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 7

Using the Conditional Operators (cont.)

Page 8: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 8

Using the Conditional Operators (cont.)

Page 9: Ppt lesson 08

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.

Page 10: Ppt lesson 08

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.

Page 11: Ppt lesson 08

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.

Page 12: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 12

Creating and Reading Flowcharts (cont.)

Page 13: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 13

Creating and Reading Flowcharts (cont.)

Page 14: Ppt lesson 08

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

Page 15: Ppt lesson 08

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.

Page 16: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 16

Using If…Else Statements (cont.)

Page 17: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 17

Using If…Else Statements (cont.)

Page 18: Ppt lesson 08

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

Page 19: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 19

Using Check Boxes (cont.)

Page 20: Ppt lesson 08

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

Page 21: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 21

Checked Property (cont.)

Page 22: Ppt lesson 08

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

Page 23: Ppt lesson 08

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

Page 24: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 24

Setting the Checked Property with Code (cont.)

Page 25: Ppt lesson 08

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

Page 26: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 26

Using the Logical Operators (cont.)

Page 27: Ppt lesson 08

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

Page 28: Ppt lesson 08

Microsoft Visual Basic 2005 BASICS 28

Order of Logical Operators (cont.)

Page 29: Ppt lesson 08

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.

Page 30: Ppt lesson 08

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.

Page 31: Ppt lesson 08

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.

Page 32: Ppt lesson 08

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.

Page 33: Ppt lesson 08

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.

Page 34: Ppt lesson 08

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.