1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture Learning programming using Visual Basic Net – Chapter 5 Specifying alternate courses of action Selection statements

30 271 0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 30
Dung lượng 417,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Chapter 5 Specifying alternate courses of action Selection statements. Lecture Learning programming using Visual Basic Net – Chapter 5 Specifying alternate courses of action Selection statements Lecture Learning,Learning programming,Lecture Learning programming

Trang 1

Specifying Alternate Courses of Action: Selection Statements

Trang 2

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

Introduction

• A need to select an appropriate action from several

alternatives.

• Two statements will make this possible:

– The If…Then…Else statement.

– The Select Case statement.

Trang 3

• Construct programs that select alternative actions.

• Compare the Select Case statement with the If…Then… Else statement.

• Create GUIs using the MsgBox() function, Radio Button, GroupBox, and CheckBox.

Trang 4

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

5.1 The Decision-Making Process

• We must be very precise in writing the criterion and

alternative actions for decisions.

• In a program,

– A condition is represented as an expression.

– An outcome is the result of an evaluated condition.

– An appropriate action follows the outcome.

Trang 5

5.2 The If…Then Else Statement

• The If…Then…Else statement enables a program to

handle situations having two outcomes.

• The statement has three parts:

Trang 6

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

5.2 The If…Then Else Statement (cont.)

• Syntax and Action of If…Then…Else

– The If…Then…Else statement has the following syntax:

Trang 7

5.2 The If…Then Else Statement (cont.)

– Run Time: The Effect of the If…Then…Else Statement

• True: the computer executes the statements in

Trang 8

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

5.2 The If…Then Else Statement (cont.)

– Problem Solving and Pseudocode

• Problem solving is the process of writing code to

perform a required task.

• Pseudocode is an English-like outline of the logical for

program code.

– Ex Request address from user

Receive input in a textbox

Trang 9

5.2 The If…Then Else Statement (cont.)

– Using Logical Expressions in If…Then…Else Statements

• The condition of an If…Then…Else statement may be a

logical expression.

– Ex (YearsExperience>5) And (NumberOfLanguages>=3)

Trang 10

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

Trang 11

• If statements may contain one within another.

If (X<Y) And (Y<Z) Then

Trang 12

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

5.4 The MsgBox() Function

• Displays a message box on the screen and waits for the user to click one of the button.

• Returns a value that indicates which button the user

clicked.

• Syntax:

MsgBox(message, mbStyle, title) As

MsgBoxResult

Trang 13

• MsgBox() Button Combinations

Trang 14

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

5.4 The MsgBox() Function (cont.)

• MsgBox() Return Values

Trang 15

• Ensures that user will select only one option

– Appearance and Use

• Appears as descriptive text next to a circle.

• User selects with the mouse.

• Selection is transferred if user clicks another

radiobutton.

• Developers should limit the number of radiobuttons to

about seven per form.

Trang 16

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

Trang 17

– Events

• CheckChanged event.

– Occurs when the value of the Checked property changes.

Trang 18

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

5.6 The GroupBox Control

• Allows you to group RadioButtons to correspond to

categories of items.

– Appearance and Use

• Appears as a rectangle surrounding the controls it

groups together.

• Descriptive text can help identify the “group.”

• You may drag a RadioButton in or out of a GroupBox.

Trang 20

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

5.7 The CheckBox Control

• Used when a combination of options may be selected.

– Appearance and Use

• Appears as descriptive text next to a square.

• User may select a CheckBox by clicking it with the

mouse.

• User may deselect a CheckBox by clicking it again with

the mouse.

Trang 21

– Properties and Events

• Similar to the RadioButton.

• CheckState property gets or sets the state of the

Trang 22

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

5.7 The CheckBox Control (cont.)

– Properties of the CheckBox control

Trang 23

• Can handle conditions with multiple outcomes.

– Syntax and Action of Select Case

Select Case testexpression Case expressionlist1

Trang 24

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

5.8 The Select Case Statement (cont.)

– Run Time: The Effect of the Select Case Statement

• First the computer evaluates the test expression.

• Then it tries to match the resulting value.

• The search starts at the top expression list.

• The computer stops at the first match.

• The corresponding statement block is executed.

• Control resumes after the End Select.

Trang 25

• If no match occurs and a Case Else exists, then the Case

Else block is executed.

• Then control resumes after the End Select.

• If no match occurs and no Case Else exists, then control

resumes after the End Select.

Trang 26

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

5.8 The Select Case Statement (cont.)

– If versus Select Case

• If a decision has only two outcomes and the condition

can be expressed as a single logical expression, then the If statement is easier to read.

• If a decision has multiple outcomes that depend on a

single expression, then the Select Case is usually easier

to read.

• If the outcomes depend on a number of conditions that

may be independent, then embedded If statements are better.

Trang 27

• For decisions where one of the appropriate actions is to

stop processing.

• Causes execution to skip directly to End Sub.

• Is not limited to If…Then…Else and Select Case

statements.

Trang 28

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

Chapter Summary

• To make a decision, we first evaluate a condition and

determine its outcome.

• Programs must imitate the decision-making process.

• The logic of decision-making may be complex.

Trang 29

• Developers use pseudocode to describe the logical

steps in a problem solution.

• A meta statement describes a compound statement with

a single phrase.

• The If…Then…Else statement enables a program to

choose one of two actions based on a condition.

Trang 30

McGraw Hill/Irwin ©2002 by The McGraw-Hill Companies, Inc All

rights reserved

Chapter Summary (cont.)

• The RadioButton, GroupBox, and CheckBox are three

controls that enable the user to make choices with the

GUI.

• The SelectCase statement handles a single condition

that may have more than one outcome.

• The Exit Sub statement enables a program to stop

execution before a procedure is completed.

Ngày đăng: 16/05/2017, 14:41

TÀI LIỆU CÙNG NGƯỜI DÙNG

TÀI LIỆU LIÊN QUAN

w