decisions

14
1 Selection Statements VB.NET

Upload: nickywalters

Post on 26-May-2015

3.750 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Decisions

1

Selection Statements

VB.NET

Page 2: Decisions

222

Objectives

Understand how relational operators are used to create a conditionUnderstand and use IF statements, including nested IF statementsUnderstand and use compound conditionsDevelop and evaluate multiple solutions to the same problemSuccessfully write program solutions that require decision making

Page 3: Decisions

333

Selection Statements

The most common decision structure is the IF statement.

A condition is a boolean expression that evaluates to either true or false.

Comparison operators require numeric operands and produce a logical result.

Conditions typically involve one of six relational operators. Greater than > Less than < Greater than or equal >= Less than or equal <= Equal = Not equal <>

Page 4: Decisions

444

The IF Statement Conditions

Page 5: Decisions

555

Simple IF Statements (design)

Decision symbol

Process symbol

Page 6: Decisions

666

Solving the Overtime Problem

Page 7: Decisions

777

VB.net code Example

Task 1: Create programs to perform the functions shown in the flow chart examples.

Task 2: Create a version(2) of the Correct Change program (from Input/Output) using selection statements.

Private Sub btnCheck_Click()Dim Input, value1, value2 As IntegerIf Input = value1 Then ‘ Condition 1 is True

process A ‘so only this code runs

ElseIf Input = value2 Then ‘ Condition 2 is Trueprocess B ‘different response

Else ‘ Neither condition is True process C ‘can be error message

End IfEnd Sub

Page 8: Decisions

888

Nested IF Statements (design)

The term nested IF refers to an IF statement contained within the true or false branch of another IF statement.

Page 9: Decisions

999

True OR False Conditions

Page 10: Decisions

101010

Compound Conditions

A compound condition consists of two conditions within parentheses joined by a logical operator.

Logical Operators require logical operands and produce a logical result. NOT Logical opposite AND Both values are true OR At least one value is true XOR Exactly one value is true

Page 11: Decisions

111111

Logical Operators

Page 12: Decisions

121212

Operator Precedence

When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence. 1st : Evaluate all arithmetic/concatenation

operators 2nd : Evaluate all comparison operators 3rd : Evaluate all logical operators

Page 13: Decisions

131313

The Select…..Case Statement

When more choices are available use the Select – Case statement, an example is shown below.

Page 14: Decisions

141414

SummaryA condition is an expression that evaluates to either true or false.IF statements use conditions to choose between actions.The true and false branches of an IF statement may contain any valid statement, including other IF statements.A compound condition is two or more conditions joined by a logical operator.