Lecture 6 - Decision Structures_And_Task1_continued
Transcript of Lecture 6 - Decision Structures_And_Task1_continued
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
1/20
Lecture 6: Decision Structures
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
2/20
Outline
In this lecture, we will discuss VB decision structures:The If statement
Allowing multiple choices with Else and ElseIf
Nested If statements
Comparison operators:= , , >= ,
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
3/20
Decision Structures
So far, our programs have been rather linearMost have executed single-path, unbranched computation:
Get Input Apply a single Computation Output Result
However, most real algorithms involve decision making .
Example: Assign grades to students, based on their scores.The ability to make decisions is called branching .
Blocks of code for making decisions are called DecisionStructures .
VB .NET provides several flavors:The IfThen statementThe IfThenElse statementThe Select Case statement
Each of these provides a means to control program flowVia evaluation of one or more test conditions .
Lets take a look at the simplest: The If Statement
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
4/20
The If Statement
The simplest VB .NET decision structure is the IfThen statement.This allows conditional execution of a set of statements
Based on satisfaction of a specific condition.
The VB syntax for the If statement is:
If (condition ) Then
Statements to execute when the condition is met End If
Here, If is a VB keyword:Telling the computer to test for satisfaction of a condition .
Note: The condition may be composite.
Example: Is x larger than 3?Then is another VB keyword:
Telling the computer to execute the Blocks statements:Example: Add 5 to x.
End If is a pair of VB keywords:
Which tells the computer that the IfThen block is over.
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
5/20
Understanding the SyntaxIn words, the IfThen statement says:
If the condition is true, then the statement(s) following the Then will be executed
Example:In words: If a students score is higher than 60, they can pass the test.VB code:
If (grade >= 60) Thenevaluation = pass
End If
The IfThen flow of control is shown moreclearly by a Flow Chart :
If the condition is True:The statements in the block take effect.Example: Student passes.
Otherwise:The statements in the block are by-passed.Example: Student fails to pass.
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
6/20
A Simple IfThen Program
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
7/20
The Else StatementIn the last example, we used two IfThen blocks:
The first tests whether the student passedAnd executes a pass statement.
The second tests whether the student failedAnd executes a fail statement.
This is rather redundant.
The Else statement allows binary selection of 2 processes:The Then blocks statements are executed if the condition is met.Otherwise, a set of statements in an Else block are executed.
The syntax for the IfThenElse structure is:
If (condition ) ThenStatements_1 (execute when the condition is met)
Else
Statements_2 (execute when condition is NOT met) End If
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
8/20
The Else Statement (cont.)The Flow of Control may be made more clear via a Flow Chart :
An IfThenElse structure thus acts like a pair of IfThen blocks:One for the If case;One for the If NOT case.
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
9/20
The ElseIf StatementThis idea may be generalized
To the linear testing of any number of conditionsusing the ElseIf statement (note: no space between Else and If ).
The syntax for the If Then ElseIf conditional structure is:
If (condition 1) Then
Statements_1 (execute when condition1 is met) ElseIf (condition 2)
Statements_2 (execute when condition2 is met).
.
.
Else
Statements_n (execute when NONE of the above conditions are met)End If
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
10/20
The Else Statement (cont.)The Flow of Control may be made more clear via a Flow Chart :
An IfThenElseIf structure thus acts like n IfThen statements:Sequentially testing n-1 explicit conditions
And 1 implicit condition (default failure).
Note the exclusivity: exactly one conditions block will execute
Thus: If two conditions test true, only the 1st
will execute!
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
11/20
A Simple IfThen Else ProgramSo, lets re-write our previous program (with 2 If Then Blocks)
As a singleIfThenElse
statement.
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
12/20
The Select Case StatementIn the special case where:
There are a large number of conditions (alternatives);A branch is selected based on the value of a single variable We may use a Select Case statement, instead of ElseIfThen .
The syntax for the Select Case conditional structure is:
Select Case (name_of_variable )Case condition_1 statement(s)_1Case condition_2 statement(s)_2
Case Else
statement(s)_nEnd Select
Here:name_of_variable is a variable to be evaluated;
Each condition (case assignment) is a possible value or range of values;Case Else
is the default failure (no case is true)End Select ends the Case Block.
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
13/20
A Simple Select Case ProgramSo, lets re-write our previous program
Using a Select Case statement.
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
14/20
Comparison OperatorsA comparison operator compares two operands
One on the right side and one on the left side of the operator.Thus, a comparison has the general form:
x op y
And returns a logical value based on whether the comparison is True or False.
Example: The Equality (= ) Operator compares operands,and
Returns True if they are equal;Returns False if they are not equal.
Example:
If (x = 5) Thenstatement_1
End If
Here, ( x=5 ) evaluates to True if x is equal to 5In which case, statement_1 will be executed.
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
15/20
Comparison Operators (cont.)Comparison operators come in 6 flavors:
Each compares the two operands , and returns a logical value
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
16/20
Logical OperatorsLogical operators operate on Boolean variables/expressions
And return a Boolean ( True or False ) result. The VB .NET Logical Operators include:
Truth tables (examples of use)
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
17/20
Program: Grade Assigner Make a program to evaluate student scores, and assign a
grade, according to the conditions below:
Our input will be the student test score .Our output will be the student grade.
For fun, lets allow use of either :an IfElse statement, or A Select Case statement.
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
18/20
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
19/20
-
8/14/2019 Lecture 6 - Decision Structures_And_Task1_continued
20/20