Title | Lecture 3 - PROGRAMMING IN VISUAL BASIC – DECISIONS/CONDITIONAL STATEMENTS ( PART 2 ) |
---|---|
Author | Jinal Somani |
Course | Programming for Information Systems (Sc) |
Institution | Trent University |
Pages | 3 |
File Size | 246.6 KB |
File Type | |
Total Downloads | 96 |
Total Views | 126 |
PROGRAMMING IN VISUAL BASIC –
DECISIONS/CONDITIONAL STATEMENTS ( PART 2 )...
No its not the same (in ASCII code, they are different.)
PROGRAMMING IN VISUAL BASIC – DECISIONS/CONDITIONAL STATEMENTS ( PART 2 ) SC 113 - next week Thursday after workshop and next day morning until noon (present programming)
Making decisions
Making decisions
It is most important to test the validity of the data being entered eg. You cannot be “25 years old” Sometimes, we need to make decisions based on the value entered Eg if your gold score for 18 holes is greater than 140, golf is probably not a career path Decisions are based on evaluating some criteria and deciding if the condition has been reached or not. In computer science, where criteria are met (TRUE) or not met (FALSE) are called Boolean expressions (all about logic). In VB, there are specific ways you can compare variables and values Comparisons are made using relational operators < less than greater than >= greater than or equal to = equal to not equal to Eg txtNameAGe = “”, exists – mean they haven’t entered a value
ASCII/ANSI values are used to decide order for strings
You can create more complex statements by combining the Boolean values of multiple simple Boolean expressions. These expressions are combines using relational operators. Relations operators are binary operators and require values on both the right and lef You can combine them using: o AND - only TRUE if both expressions are true If age is > 40 AND not retired o OR – TRUE if either value is true Older than 40 OR retired o NOT – reverses the Boolean value of the expression that follows it (not a binary operator) If I’m NOT finished, proceed to next statement
Examples: (age=8) AND (WeirdCharacter = “Yes”)
Booleans
^ Both statements to be true
Examples of statements which can be evaluated as either TRUE or FALSE
In VB:
(3+4) > (137/2) - FALSE 5 < 6 – TRUE txtUsername.Length > 25 txtStudentNumber.txt = 792019 by default, this is a character string “Hello World” = “hello world”
Dim Finished As Boolean Finished = FALSE if (NOT Finished)
IF clause
VB has a specific syntax that you must use when comparing values – known as a simple if… then statement. Line of code between the IF and End IF will be executed if the value of the Boolean expression is TRUE
The second “Cint” in the code above is not needed Characters strings and numbers are different Extra brackets to be used in the Boolean expression – any statement that you want a specific answer from should have round brackets around them.
Select Case In some examples, you will find that using if..else if can get messy – many layers can lead to code that is very hard to read. If, however, we are only testing for conditions relating to a single variable, VB allows us to use “Select Case” structure for our comparisons The syntax of the Select Case statement is as follows:
Else clause
If you want to execute code when the Boolean expression is evaluated as FALSE, then you can add an Else clause to your If… Then… End if
If.. then..Else… End if
The syntax of the If statement is as follows; Example 1
Select Case – example 2...