1 DATABASE TECHNOLOGIES BUS3500 - Abdou Illia, Fall 2012 (September 5, 2012)
Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003...
-
Upload
reginald-lloyd -
Category
Documents
-
view
214 -
download
0
Transcript of Review for Final (Part 2) School of Business Eastern Illinois University © Abdou Illia, Spring 2003...
Review for Final (Part 2)
School of BusinessEastern Illinois University
© Abdou Illia, Spring 2003
(Week 15, Friday 5/2/2003)
- IF Blocks- Evaluating conditions
3IF Statements
Allow a program to decide on a course of action– Based on whether a certain condition is true or false
If condition Then Statement(s)Else Other Statement(s)End If
Syntax
Condition is an expression that is evaluated to True or False
If condition is true, Statement(s) are executed. If condition is false, Other Statement(s) are executed.
If Credits > 10 Then Tuition = 1000Else Tuition = 100 * CreditsEnd If
Example
4Conditions
Expressions involving:– Relational operators (<, >, >=, =, ….)– Logical operators (And, Or, Not)– Arithmetic operations
If (letterGrade = “A”) Or (letterGrade = “B”) Then picOutput.Print “Good Work”Else picOutput.Print “Grade is C or D”End If
Example
5Relational operators
Can be applied to:– numbers– strings
MathematicalNotation
Visual Basic Notation Numeric Meaning
= = Equal to
# <> Unequal to
< < Less than
> > Greater than
<= <= Less than or equal to
>= >= Greater than or equalto
6Relational operators applied to strings
Computer uses a special coding system to compare character strings
String1 is said to be less than String2 if String1 precede String2 alphabetically according to ANSI table (See Appendix A).
“Chase” < “Chaz” True
“ Cat” < “Cat” True (because of the spaces)
“Pay” < “Pay “ True (because of the spaces)
“Jones” <> “James” True
“Hope” < “Hopeful” True
Examples of comparing character strings
7Logical operators
The three Logical Operators are: Not, And, Or
Not: Negates a single expression. Example: Suppose answer = “Y”
Not (answer = “y”) is true
And: Combines two expressions; each expression must be True for the entire expression to be True. Example: Suppose answer = “Y”
(answer = “Y”) And (answer = “y”) is False
Or: Combines two expressions; either expression (or both expressions) must be True for the entire expression to be True. Example: Suppose answer = “Y”
(answer = “Y”) Or (answer = “y”) is True
8VB operators hierarchy
First, Arithmetic operations (^ , *, /, +, -) are carried out– ^ executed first– * and / executed second– + and – executed third– Note: If parentheses are used, expressions in parentheses are
performed first according to those precedence rules.
Second, Expressions involving relational operators (>, <, =, >=, <=, …) are evaluated to True or False
Third, Logical operators are applied in the order Not, And, Or.
In the event of a tie, the leftmost operator is applied.
9Determine whether the following conditions are true or false?
1 <= 1
Answer: True
“car” < “cat”
Answer: True (because alphabetically “car” precedes “cat”)
“Dog” < “dog”
Answer: True (because Uppercase precedes lowercase)
(“Y” < > “X”) And (143.55 < 143.55)
Answer: False (Because 143.55 is not less than 143.55)
10Determine whether the following conditions are true or false?
(0 = 14) Or (6 ^ 2 - 3 <= 4 / 2 + 8)
Answer: False
Not (6 = 7) And ( 44 > 33)
Answer: True
In the next two cases, suppose that n = 4
(2 < n) And (n < 6)
Answer: True
(2 < n) Or (n = 6)
Answer: true
11Determine whether the following conditions are true or false?
In the next cases, assume a = 2 and b = 3
3 * a = 2 * b
Answer: True
(5 – a) * b < 7
Answer: False
b <= 3
Answer: True
a ^ b = b ^ a
Answer: false
12Determine whether the following conditions are true or false?
In the next cases, assume a = 2 and b = 3
a ^ (5 – 2) > 7
Answer: True
(a < b) Or (b < a)
Answer: True
(a * a < b) Or Not (a * a < a)
Answer: True
Not (a < b) Or Not (a < (b + a))
Answer: False
13Determine the output displayed in the picture box when the command button is clicked.
Private Sub cmdDisplay_Click()
Dim a As Single
a = 5
If 3 * a – 4 < 9 Then
picOutput.Print “Remember, ”
End If
picOutput.Print “Tomorrow is another day.”
End Sub
Answer: Tomorrow is another day.
14Determine the output displayed in the picture box when the command button is clicked.
Private Sub cmdDisplay_Click()
Dim Gpa As Single
Gpa = 3.49
If Gpa >= 3.5 Then
picOutput.Print “Honors”;
End If
picOutput.Print “Student”
End Sub
Answer: Student
15Determine the output displayed in the picture box when the command button is clicked.
Private Sub cmdDisplay_Click()
Dim a As Single
a = 5
If (a > 2) And (a = 3 Or a <7) Then
picOutput.Print “Hi”
End If
End Sub
Answer: Hi
Do LoopsSelect Case Blocks
17Do Loops (using While)
Used to repeat a sequence of statements while a condition is True
Do While Condition Statement(s)Loop
Syntax 1
Private Sub cmdDisplay_Click() Dim Num As Integer
' Display the numbers from 1 to 10 Num = 1 Do While Num <= 10 picNumbers.Print Num; Num = Num + 1 LoopEnd Sub
Example
Do Statement(s)Loop While Condition
Syntax 2
Condition involve:– Relational operators (<, >, >=, ….)– Logical operators (And, Or, Not)
Same rules than Condition in IF Blocks (seen before Spring Break)
18Do Loops (using Until)
Used to repeat a sequence of statements until a condition becomes True
Do Statement(s)Loop Until condition
Syntax 4
Private Sub cmdDisplay_Click() Dim Num As Integer
' Display the numbers from 1 to 10 Num = 1 Do Until Num > 10 picNumbers.Print Num; Num = Num + 1 LoopEnd Sub
Example
Do Until conditon Statement(s)Loop
Syntax 3
Note: To get the same result using While and Until, Conditions should be opposites. For instance, While Num <= 10 is the same as Until Num > 10
19Do Loops
Used to display all or selected items from a file– Using EOF function
Private Sub cmdDisplay_Click() Dim Name As String, Phone As String picPhones.Cls
Open "A:\Phone.txt" For Input As #1 Do While Not EOF(1) Input #1, Name, Phone picPhones.Print Name, Phone Loop Close #1End Sub
Example
"Bert", "123-4567""Ernie", "987-6543""Grover", "246-8321""Oscar", "135-7900"
Phone.txt
20Replace each statement containing Until with an equivalent statement containing a While, and vice versa. Loop Until AccountBalance >= 1200
Answer: Loop While AccountBalance < 1200
Do While Amount = 100
Answer: Do Until Amount < > 100
Do Until Name = "Bob"
Answer: Do While Name < > "Bob"
Do While (a > 1) And (a < 3)
Answer: Do Until (a <= 1) Or (a >= 3)
21Simple program using a Do loop
The world population reached 6 billion people in 1999 and was growing at the rate of 1.4 percent each year. Assuming that the population will continue to grow at the same rate, write a program to determine when the population will exceed 10 billion.
Private Sub cmdCalculate_Click( ) Dim Year As Integer, Population As Single Year = 1999 Population = 6000000000 Do Until Population > 10000000000 Population = Population + (0.014 * Population) Year = Year + 1 Loop
picOutput.Print "The answer is: "; YearEnd Sub
22For …Next
Repeats a group of statements a specified number of times
For…Next are used when we know exactly how many times a loop should be executed.
Private Sub cmdDisplay_Click() Dim i As integer picTable.Cls For i = 1 To 5 picTable.Print i; i^2 Next iEnd Sub
Example
For counter = start To end
Statement(s)
Next [counter]
Where:
counter is a numeric variable used as a loop counter
start is the initial value of counter
end is the final value of counter
Private Sub cmdDisplay_Click() Dim i As integer picTable.Cls i = 1 Do While i <= 5 picTable.Print i; i^2 i = i + 1 LoopEnd Sub
Same example using Do loop
23Select Case Blocks
Very efficient decision-making structures:– Simplifies choosing among several actions
Avoid using Nested IF and ElseIf statements
Actions based on the value of an expression called selector
Select Case Selector Case ValueList1 Action1 Case ValueList2 Action2 : Case Else
Action of last resortEnd Select
SyntaxAge = Val(txtAge.Text)Select Case Age Case 5 Category = "5-year old" Case 13 To 19 Category = "Teenager" Case Is > 65 Category = "Senior" Case Else Category = "Everyone else"End Select
Example
24Select Case Statements
Selectors can be:– Numeric or string variables. Example: Select Case FirstName– Expressions. Example: Select Case Left(EmployeeName, 1)
Select Case Selector Case ValueList1 Action1 Case ValueList2 Action2 : Case Else
Action of last resortEnd Select
Syntax
ValueList items must be separated by commas
ValueList can be:– A constant– An expression– An inequality sign preceded by Is and
followed by a constant, a variable, or an expression
– A range expressed in the form a To b.
x = 2 : y = 3Num = Val(txtNumber.Text)Select Case Num Case y – x, x picOutput.Print "Buckle my shoes"End Select
Grade = Val(txtBox.Text)Select Case Grade Case Is >= 90 picOutput.Print "A"End Select
Grade = Val(txtBox.Text)Select Case Grade Case 60 To 89 picOutput.Print "Pass"End Select
25Problem 1: Assigning letter grades(To be done in class)
Write a Click event procedure associated to the command button cmdShow that asks the user to type in an exam score and:
– Assigns a letter grade based on the scale in the table below. The variable to be assigned the letter grade as value should be named Grade
– Displays the score typed by the user and the letter grade in picBox.
Score Letter grade
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
txtScore
cmdShow picBox
26
Problem 1: Assigning letter grades
Private Sub cmdShow_Click() Dim Score As Single Score = Val(txtScore.Text) Select Case Score Case 90 To 100 Grade = "A" Case 80 To 89 Grade = "B" Case 70 To 79 Grade = "C" Case 60 To 69 Grade = "D" Case 0 To 59 Grade = "F" End Select picBox.Print Score; GradeEnd Sub
Score Letter grade
90-100 A
80-89 B
70-79 C
60-69 D
0-59 F
txtScore
cmdShow picBox
27Problem 2: IRS
IRS informants are paid cash rewards based on the value of the money recovered.
Write a Click event procedure associated to the command button cmdReward that asks the user to type in the Amount recovered and:
– Determine the Reward based on the following rule: 10% for up to $75000 recovered, 15% for a recovered amount greater than 75000 but less than $100000; and 18% for more than $100000.
– Displays the calculated reward in picReward. txtAmount
cmdShow
picReward
28Problem 2: IRS
Private Sub cmdShowReward_Click() Dim Amount As Single Amount = Val(txtAmount.Text) Select Case Amount Case Is <= 75000 Reward = 0.1 * Amount Case Is <= 100000 Reward = 0.15 * Amount Case Is > 100000 Reward = 0.18 * Amount End Select picReward.Print "The reward is "; RewardEnd Sub
txtAmount
cmdShow
picReward
- One-dimensional arrays- Two-dimensional arrays- Sequential Files
30One-dimensional arrays
See hard copy received to prepare for exam 3
Copy available at: http://www.eiu.edu/~a_illia/cis2000/notes/AllReview.htm
31Two-dimensional arrays
See hard copy received to prepare for exam 3
Copy available at: http://www.eiu.edu/~a_illia/cis2000/notes/AllReview.htm
32Sequential Files
See hard copy received to prepare for exam 3
Copy available at: http://www.eiu.edu/~a_illia/cis2000/notes/AllReview.htm
List boxCombo box
34List box and Combo box
See Answer to Exercises received in class.
See Quiz 3 (available at http://www.eiu.edu/~a_illia/cis2000/notes/AllReview.htm)