Download presentation
Presentation is loading. Please wait.
Published byCatherine Harrington Modified over 9 years ago
1
1 Flow Control Ifs, loops
2
2 Data Type At the lowest level, all data in a computer is written in 1’s and 0’s (binary) How the data gets interpreted, what actions can be performed on it, etc. depends in part on the “data type” associated with it We let the computer know how we mean to interpret a variable by declaring its type
3
3 Number misinterpreted as string
4
4 Option Explicit Private Sub cmdAdd_Click() txtSum.Text = txtNumber1.Text + _ txtNumber2.Text End Sub A underscore with a space in front means the statement is continued on the next line
5
5 Declaring variables to fix
6
6 Option Explicit Dim Number1 As Integer Dim Number2 As Integer Private Sub cmdAdd_Click() Number1 = txtNumber1.Text Number2 = txtNumber2.Text txtSum.Text = Number1 + Number2 End Sub
7
7 Data types integer: “a whole number,” a number that has no fractional part (e.g. –57, 0 or 255) Single: A number that can have a fractional part or a “decimal point” (e.g. 3.14159). A.k.a. floating-point number. Double: same as a single but uses more bits to represent the number, thus doubles cover a wider range of numbers and are more precise (e.g. 3.14159265358)
8
8 More Data Types String: text, words Boolean: a variable that can only be true or false Currency: Data type use to represent money; it automatically takes care of certain problems (like unusual rounding) associated with money
9
9 Algorithm A collection of steps (instructions, “statements”) that Are well-defined: each instruction, as well as the order of instructions, should be unambiguous Terminate: the instructions cannot go on forever Accomplish some goal
10
10 Algorithm Derived from the name of the mathematician, Mohammed ibn-Musa Al- Khowarizmi, who was part of the royal court in Baghdad and who lived from about 780 to 850
11
11 Statements Computer algorithms consist of a sequence of instructions or statements. A statement tells the computer to perform a specific action. Statements generally consist of operators and operands
12
12 Operators and Operands The operator is the action The operand is the data acted upon 5 + x operands operator
13
13 Assignment A variable corresponds to a memory location, an assignment statement like x=5 takes the value from the right- hand-side and places it in the memory location represented by the left- hand-side x = 5 Assignment operator
14
14 Branching The steps in an algorithm may be different for different cases or under different conditions For example, the formula for calculating weekly salary is different if the employee worked overtime or the calculation of one’s taxes differs depending on one’s tax bracket
15
15 Control flow In programming, control flow are the structures that determine which operations are executed and in what order. Branching statements (e.g. if…then) make up part of a programming language's flow control mechanism. Looping statements (e.g. for…next and while) are known as flow control statements.
16
16 If/Then structure Tests a condition and executes statements within the body (block) of the if structure provided the condition was true and skips over those steps otherwise
17
17 If-then example If State=“PA” Then Price=Price*(1.06) End If ‘State ‘Recalculates the price factoring in the PA ‘sales tax if the state is PA ‘Note strings are more easily compared in VB ‘Deitel, Deitel and Nieto, p.93
18
18 Condition A condition is an expression that evaluates to a Boolean value (true or false) It asks a question, the answer of which is either true or false It often involves a comparison A > B (is A greater than B?) A >= B (is A greater than or equal to B?) A < B (is A less than B?) A <= B (is A less than or equal to B?) A=B (is A equal to B?) A <> B (is A not equal to B?) ‘(does not use !)
19
19 If-Then-Else The If-Then-Else structure provides two statement blocks, one to execute if the condition is true, the other to execute if the condition is false
20
20 If-then-else example If State=“PA” Then If City = “Phila” Then Price=Price*(1.07) Else Price=Price*(1.06) End If ‘City End If ‘State ‘This is also an example of nested if’s (an if within an if)
21
21 Elseif If Grade > 94 Then Letter_Grade = “A” ElseIf Grade > 86 Then Letter_Grade = “B” ElseIf Grade > 78 Then Letter_Grade = “C” ElseIf Grade > 70 Then Letter_Grade=“D” Else Letter_Grade=“F” EndIf
22
22 Other conditions Other examples of conditions include Does a string correspond to a number? Has the end of a file been reached? Does a cell in Excel contain any data? Does a queue have any values in it? There are a number of methods in in VB that ask these questions for you. They generally begin with Is, such as IsNumeric
23
23 Ensuring data is numeric
24
24 Ensuring data is numeric
25
25 Ensuring data is numeric Private Sub txtNumber1_Validate(Cancel As Boolean) If Not IsNumeric(txtNumber1.Text) Then MsgBox ("You must enter a number.") txtNumber1.Text = "" Cancel = True Else Number1 = txtNumber1.Text End If End Sub The validate method is triggered when one is leaving a control (if one hits tab or clicks on another control) Brings up little window with message Boolean operator
26
26 More complicated conditions In the same way an arithmetic operators can act on two numbers and produce a third number 2 + 5 7 A Boolean operator can act on two conditions (Boolean expressions) and produce a third Boolean expression (a c)
27
27 Boole The expression (Booleans) and the rules for combining them (Boolean algebra) are named after George Boole (1815-64), an British mathematician
28
28 Boolean operators AND: when two or more Boolean expressions are ANDed, both must be true for the combination to be true OR: when two or more Boolean expressions are ORed, if either one or the other or both are true, then the combination is true NOT: takes one Boolean expression and yields the opposite of it true false and vice versa
29
29 Compound logic example If Grade > 94 Then Letter_Grade = “A” EndIf If Grade 86 Then Letter_Grade = “B” End If If Grade 78 Then Letter_Grade = “C” End If ‘etc. (Deitel, Deitel and Nieto, p. 156)
30
30 Boolean searching In Boolean searching, an "and" operator between two words (for example, "pear AND apple") means one is searching for documents containing both of the words or values, not just one of them. An "or" operator between two words or other values (for example, "pear OR apple") means one is searching for documents containing either of the words. http://library.albany.edu/internet/Boolean. html
31
31 Bit level At the lowest level, everything in a computer is a bit (a 1 or 0) which can be interpreted as true or false. So at this level all operations are Boolean operations
32
32 Loops With an If, a condition determines whether or not a block of statements is executed, but they are executed at most once In a loop, a condition determines whether or not a block of statements are executed then the condition is retested and the statements possibly re-executed Deitel, Deitel and Nieto, p. 99
33
33 loop
34
34 Do While Loop Example Private Sub cmdCalculate_Click() Dim Principle As Currency Dim Interest As Double Dim Monthly_Payment As Currency Dim Month_Count As Integer Principle = CDbl(txtPrinciple.Text) Interest = CDbl(txtInterest.Text) Monthly_Payment = CDbl(txtMonthlyPayment.Text) Month_Count = 0
35
35 Do While Loop Example Do While (Principle > 0) Principle = Principle * (1 + Interest / _ 100 / 12) - Monthly_Payment Month_Count = Month_Count + 1 Loop lblResult.Caption = lblResult.Caption & _ Month_Count End Sub
36
36 Infinite loop An infinite loop occurs when the programmer assumed that some condition would eventually terminate the loop and this condition failed to happen. The result is that the loop repeats continually until the operating system or the user stops it. In VB if you haven’t saved and you get into an infinite loop you will probably lose your work
37
37 Getting out of a loop
38
38 Getting out of a loop
39
39 Getting out of a loop Do While (Principle > 0) Principle = Principle * (1 + Interest / _ 100 / 12) - Monthly_Payment If Principle > CDbl(txtPrinciple.Text) Then MsgBox ("You'll never pay off the loan _ at that rate.") Exit Do End If Month_Count = Month_Count + 1 Loop Gets one out of the loop
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.