Revision Expressions & constructs Lesson...
Transcript of Revision Expressions & constructs Lesson...
Revision Expressions & constructs
Lesson 1
1. Selection/Conditional
2. Repetition
3. Linear
Name the common repetition constructs
1. Do While . . . Loop
2. Do . . . Until Loop
3. For . . . Next
Loop continues while condition is true
Do While RandomNum > 10 RandomNum = InputBox.Show(“Input random number”) Loop
Do RandomNum = InputBox.Show(“Input random number”) Loop While RandomNum > 10
Since condition is at end of loop, the loop body executes at least once.
Loop continues until condition is true
Do until RandomNum = 10 RandomNum = InputBox.Show(“Input random number”) Loop
Do RandomNum = InputBox.Show(“Input random number”) Loop until RandomNum = 10
Since condition is at end of loop, the loop body executes at least once.
Loop continues for specified number of repetitions
For x = 1 to 10 NewName = InputBox(“Enter name”) lstNames.Items.Add(NewName) Next x
if
Select . . . Case
One or more lines of code are executed depending upon a condition
IF x > 70 Then MsgBox(“Distinction”) ElseIF x > 60 Then MsgBox(“Merit”) ElseIF x > 50 Then MsgBox(“Pass”) Else MsgBox(“Fail”) End IF
The Else and Else IF sections is optional
Select . . Case construct allows a large number of results
Select Case Month Case 1 MsgBox(“Jan”) Case 2 MsgBox(“Feb”) Case 3 MsgBox(“Mar”) Case 4 MsgBox(“Apr”) Case 5 MsgBox(“May”) End Select
Mathematical equations
= 2 + 4/2 + 1
5
= 2 + 4 * 2
10 (not 12)
Precedence indicates when operators will be evaluated in complex expressions.
Operators with high precedence are evaluated before operators with low precedence.
For example, the multiplication operator (*) has higher preference than the addition operator (+), so the expression:
2+3*4
equals 14, not 20.
Order Operator Explanation
1 ^ Exponential
2 * Multiplication
3 / Integer division
4 \ Division
5 MOD Returns remainder of division
6 + Addition
7 - Subtraction
8 & or (+)
Concatenation
= 7 + 4*2 + 3
= 7 + 8 + 3
= 18
1st
= 16 / 4 * 2
= 16 / 8
= 2
Lets try some more
= 2 + 4 / 2
4
= 10-10*2-2
= 10-20-2
-12
=3+4+2—1
10
= 2 + 18 * 2
38
= 80 / 2 * 2
20
=4*4/2*2/4
1
=10/20+1*0.5
1
^ operator
This is an exponential mathematical operator
22 = 2 ^ 2 = 2 * 2 = 4
23 = 2 ^ 3 = 2 * 2 * 2 = 8
42 = 4 ^ 2 = 4 * 4 = 16
= 4 + 2 ^ 2 – 1
7
= 2 ^ 3 + 2
10 = (2*2*2+2)
x = 80 / 2 ^ 2
20
= 80/2*2/2^2
5
= 4*4/2*2*2*4
= 4^2/2^3*4
= 16 / 8 * 4
= 0.5
Parenthesis can help read in assembling an expression.
Parenthesis also make the expression easier to read.
Types of brackets include: ◦ round brackets ( )
◦ square brackets [ ]
◦ curly brackets - braces { }
◦ angle brackets 〈〉
2
yxt
t = ( + y) / 2
22
rt
t= 2 * ( r / 2 )
23
y
w
rt
t = (r / (3+w)) * (y / 2)
32
3py
t
t = ((y/2)^3) * (p/3)
242
h
p
yxt
t = + ((y / (2*p)) / h) * (4 ^ 2)
hey
tr
4
3
2
2
t = (((2*y)^(3*r)) / (2^4)) * (e-h)
er
eeeyxt
2
2
t = *((2+y) / 2)+((e^3/(r+e))
162
yxt
2
1
22 Hint:
t = +(y/2) – (16 ^ (1/2))
Constructs
Iteration
Expressions
Revision: Programming tasks