CO1403 Visual Basic Programming - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

CO1403 Visual Basic Programming

Description:

VB also gives us the ability to make decisions within our program code ... (It is untidy to attempt to fit too much on a signle line) Decisions ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 23
Provided by: ucla9
Category:

less

Transcript and Presenter's Notes

Title: CO1403 Visual Basic Programming


1
CO1403 Visual Basic Programming
  • Week 6

2
Introduction
  • This week we will look at,
  • Conditional IF statements
  • LO3, Syllabus Item 3 (See Matrix)

3
Decisions
  • It is a really useful tool to be able to make
    decisions
  • As humans, we do it every single day
  • VB also gives us the ability to make decisions
    within our program code
  • For example, you may only want to display the
    message Congratulations if someone has entered
    the correct answer to one of your questions.

4
Decisions
  • To make decisions within our programs, we have to
    make use of an IF statement.
  • An IF statement only executes some lines of code
    if and only if a condition is satisfied.
  • IF condition THEN statements ELSE statements

5
Decisions
  • IF condition THEN statement ELSE statements
  • Is made up of,
  • IF is a VB keyword which tells VB that we are
    making some kind of decision
  • condition is a boolean expression that must
    evaluate to true or false
  • THEN is a VB keyword
  • statement is a line of code that you can
    potentially write these are executed if the
    condition evaluates to true
  • ELSE statement is a line of code that you can
    write that is executed if the condition evaluates
    to false

6
Decisions
  • Consider pseudo-code forms of this IF statement.
  • IF John is giving a VB Lecture THEN Students
    are supposed to be in LH14 ELSE Students could
    be anywhere
  • IF You understand what John is talking about
    THEN You can write IF statements ELSE You need
    to listen more.

7
Decisions
  • The previous example of an IF statement works
    only when there is ONE line of code after the
    THEN and after the ELSE parts of the IF
    statement.
  • There is also a form of IF statement that allows
    you to execute more than one line of code
  • IF condition THEN
  • statements
  • ELSE
  • statements
  • END IF

8
Decisions
  • This form of IF statement can word EXACTLY the
    same as the previous example (i.e. with only one
    line of code)
  • But also gives us the advantage that we can have
    more than one line of code
  • But it also makes your programming code more
    readable, and I would prefer it if you always
    used this style of IF statement
  • (It is untidy to attempt to fit too much on a
    signle line)

9
Decisions
  • We may also want to have more than one condition
    that we want to test.
  • For example, how could we test to see if a
    username was John or Chris and to output
    different results based on the username?
  • This introduces us to the ELSEIF statement.

10
Decisions
  • IF condition THEN
  • statements
  • ELSEIF condition THEN
  • statements
  • ELSE
  • statements
  • END IF

11
Decisions
  • Using the ELSEIF keyword allows us to add more
    than one condition to test for, and makes the IF
    statement very flexible.

12
Decisions
  • Show VB examples on OHP

13
What makes a condition?
  • As far as programming is concerned, a condition
    is something that evaluates to either true or
    false.
  • In order to be able to decide something, a
    computer must have either a definite yes or a
    definite no answer.
  • We use booleans to help us define our conditions.
    True / False is the same as Yes or No.
  • When writing a condition in VB, you must write it
    in a way that has a yes or no answer (true or
    false)

14
What makes a condition?
  • Sometimes, it is useful to be able to combine
    more than one condition. For example,
  • IF The username entered was John OR Chris THEN
    Output to screen you are course leaders ELSE
    Output to screen that you are not course
    leaders.
  • To achieve this we must look at some boolean
    operators.

15
Boolean operators
  • Boolean operators work on True/False values.
    There are a limited number of boolean operators
  • All Boolean operators make sense, and you use
    them in the real world (probably without
    realising it)

16
Boolean Operators
  • AND is a boolean operator. We use it everyday
    in life If the sandwich has cheese AND ham,
    then it is a cheese and ham sandwich.
  • AND works on two boolean conditions, in the case
    of the example above, the two boolean expressions
    are
  • 1.The sandwich has cheese
  • 2.The sandwich has ham
  • If they both evaluate to true, then we definitely
    have a cheese and ham sandwich
  • Both conditions need to be true for the whole
    condition to evaluate to true

17
Boolean Operators
  • OR is a boolean operator. We use it every day
    in real life. For example, If it is 9am OR 10am
    on a Monday morning, THEN I have John in CAT
    building
  • OR works on two boolean conditions,
  • Is it 9AM?
  • OR is it 10AM?
  • If either condition is true, then you should be
    in the CAT building.
  • Only one condition needs to be true for the whole
    condition to be true

18
Boolean Operators
  • NOT is a boolean operator. We also use this
    everyday in life. For example, John is the best
    lecturer aboutNOT!
  • The NOT operator works on ONE condition only, and
    totally negates the result.
  • If a condition evaluates to true, using the NOT
    operator will make the whole condition evaluate
    to false
  • If a condition evaluates to false, using the NOT
    operator will make the whole condition evaluate
    to true

19
Boolean Operators
  • You can use a boolean operator in your VB code
    easily,
  • IF aCondition OR anotherCondition THEN
  • statement
  • END IF
  • IF NOT aCondition THEN
  • statement
  • END IF

20
Boolean Operators
  • VB evaluates operators from left to right. This
    may cause potential confusion with expected
    results. For example,
  • IF John AND Chris OR Kate are present
  • Which is it? What happens if John is present,
    but not Chris or Kate?
  • To avoid confusion such as this, VB implements an
    operator precedence
  • This means that some operators will be evaluated
    before others

21
Boolean Operators
  • Order of operator evaluation
  • 1.() Parenthesis
  • 2.NOT
  • 3.AND
  • 4.OR
  • 5.XOR
  • 6. (Equivalence)
  • Which means an AND expression will evaluate to
    true before an OR expression will evaluate.

22
Boolean Operators
  • You dont have to, but I always recommend you
    make full use of parenthesis
  • This is the only way that you are 100 certain of
    how things are being evaluated
  • I.e.
  • IF (John AND Chris) OR Kate
  • IF John AND (Chris OR Kate)
  • You can be 100 certain of the outcomeit is
    predictable!!!
Write a Comment
User Comments (0)
About PowerShow.com