0% found this document useful (0 votes)
67 views47 pages

CMPE 1403 Intro. To Programming (VB) : Bahçeşehir University Department of Computer Engineering

This document provides a summary of lecture 3 from the Introduction to Programming (VB) course at Bahçeşehir University's Department of Computer Engineering. The lecture covers decisions in programming using relational and logical operators, if blocks, and select case blocks. It includes examples of using these structures to evaluate conditions and execute different code blocks based on conditional logic. Key topics are ANSI character values, relational operators and expressions, logical operators, Boolean variables and expressions, syntax for if and select case blocks, and multiple examples demonstrating their use.

Uploaded by

Escaldes
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views47 pages

CMPE 1403 Intro. To Programming (VB) : Bahçeşehir University Department of Computer Engineering

This document provides a summary of lecture 3 from the Introduction to Programming (VB) course at Bahçeşehir University's Department of Computer Engineering. The lecture covers decisions in programming using relational and logical operators, if blocks, and select case blocks. It includes examples of using these structures to evaluate conditions and execute different code blocks based on conditional logic. Key topics are ANSI character values, relational operators and expressions, logical operators, Boolean variables and expressions, syntax for if and select case blocks, and multiple examples demonstrating their use.

Uploaded by

Escaldes
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 47

Baheehir University Department of Computer Engineering

CMPE 1403 Intro. to Programming (VB) Lecture 3


Fall 2010 Burcu Bakr Gngr

CMPE 1403 Introduction to Programming (VB)

Chapter 4 Decisions

Road Map
Relational and Logical Operators If Blocks Select Case Blocks

ANSI Values
A numeric representation for every key on the keyboard and for other assorted characters. Total of 94 characters. Adding 1 for the space character produced by Space bar makes 95 characters.

ANSI Values
They have numbers ranging from 32 to 126 associated with them.
32 (space) 33 ! 34 35 # 48 0 49 1 57 9 65 A 66 B 90 Z 125 } 98 b

ANSI Values
If n is anumber between 0 and 255, then Chr( n ) is the string consisting of the character with ANSI value n. If str is any string, then Asc( str ) is the ANSI value of the first character of str. For instance, the statement
txtBox.Text = Chr( 65 ) displays the letter A in the text box, and the statement lstBox.Items.Add( Asc( Apple ) ) displays the number 65 in the list box.

Relational Operators
Mathematical Notation
= < >

Visual Basic Notation


= <> < > <= >=

Numeric Meaning
equal to unequal to less than greater than less than or equal to greater than or equal to

Condition
A condition is an expression involving relational and/or logical operators. Result of the condition is True or False.

Chapter 4 - VB 2008 by Schneider

Example
When a = 3, b = 4 (a + b) < 2 * a
2*3=6

3+4=7

7 is NOT less than 6 and the value of the expression is False

Chapter 4 - VB 2008 by Schneider

Another Example
a = 4 b = 3 c = "hello" d = "bye" ( c.Length b ) = ( a / 2 )

53=2

4/2=2

True because 2 equals 2

Chapter 4 - VB 2008 by Schneider

10

Relational Operator Notes


Relational operators are binary they require an operand on both sides of the operator Value of a relational expression will always be True or False Expressions are evaluated from left to right with no order of operations
Chapter 4 - VB 2008 by Schneider 11

Relational Operators
Examples
cat < dog cart < car 9W < bat Dog < cat cat < catalog 2<5 -5 < -2

Relational Operators
Determine whether each of the following conditions is true or false.
1 <= 1 1<1 car < cat Dog < dog

Suppose the numeric variables a and b have values 4 and 3, and the string variables c and d have values hello and bye. Are the following conditions true or false?
(a + b) < 2 * a (c.Length b) = ( a / 2)

Logical Operators
And
cond1 And cond2 cond2 are true true if both cond1 and

Or
cond1 Or cond2 true if either cond1 and cond2 (or both) is true

Not
Not cond1 true if cond1 is false

Examples (Logical Operators)


Suppose the numeric variable n has value 4 and the string variable answ has value Y. Determine whether each of the following conditions is true or false. (2 < n) Or (n = 6) (2 < n) And (n < 6) Not (n < 6) (answ = Y) Or (answ = y) (answ = Y) And (answ = y) ((n = 2) And (n = 7)) Or (answ = Y) (n = 2) And ((n = 7) Or (answ = Y)) ((2 < n) And (n = 5 + 1)) Or (answ = No)

Boolean Expression
An expression that evaluates to either True or False is said to have Boolean data type. Example: The statement txtBox.Text = CStr((2+3)<6) displays True in the text box.

Chapter 4 - VB 2008 by Schneider

16

Boolean Variable
A variable declared with a statement of the form Dim var As Boolean is said to have Boolean data type. It can assume just the two values True and False. Example: Dim boolVar As Boolean boolVar = 2 < 6 txtBox.Text = CStr(boolVar) displays True in the text box.
Chapter 4 - VB 2008 by Schneider 17

Syntax Error
The following is NOT a valid way to test if n falls between 2 and 5: (2 < n < 5 )

Chapter 4 - VB 2008 by Schneider

18

Correction to Syntax Error


To test if n falls between 2 and 5 use: (2 < n ) And ( n < 5 ) A complete relational expression must be on either side of the logical operators And and Or.
Chapter 4 - VB 2008 by Schneider 19

Common Error in Boolean Expressions


A common error is to replace the condition Not ( 2 < 3 ) with the condition (2>3) The correct replacement is ( 2 >= 3 ) because >= is the opposite of <, just as <= is the opposite of >

Chapter 4 - VB 2008 by Schneider

20

4.2 If Blocks
If Block ElseIf Clauses

Chapter 4 - VB 2008 by Schneider

21

If Block
The program will take a course of action based on whether a condition is true. If condition Then action1 Else action2 End If

Will be executed if condition is true

Will be executed if condition is false

Chapter 4 - VB 2008 by Schneider

22

Another example If block


If condition Then action1 End If Statement2 Statement3
Regardless of whether the condition in the If statement is true or alse, these statements will be executed

Chapter 4 - VB 2008 by Schneider

23

Decision on a Flowchart
Pseudecode and flowchart for an If block

No

Is condition true?

Yes

If condition Then action1 Else action2 End If

action 1

action 2

Example 1: Form

txtFirstNum txtSecondNum txtResult

Chapter 4 - VB 2008 by Schneider

25

Example 1: Code
Private Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2, largerNum As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If num1 > num2 Then largerNum = num1 Else largerNum = num2 End If txtResult.Text = "The larger number is " & largerNum End Sub
Chapter 4 - VB 2008 by Schneider 26

Example 1: Output

Chapter 4 - VB 2008 by Schneider

27

Example 2: Form

Chapter 4 - VB 2008 by Schneider

28

Example 2: Partial Code


If costs = revenue Then txtResult.Text = "Break even" Else If costs < revenue Then profit = revenue - costs txtResult.Text = "Profit is " & _ FormatCurrency(profit) & "." Else loss = costs - revenue txtResult.Text = "Loss is " & _ FormatCurrency(loss) & "." End If End If
Chapter 4 - VB 2008 by Schneider 29

Example 2: Output

Chapter 4 - VB 2008 by Schneider

30

Example 3: Form

Example 3: Partial Code


Private Sub btnShow_Click(...) Handles btnShow.Click If CDbl(Text1.Text) >= 3.5 Then lstbox.Items.Add(High Honour) Else lstbox.Items.Add (Under 3.5 GPA) End If End Sub

Example 3: Output

ElseIf clause
If condition1 Then action1 ElseIf condition2 Then action2 ElseIf condition3 Then action3 Else action4 End If
Chapter 4 - VB 2008 by Schneider 34

Example 4: Form

txtFirstNum txtSecondNum txtResult

Chapter 4 - VB 2008 by Schneider

35

Example 4: Code
Private Sub btnFindLarger_Click(...) _ Handles btnFindLarger.Click Dim num1, num2 As Double num1 = CDbl(txtFirstNum.Text) num2 = CDbl(txtSecondNum.Text) If (num1 > num2) Then txtResult.Text = "Larger number is " & num1 ElseIf (num2 > num1) Then txtResult.Text = "Larger number is " & num2 Else txtResult.Text = "The two are equal." End If End Sub
Chapter 4 - VB 2008 by Schneider 36

Multiple Conditions
If condition1 Then action1 ElseIf condition2 Then action2 ElseIf condition3 Then action3 Else action4 End If

Example Program-V
Private Sub btnShow_Click(...) Handles btnShow.Click Dim mid1, mid2, final, point As Double mid1 = CDbl(Text1.Text) mid2 = CDbl(Text2.Text) final = CDbl(Text3.Text) point = mid1 * 0.25 + mid2 * 0.25 + final * 0.5 If point >= 93 Then ListBox1.Items.Add("A) ElseIf point >= 85 And point < 93 Then ListBox1.Items.Add(B) ElseIf point >= 70 And point < 85 Then ListBox1.Items.Add(C) ElseIf point >= 55 And point < 70 Then ListBox1.Items.Add(D) ElseIf point < 55 Then ListBox1.Items.Add(F) End If End Sub

Example Program-V

SELECT CASE Blocks


A Select Case block is an efficient decision-making structure that simplifies choosing among several actions.
It avoids complex If structures. Select Case choices are determined by the value of an expression called a selector.

SELECT CASE Properties


Select Case selector Case Else (and its Case valueList1 action) is optional! action1 The selector can be Case valueList2 a literal a variable action2 an expression . an inequality sign preceded by Is and followed by a constant, . variable, or expression Case Else a range expressed in the form a To b where a and b are action last constants, variables, or expressions End Select

SELECT CASE Properties


Different items appearing in the same list must be separated by commas. Each action consists of one or more statements.

Evaluate selector

Algorithm
In value list 1? YES Action 1 In value list 2? YES Action 2

Pseudocode is same as IF !!

In value list n?

YES

Action n

Perform the last action

Example Program-V
Private Sub btnShow_Click(...) Handles btnShow.Click Dim num As Double num = CDbl(Text1.Text) Select Case num Case Is < 0 ListBox1.Items.Add (The number is negative) Case 0 ListBox1.Items.Add (The number is zero ) Case 1, 10 ListBox1.Items.Add (The number is one or ten) Case 2 To 8 ListBox1.Items.Add (The number is between one and nine) Case 3 ^ 2 ListBox1.Items.Add ( The number is nine) Case Else ListBox1.Items.Add ( The number is bigger than ten) End Select End Sub

Example Program-V

Example Program-VI
Private Sub btnShow_Click(...) Handles btnShow.Click Dim shape As Integer 'selector Dim r As Double 'radius Const pi As Double = 3.1416 'pi constant Dim l As Double 'length Dim h As Double 'height Dim w As Double 'width Dim area As Single 'area Dim sh_name As String 'name of the shape shape = CInt(Text1.Text) Select Case shape Case 1 prompt = "Enter radius for circle" Title = "Circle (radius)" r = CDbl(InputBox(prompt, Title)) area = pi * r ^ 2 sh_name = " The area of circle: " Case 2 prompt = "Enter length for parallelogram" Title = "Parallelogram (length)" l = CDbl(InputBox(prompt, Title)) prompt = "Enter height for parallelogram" Title = "Parallelogram (height)" h =CDbl(InputBox(prompt, Title)) area = l * h sh_name = " The area of parallelogram: " Case 3 prompt = "Enter length for kite" Title = "Kite (length)" l = CDbl(InputBox(prompt, Title)) prompt = "Enter width for Kite" Title = "Kite (width)" w = CDbl(InputBox(prompt, Title)) area = (l * w) / 2 sh_name = " The area of kite: " Case Else prompt = "Wrong selection" Title = "Warning" MsgBox prompt, , Title End Select ListBox1.Items.Add( sh_name & area) End Sub

Example Program-VI

You might also like