Programming continued - PowerPoint PPT Presentation

1 / 34
About This Presentation
Title:

Programming continued

Description:

btnOne' is the actual object this deals with. Click' is the event it handles for that object ... 'car' 'cat' 'Dog' 'dog' Logical Operations. Conditions can ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 35
Provided by: mil94
Category:

less

Transcript and Presenter's Notes

Title: Programming continued


1
Programming (continued)
  • Chapters 3.4, 5.1, and 5.2
  • http//www.cs.pitt.edu/bmills/cs4/

2
Administration
  • Mailing List
  • Programming Assignment 1
  • Will be posted today/tomorrow
  • We will discuss on Wednesday
  • First part due Monday
  • Quiz Today

3
Brief Review
  • Math Operations (,-,/,,mod,math.sqrt)
  • Defining Variables
  • Dim x as String
  • Assigning Variables
  • Dim x as String bryan
  • String Functions
  • Concatenation -
  • fan.Length 3
  • fan.toUpper FAN
  • fanatic.IndexOf(a) 1
  • Visual.Substring(3,2) ua

4
Event Procedures
  • Private Sub btnOne_Click() Handles btnOne.Click
  • First, some terminology
  • procedure, subroutine, function, method,
    subprogram
  • Portion of larger program designed for a task
    that is relatively independent of the larger
    program
  • Some languages make distinctions
  • Technically, VB does, but well worry about this
    later
  • The Sub above actually stands for Subroutine
  • btnOne is the actual object this deals with
  • Click is the event it handles for that object

5
Blocks of Code
  • Code is divided into blocks of code in Visual
    basic
  • Private Sub foo(x)
  • If x gt 100 Then
  • A Block Here
  • End If
  • End Sub

6
Types of Errors
  • Syntax
  • Runtime (trying to do math on strings)
  • Logical (integer division)

7
Types of Errors
  • Syntax
  • These errors are caused when something is
    misspelled, punctuation is missing, etc.
  • Dim x as Ineger
  • x5 12
  • My foo blat fe fi
  • x x 5
  • jnk_box.Text - Text box is really junk_box

8
Types of Errors
  • Runtime
  • Not picked up by VB until execution of the
    program
  • User entered strings and you tried to multiple
    them
  • User enters the wrong file name

9
Types of Errors
  • Logical
  • Not recognized by VB as wrong.
  • No visible error produced by VB or your program
  • These are the hardest to debug
  • avg first_num second_num / 2

10
ANSI Characters
  • About 47 keys on your keyboard
  • Each key has two possible characters
  • At least 94 different characters
  • Each character is represented by a number
  • Chr(n) - gives you character for n
  • Asc(c) - gives you number n for c
  • See Page 605 - Appendix A
  • Also referred to as ASCII

11
ANSI Examples
  • txtBox.Text Chr(65)
  • This will display A in the txtBox

Why do I care? I could just do txtBox.Text A
12
ANSI Example
  • Special Characters
  • txtBox.Text 32 Chr(176) Fahrenheit
  • This will display
  • 32 Fahrenheit

13
Logic Conditions
  • A condition is a expression that is either true
    or false
  • Used in various VB statements (if, while, for,
    etc)
  • Conditions contain
  • Logical Operators
  • Relational Operators
  • Combination of both

14
Relational Operators
  • Equal
  • Not Equal
  • ltgt
  • Less Than
  • lt
  • Greater Than
  • gt
  • Less Than or Equal
  • lt
  • Greater Than or Equal
  • gt

15
Example
  • 1lt1
  • 1lt1
  • car lt cat
  • Dog lt dog

16
Logical Operations
  • Conditions can involve logical operations
  • And
  • Or
  • Not
  • (n lt 5) And (n gt 10)
  • (n gt 5) Or (n lt 10)

17
Logical Operations
  • Not - Flips a true to false and false to true
  • 5 lt 4 is False
  • Not 5 lt 4 evaluates to True
  • And - Results in True if both are True, but False
    otherwise
  • (5 lt 4) And (5 5) is False
  • (4 lt 5) And (5 5) is True

18
Logical Operations
  • Or - Results in False only if both are False,
    True otherwise
  • (5 lt 4) Or (5 5) is True
  • (4 lt 5) Or (5 ltgt 5) is True
  • (5 lt 4) Or (5 ltgt 5) is False

19
Logical Operations
  • Evaluated left to right
  • Not takes precedence over And and Or
  • And takes precedence over Or
  • And and Or take something on both sides
  • Not takes only a single thing
  • Not legal xltyltz
  • Legal xlty And yltz

20
More Examples
  • Let n 4
  • n lt 6 And Not (n 4)
  • (n gt 100) Or (n lt 90)
  • Let answ Y
  • answ y
  • Not (answ Y)
  • (n lt 6) And (answ Y)

21
Boolean Data Type
  • Boolean is another variable type
  • Used to store outcomes for conditions, so only
    stores true or false
  • Dim b as Boolean
  • b x lt y

22
Types of Operations
  • Arithmetic operations first
  • Relational operations next
  • Logical operations last

23
Logical Operations
  • Not (x 5)
  • is the same as
  • x ltgt 5
  • x lt 5 is not the opposite of x gt 5 - Why?
  • Is xlt 5 the opposite of x gt 5?

24
Decisions
  • So far we have just told the computer what to do
    in the order we want it to do it
  • BUT what if we want it to do something if some
    condition is meet?
  • If and Select Case statements will be used

25
If Blocks
  • If condition_1 Then
  • block_1
  • ElseIf condition_2
  • block_2
  • Else
  • block_3
  • End If

26
If Blocks
  • If condition_1 Then
  • block_1
  • ElseIf condition_2
  • block_2
  • Else
  • block_3
  • End If

executed ONLY if condition_1 is true
27
If Blocks
  • If condition_1 Then
  • block_1
  • ElseIf condition_2
  • block_2
  • Else
  • block_3
  • End If

executed ONLY if condition_1 is false AND
condition_2 is true
28
If Blocks
  • If condition_1 Then
  • block_1
  • ElseIf condition_2
  • block_2
  • Else
  • block_3
  • End If

executed ONLY if condition_1 is false AND
condition_2 is false
29
If Blocks
  • If condition_1 Then
  • block_1
  • ElseIf condition_2
  • block_2
  • Else
  • block_3
  • End If

Any Code After End If will be executed regardless
of the conditions
More Code
30
If Blocks
  • ElseIf is optional, this is totally fine
  • If condition_1 Then
  • block_1
  • Else
  • block_3
  • End If

31
If Blocks
  • Else is also optional, this is totally fine
  • If condition_1 Then
  • block_1
  • ElseIf condition_2
  • block_2
  • End If
  • Or
  • If condition_1 Then
  • block_1
  • End If

32
Nested If Blocks
  • Nesting refers to having one block inside another
    block
  • If cond1 Then
  • If cond2 Then
  • block1
  • End If
  • End If

33
Nested If Blocks
  • These can be confusing
  • A better way of writing the previous example is
  • If cond1 And cond2 Then
  • block1
  • End If

34
Code Example
  • Profit/Loss
  • Accept input of Costs and Revenue and have a
    button to calculate the Profit or Loss. Display
    one or the other
  • Loss is xxxx.xx
  • Or
  • Profit is xxxx.xx
Write a Comment
User Comments (0)
About PowerShow.com