The document discusses different types of decision structures in programming, including if/then statements, select case statements, and logical operators. It explains that decision structures control program flow and execution based on conditions. If/then statements can evaluate a single condition or include elseif to evaluate multiple conditions in order. Select case structures provide an efficient way to control program flow based on one variable. Logical operators like AND and OR can combine conditions to determine if code blocks execute.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
42 views25 pages
Lesson3 Selection
The document discusses different types of decision structures in programming, including if/then statements, select case statements, and logical operators. It explains that decision structures control program flow and execution based on conditions. If/then statements can evaluate a single condition or include elseif to evaluate multiple conditions in order. Select case structures provide an efficient way to control program flow based on one variable. Logical operators like AND and OR can combine conditions to determine if code blocks execute.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25
Using Decision
Structures
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama.
3/9/2009 1 Write conditional expressions. Use an If…Then statement to branch to a set of program statements based on varying condition. Use a select case statement.
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 2
In this lecture you’ll increase your programming vocabulary by creating code blocks called Decision structures that control how your program executes, or flows, internally.
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 3
A conditional expression is a part of a complete program statement that asks a True-or-false question about a property or a variable. You can use relational/comparison operators in a conditional expression such as =, < >, >, <, >= or <=.
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 4
You can use an If…Then decision structure to evaluate a condition in the program and take a course of action based on the result.
In its simplest form, an If…Then decision is
written on a single line. Syntax
If condition Then statement
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 5
If score>=20 Then Label1.text=“You Win” • If the score variable contains a value greater than 20 or equal to 20, visual basic sets the text property, otherwise it skips the assignment statement and execute the next line. •This sort of a comparison always results in a True or False value. •The conditional expression never results in maybe. APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 1- 6 3/9/2009 Syntax If condition Then statements Else statements endif
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 7
If score>=20 Then Label1.text=“You Win” Else Label1.text=“you fail” End if
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 8
Visual Basic also supports an If…Then decision structure that you can use to include several conditional expressions. This block of statements can be several lines long and contains important keywords Elseif, Else and End If . Identify this as a nested decision structure.
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 9
Syntax If condition1 Then statements executed if condition1 is true ElseIf condition2 Then statements executed if condition2 is true [Additional ElseIf and statements can be placed here] Else statements executed if non of the conditions is true End if
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 10
If score>=75 Then Label1.text=“A” Elseif score>=65 Then Label1.text=“B” Elseif score>=50 Then Label1.text=“C” Elseif score>=35 Then Label1.text=“S” Else Label1.text=“F” Endif
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 11
The order of the conditional experssions in your if…then and elseif staetements is critical. When you use more than one conditional expression, consider the order carefully. What happens if you reverse the order of the conditional expressions of grade evaluation example and list the score from lowest to the highest?
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 12
If score>=35 Then Even though the Label1.text=“S” same expressions Elseif score>=50 Then are evaluated as in the previous Label1.text=“C” example, the Elseif score>=65 Then result won’t be Label1.text=“B” same. Elseif score>=75 Then In an if…elseif Label1.text=“A” structure, conditions need to Else be listed in Label1.text=“F” ascending order. Endif APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 13 You can test more than one conditional expression in if…then…elseif clauses if you want to include more than one selection criterion in you decision structure. The extra conditions are linked together by using one or more logical operators listed as follows. And Or Not Xor APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 14 Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button1.Click If TextBox1.Text = "555-55-5555" _ And TextBox2.Text = "12345" Then Continuation MsgBox("Welcome to the system!") character Else MsgBox("I don't recognize this number") End If End Sub End Class
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 15
For the statements of the If structure to be executed, both conditions must be evaluated to True. If the first condition is evaluated to False, VB skips to the next line or the Else statement immediately, without testing the second condition.
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 16
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 17
With VB, you can also control the execution of statements in your programs by using Select Case Decision structure. A select Case structure is similar to an If…Then…Else structure, but it is more efficient when you’re making 3 or more branching decisions based on one key variable, or test case. You can also use Select Case structures to make your code more readable. APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 18 Select Case selector Case valuelist1 action1 Case valuelist2 action2 Case Else action of last resort End Select
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 19
You can use comparison operators to include a range of text values in a Select Case structure. To use the comparison operators, you need to include the Is keyword or To keyword in the expression to identify the comparison you’re making.
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 20
The Is keyword instructs the compiler to compare the test variable to the expression listed after the Is keyword.
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 21
The To keyword identifies a range of values.
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 22
Select Case Age Case Is < 13 label1.text=“Enjoy your youth!” Case 13 To 19 label1.text=“Enjoy your teens!” Case 21 label1.text=“You can drink wine with your meals!” Case Is > 100 label1.text=“Oh…That’s great!” Case Else label1.text=“That’s a nice age to be!” End Select APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 23 The above structure users several comparison operators to test the age variable and to display on of the five messages.
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 24
Q&A
APIIT City Campus – 2009 Feb – Revised by Nadeera Ahangama. 3/9/2009 1- 25