21
11/4/20 16 1 Computer Programming 1 5.03 Apply operators and Boolean expressions 1 Operators Just like Math class, Operators are the ______________________________ for performing math. You have used them since early in life. There are several types of operators. 2 3 Basic Math Operators These you are familiar with: • + (add), •- (subtract) •/ (divide) •* (multiply) •^ (Exponents) New Math operators include: •\ integer divide - returns the ________________________________. •Mod (Modulus division) - returns the ________________________________. 4 Samples of Math with various Operators Addition + intNumber = ________________________ Subtraction – intNumber = ________________________ Multiplication * intNumber = ________________________ Division / intNumber = ________________________ Integer Division \ intNumber = ______________________ Modulus Mod intNumber = ______________________ Exponents ^ intNumber = ________________________ 5 Operators of Operations Just as in math, Please Excuse My Dear Aunt Sally describes the precedence applied to math

5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

  • Upload
    others

  • View
    8

  • Download
    0

Embed Size (px)

Citation preview

Page 1: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

1

Computer Programming 15.03 Apply operators and Boolean expressions

1 Operators• Just like Math class, Operators are the ______________________________ for performing math. You

have used them since early in life. There are several types of operators. 2

3 Basic Math Operators• These you are familiar with:

• + (add),• - (subtract)• / (divide)• * (multiply)• ^ (Exponents)

New Math operators include:• \ integer divide - returns the ________________________________.• Mod (Modulus division) - returns the ________________________________.

4 Samples of Math with various Operators• Addition +

intNumber = ________________________• Subtraction –

intNumber = ________________________• Multiplication *

intNumber = ________________________• Division /

intNumber = ________________________• Integer Division \

intNumber = ______________________• Modulus Mod

intNumber = ______________________• Exponents ^

intNumber = ________________________

5 Operators of Operations• Just as in math, Please Excuse My Dear Aunt Sally describes the precedence applied to math

in programmingo Parantheseso Exponentso Multiplyo Divide (NOTE: Division will be complete in the order it appears in the problem, no ranking with division types.)

o Addo Subtracto

Page 2: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

2

6 Examples Using Familiar Operators

Operator Example dblResult

+ dblResult = 3 + 4 7

- dblResult = 4 – 3 1

* dblResult = 3 * 4 12

*, + dblResult = 3 * 4 + 5 17

*, +, () dblResult = 3 * (4 + 5) 27

^, * dblResult = 2^3 * 6 48

7 Division Operators• Regular division

o ________________________________________if assigned to an integer.o This is the division with which you are most familiar.

• Integer divisiono ________________________________________________________________________ ( \ )

• Modulus divisiono ______________________________________________________________________ (mod)

o

8 Integer division 19 \ 5 = 3•••

Anytime you divide a number into another number the ___________________________________of • the quotient.

9 Modular division 19 Mod 5 = 4

• Modular Division is about ____________________________• Anytime you divide a number into another number ___________________________________ part of

the quotient.

Page 3: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

3

5.04 Applying Decision Making Structures

1 What is a decision?a. Decisions are _________________________hundreds of times a day. b. Computers can make _______________ decisions also.

2 Boolean Logica. Remember, from the previous section Boolean logic? (Ex: intNum = 5)b. Computers use it to make decisions!c. There are only ________________________________________d. Computers do not know about _______________

The If Decision Structure3 Decisions… Decisions

a. So when you got up this morning what did you do?i. This is a decision. Let’s put it in computer terms.

1. ____________________________________________________________b. The If…Then Statement

i. If…Then is a decision structure that executes a set of statements _______________________1. Form:

If condition ThenStatements

End If

ii. IF the sun is out (question) THEN I will walk to school.c. Remember the computer ___________________________________So either the sun is out (true) or

ANY other state (false). ________________________________________. d. When the question is True, the statements after THEN (and down to ENDIF) execute. ___________

___________________if the question is False. e. In Visual Studio we use _________________________statement to tell the computer we want it to

make a decision. i. For example

If a = b then c= 10

Else c=13

End If

4 Use of the ELSE a. Else is ________________- if omitted and the “question” is false ______________________________b. If ELSE is used, the statements after the ELSE will execute if the “question” ____________________

_________________________

5 Nested If…Then…Else Statements a. If…Then…Else statement within an If…Then…Else statement is called _______________.b. An IF statement can have ___________ else statements - each one

___________________________to check. c. Also called an ElseIf Ladder

Page 4: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

4

d. The computer executes the statements below the ______________________________and immediately exits the whole statement (ignores anything below the true statements).

e. The Nested If…Then…Else… is used to decide ________________________________________i. Form:

If condition Then StatementsElseIf condition Then StatementsElseIf condition Then Statements…Else

StatementsEnd If

i. Last Else clause is optional – executes when no True above.

6 If…Then…ElseIf Example #1If a = b Then c = 10ElseIf a > b Then c = 14ElseIf a < b Then c = 16Else c = 12

End If

7 If…Then…ElseIf Example #2 If strStudentClass = “Senior” Then lbl.Answer.Text = “Seniors - start 1 hour late Fri”ElseIf strStudentClass = “Junior” Then lbl.Answer.Text = “Juniors - start 30 min late Fri”ElseIf strStudentClass = “Sophomore” Then lbl.Answer.Text = “Sophomores - start on time Fri”ElseIf strStudentClass = “Freshman” Then lbl.Answer.Text = “Freshmen - start 15 min early Fri”

End If

The Select Decision Structure8 Select Case Decision

a. Another way to make a decision is a ______________________________b. This statement is used _______________multiple else if statements.c. The computer again executes ________________________________________and ignores the rest

of the statement. d. These are decision structure that uses the result of an expression __________________________

_________________________________

Page 5: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

5

Form ExampleSelect Case expression Select Case intScore

Case value Case 0,10 Statements Statements… …Case Else Case Else Statements Statements

End Select End Case

9 Select Casea. ___________________________________to a built-in data type.

i. Integer, Double, Character, String…b. There can be multiple Case clauses.c. Case Else clause is optionald. _______________should match the _________________________and can be

i. a single value, 1. Case 2

ii. a list separated by commas, or 1. Case 1, 2, 3

iii. a range separated by the keyword To.1. Case 1 To 5

10 Case Isa. Compares _______________of an expression to a range of values when _______________________

is part of the value.b. Must use Case Is with relational operators.

i. Example: Case Is < 10

11 Select Case ExampleSelect Case intGrades Case 100

strGrade= “A+” Case 90 To 99

strGrade= “A” Case 80 To 89

strGrade=“B” Case 70 To 79

strGrade=“C” Case 60 To 69

strGrade=“D” Case Is < 60

strGrade=“F” Else

Messagebox.show(“Please input a valid number!”)End Select

Page 6: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

6

12 If..Then vs. Select Casea. In most cases ___________________________________and a ______________________________

__________________________________________________ with only one correct answer. b. In the following example, remember the .Checked property of a radiobutton holds the values _____

_______________c. A statement such as AddMachine() is a block of code called a _________________________that runs

when its name is executed (like the Convert.ToDouble(..) Function).

13 Which Radio Button is Clicked?Select Case True Case radAdd.Checked

AddMachine() Case radMultiply.Checked

MultipleMachine() Case radDivision.Checked

DivisionMachine() Case Else

Messagebox.Show(“Pick a button!”)End Select

Compound Boolean Expressions14 Logical Operators

a. Logical Operators are used to create ___________________________________.b. Use logical operators __________ or _____to form a _______________ expression.c. Logical operators _________________________to create an expression that ____________________

_______________.d. Third operator is _________.e. The order of precedence is _________, then ________, then_____.

15 Compound Boolean Expressionsa. Use to ___________________________________in a single statement.b. Compound expressions ______________________________c. For example:

If intNum1 = 15 And intNum2 > 2 Then statements End If

16 Compound Boolean Expressions - ANDa. T AND T ____________________b. T AND F ____________________c. When using And or & with IF, both (all) conditions ___________________________________ (or else

if) question to evaluate to true. If number < 15 & number > 5 Then

…d. If the number is not less than 15 AND greater than 5 this statement is false.

Page 7: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

7

17 Compound Boolean Expressions - ORIf number < 15 Or number > 5 Then StatementsEnd If

a. Now if any one of the conditions are true, the statement evaluates to true.b. T Or F _____________________c. A truth table shows the possible outcomes of the Boolean Expressions.

18 And Truth Table

19 Or Truth Table

Advanced If Statements20 Short Circuiting

a. Visual Basic provides a way to “short circuit” ___________________________________i. The computer looks at the first statement and _____________________________________

if it needs to look at the rest of the statement.b. AndAlso

If intNum1 > 0 AndAlso intNum2 > 0 Then1. So if inNum1 is not greater than 0 the first Boolean expression is false and the

computer does not waste time looking at num2.c. OrElse

If intNum1 > 0 OrElse intNum2 > 0 Then1. If intNum1 is greater than 0 there is no need to check intNum2 because the first

Boolean expression is true.

Page 8: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

8

21 Using IF in Assignment Statementsa. An IF can also be used in assignment statements ___________________________________.

i. Example: 1. Dim varName As Data Type = If(comparison, value if true, value if false)

ii. Example:1. 'Assign a value to the string strName based on the comparison given. 2. Dim strName As String = If(a < b, "Less than 10", "Greater than 10")

Randomizing a Number22 Randomizing a Number

a. To generate a random integer, we can use the ____________________________class.b. First create your Random Number Generator

Dim gen As New System.Random()

c. You create the name (gen, numGen,…) to match the purpose of the generator.d. Use the next or nextDouble methods to generate the random numbers.

e. NextDoublei. Returns a ____________________________between 0.0 and 1.0.

ii. Example: dblRndNum = gen.NextDouble

a. Generates a number between 0.0 and up to, but not including 1.0+f. Next

i. Returns a __________________________________________between 0 and the MaxValue (2,147,483,647)

ii. Example: intRndNum = gen.Next

a. Generates a number between 0 and up to, but not including 2,147,483,647g. Next (Int32)

i. Returns a _________________________________________________ii. Example:

intRndNum = gen.Next(10)a. Generates a number between 0 and up to (but not including) 10

h. Next (Int32, Int32)i. Returns a ___________________________________

ii. Example: intRndNum = gen.Next(0, 50)

a. Generates a number between 0 and up to (but not including) 50

23 Random Numbers Example below

Page 9: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

9

5.05 Looping Structures

Getting Input from an InputBoxa. Sometimes we need input from the user. b. We already learned how to do this using a text box.c. There is another method: ___________________________.

i. The input box is a ___________________________________________________________. The program stops and waits until the user ____________________________________.

2. Input Boxes – Syntax & Examplea. Syntax:

i. StringVariable = InputBox (__________________)1. The prompt and title can be string literals or _______________________.2. Example:

a. strName= InputBox(“Enter your name to continue!:”, “What is your name?”)ii. The program stops executing until the user chooses the OK or Cancel buttons. Then _______

_____________________________________________.

b. Usesi. _____________________________: Input boxes can be placed within a _________________

________________________________until the user ends the loop. 1. Enter scores (Enter -1 to Quit)

a. A variable number 2. Enter 10 scores

a. A fixed numberii. ___________________________: Input Boxes _______________________________ if the

user actually keyed something.

Page 10: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

10

1. Code uses the NOTHING keyword and an IF statement with the InputBox. Example follows…….

a. Example: Input Boxes – “Empty” Data Validationi. If strName = Nothing Then

strName= InputBox(“Enter your name to continue!:”, “What is your name?”) End If

ii. Example 2: If strName = Nothing Then Messagebox.Show(“Bye!”)

Application.Exit() End If

Special Variables with Loops3. Counter Variables

a. When dealing with _____________________ you also may use a counter variable ________________ ____________________________________________

b. This type of counter you can ___________________________________ outside of the loop.c. This variable (normally of integer data type) ______________________________________________

in the loop.d. The most common name for a counter variable is i. Next comes j, then k, and so on.

4. Increment the Countera. ___________________________________we want to add (or subtract) some constant number

(usually 1) from the counter variable. b. Examples

i = i + 1 or i+=1i = i - 1 or i-=1i+=2 or i-=2

5. Accumulator Variablesa. Similar to a counter, except the value that updates the ______________________________b. Also known as a variable storing a number that is __________________________________________.c. Forms:

accumulator = accumulator + valueaccumulator += value

d. Example: ___________________________________e. Accumulator variables are useful for _____________________________________________

Looping Structures6. Repeated Actions

a. Have you ever performed the same action over and over?i. For example: As long as it rains I am going to sing the same song. When the song finishes I

am going to start over again.b. Programmers can code repeated actions into their programs. This is called _______________.

Page 11: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

11

c. Each time the loop runs is called _________________________.

7. Types of Loopsa. These are the four types of loops.

i. ______________________________ii. ______________________________

iii. ______________________________iv. ______________________________

8. Pretest and Posttesta. When dealing with loops there are two different ways to ______________________________.b. Pretest

i. Test the condition _________________________. If the condition is false _____________ _________________________.

ii. A pretest loop only executes ____________________________________________________.c. Posttest

i. ________________________________then test the condition- - - if the condition is _______ ___________________________________.

ii. A posttest loop will _____________________________________________.

Do While Loop9. Do While…Loop10. Syntax

Do While condition StatementsLoop

a. This form executes ______________________________, therefore __________________________ ________________________________________.

i. This is a _______________loop.1. Example

sum = 20 Do While sum < 10 sum += 2 Loop

b. The statement does not iterate at all because sum is initially greater than 10.

11. Do While ExampleAdd all even numbers 1 to 25 - skip all odd numbers.

Dim i As Integer = 0Dim intResult As Integer = 0Do while i < 25

intResult = intResult + i ‘ updates intResult by ii += 2 ‘ increases i by 2

Loop

Checks the condition first

____________________

_______________

Page 12: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

12

12. Using Do While To Validate Input

Dim intNum As Integer = -1Dim strInputNum As String = “”

strInputNum = InputBox("Enter a Number between 1 & 10", "Number")

Do While strInputNum = Nothing Or (Convert.ToInt32(strInputNum) < 0 Or Convert.ToInt32 (strInputNum) > 10)

MessageBox.Show("Enter a Number between 1 & 10") strInputNum = InputBox("Enter a Number between 1 & 10", "Number")

Loop

MessageBox.Show("You entered a number between 1 & 10. Your number was " & strInputNum)

13. Do…Loop While a. The looping structure that evaluates a condition ________________________________________.

SyntaxDo StatementsLoop While condition

* _______________ is the loop and ___________________________________.

* _______________ is a Boolean expression used to ______________________________ ______________________________.

condition is true _______________

b. The looping structure that executes a set of statements __________________a condition is _______.c. The condition is a __________________ expression.

i. It evaluates to __________ii. Executes at least once.

d. The loop below iterates (repeats) while sum is less than 10: sum = 0;

Do sum =sum + 2

Loop While sum < 10

14. The For… Next Statementa. A looping structure that executes a set of statements ___________________________________.b. It executes until ______________________.c. The counter is automatically _________________________.

i. Syntax:For counter = start To end

Statements

___________________________________________________________________________

Page 13: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

13

Next counter** counter, start and end are _________________________.

15. How the For…Next Worksa. You create an _________________________to use _________________________.b. The _______________the For line executes, your _________________________to whatever is after

the “=”. It _________________________each time the loop repeats. c. Each time “Next” is executed, the ________________________________ (default value is 1)d. The condition is tested on the FOR linee. The condition is still true when counter reaches the value after the “TO”. (Loop still executes)f. When the _________________________, execution __________to statement __________________.g. Remember, counter is updated to false value _____________________________________________.

16. For…Next Examplea. The loop below executes until intNum is equal to 5, by checking one last time (it is no longer true),

jumps to Next and exits the loop.

For intNum = 1 To 4 ‘accumulator intTotal = intTotal + intNumNext intNum

b. The variable counter “intNum” holds a 5 after the loop ends, but the code inside only executed four times.

17. For…Next – Internal Countera. You may create the counter variable (_______________) in the For line by using the optional As

Integer keywords, rather than using a Dim command ___________________.

b. The lifetime of the variable counter created this way is the lifetime of the loop. (When you exit the loop, the variable counter no longer exists).

For intCount As Integer = 0 To 4 intTotal = intTotal + intCountNext intCount

**intCount is only in scope in the loop

18. The For … Next Statement with Stepa. Step: Changes the way the counter is ____________________. It can be ______________________________b. Syntax

For counter = start To end Step stepnumStatements

Next counter c. The step integer can be a ____________number to tell the compiler to ________________________.

d. Make sure your start is the ____________________, the end is the ____________________.

intNum intTotal

1 1

2 3

3 6

4 10

5

intNum intTotal

1 1

2 3

3 6

4 10

Page 14: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

14

For intY = 5 To 1 Step -1 MessageBox.Show("Counting Down " & intY)Next intY

e. Add all even numbers 1 to 250 - _________________________.For i as Integer = 0 to 250 Step 2 intResult = intResult + iNext IlblResult.Text = intResult.ToString()

f. Counting by _______ instead of 1’s.

Special Variables with Loops19. Using Flags

a. Flag or Sentineli. A condition used to __________________________________________________.

b. ExampleConst strFLAG As String = “Quit”

strTempInput = InputBox(“Enter a positive number (Quit to stop)”)

Do While strTempInput <> Nothing Or strTempInput <> strFLAG

Statements strTempInput = InputBox(“Enter a positive number (Quit to stop)”)

Loop

20. Endless Loopa. A ____________________known as the _________________________________ occurs when the

programmer _________________________the counter variable.b. A loop is endless when ____________________- so it will run _______________________________

(or modern operating systems/web browsers will alert the end user so they can terminate the program).

Dim x As Integer = 5 Do While (x < 10) ‘always true

x -= 1 Loop

21. So What Loop Should You Use?a. Use a For…Next loop when you know ______________________________the loop will run or are

_________________________.i. This can be in the form of a ___________________________________.

ii. The condition will ________________________________________. b. Use a While loop (or Do While) when ______________________________the condition becoming

_______________. i. If you do not make the condition false, an _________________________results

ii. Two possible ways to control a While: ___________________________________or a flag input from the user.

Computer Programming 1 Chapter 5 Vocabulary Essential

Page 15: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

15

Standard: 5.00 Apply Programming and Conditional LogicIndicator 5.01 Understand different types of programming errors.Indicator 5.02 Understand Breakpoint, Watch Window, and Try And Catch to Find Errors.Indicator 5.03 Apply Operators and Boolean expressions.Indicator 5.04 Apply Decision-making Structures.Indicator 5.05 Apply Looping Statements.

Accumulator A variable used to store a number that is incremented by a changing amount.

Concatenated Strings that have been joined.

Concatenation The process of joining two or more strings into one string.

Flag A condition used to signify that a loop should stop executing.

Infinite loop A loop that continues forever.

Input box A predefined dialog box that accepts input from the user.

Iteration Also called looping.

Loop A set of statements that repeatedly perform a task based on a condition.

Loop structure A statement that executes a loop as long as a condition is true.

Looping Repeating one or more statements.

Members Properties and methods of a class.

Method A procedure in a class.

Object A variable that is declared with a data type that is a class.

Pattern matching Allows wildcard characters (?, *, #), character lists, and character ranges ([A–M, Z]) to match strings using the Like operator.

Robust Describes an application that performs even under unexpected circumstances.

Shared method A method that is used with a class, not with a particular object of that class.

Sentinel See Flag.

Structure A simple form of a class.

Substring A portion of a string.

Textual comparison Comparing characters without distinguishing case.

Unicode A digital code that represents every letter of an alphabet and symbols of every culture. Each code uses 16 bits or 2 bytes.

Page 16: 5.03_Applying_Operators€¦ · Web view5.03 Apply operators and Boolean expressions Operators Just like Math class, Operators are the _____ for performing math. You have used them

11/4/2016

16

Chapter 5 Visual Basic

*= Assignment operator that multiplies the value on the right of the statement and the current value of the variable on the left and then updates the variable to store the new value.

/= Assignment operator that uses the value on the right of the statement to divide the current value of the variable on the left and then updates the variable to store the new value.

\= Assignment operator that uses the value on the right of the statement to divide the current value of the variable on the left and then updates the variable to store the integer portion of the new value.

^= Assignment operator that raises the current value of the variable on the left to the power of the value on the right of the assignment statement and then updates the variable to store the new value.

&= Assignment operator that concatenates the string on the right of the statement and the current string of the variable on the left and then updates the variable to store the new string.

& Used to concatenate two or more strings.

AscW() Function that returns the integer Unicode value that corresponds to a character argument.

Char structure Used to manipulate characters. Methods include ToLower() and ToUpper().

ChrW() Function that returns the character corresponding to an integer representing a Unicode value.

Do...Loop Statement that repeatedly executes a loop as long as a condition is True.

For...Next Statement that executes a loop a fixed number of times.

InputBox() Function used to generate a predefined dialog box that has a prompt, a text box, and OK and Cancel buttons.

Like Operator used to perform textual comparison on strings and pattern matching using characters such as ?, *, #, [].

Space() Function used to generate a string of spaces.

Step Keyword used in a For...Next statement to increment or decrement the counter by a set amount.

String class Used to manipulate strings. Properties include Chars and Length. Methods include Concat(), Compare(), IndexOf(), Insert(), PadLeft(), PadRight(), Remove(), Replace(), Substring(), Trim(), ToUpper(), ToLower(), TrimEnd(), and TrimStart().

vbCrLf Built-in constant that represents a carriage return-linefeed combination.

vbTab Built-in constant that places a string in the next field of eight characters.