Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science Up Down Controls, Decisions and Random Numbers.

Similar presentations


Presentation on theme: "Computer Science Up Down Controls, Decisions and Random Numbers."— Presentation transcript:

1 Computer Science Up Down Controls, Decisions and Random Numbers

2 Numeric UpDown Control The Numeric UpDown control is used to obtain a numeric input. In Toolbox: On Form (default properties):

3 TASK: Numeric UpDown Control Start Visual Basic Express and start a new project. We will create a numeric updown control that provides numbers from 0 to 20. Put a numeric updown control on the form. (Make sure you choose the numeric updown control and not the domain updown control.) Resize it and move it, if desired. Set the following properties: PropertyValue Value10 Increment1 Minimum0 Maximum20 ReadOnlyFalse

4 TASK: Numeric UpDown Control Run the project. The numeric updown control will appear and display a value of 10. Click the end arrows and see the value change. Notice the value will not drop below 0 or above 20, the limits established at design time.

5 Logical Expressions True or False This expression is asking the question “is X and Y true?” This expression is asking the question “is X or Y true?” XYX And Y True False TrueFalse Logic Table for AND Logic Table for OR XYX Or Y True FalseTrue FalseTrue False

6 Logical AND A > 10 And B > 10 Comparisons are done first, left to right since all comparison operators share the same level of precedence. A (14) is greater than 10, so A > 10 is True. B (7) is not greater than 10, so B > 10 is False. Since one expression is not True, the result of the And operation is False. This expression ‘A > 10 And B > 10’ is False.

7 Logical OR What is the result of this expression: A > 10 Or B > 10 Can you see this expression is True (A > 10 is True, B > 10 is False; True Or False is True)?

8 Logical Expression So, let’s complicate things a bit. What if the expression is: A > 10 Or B > 10 And A + B = 20 Precedence tells us the arithmetic is done first (A and B are added), then the comparisons, left to right. We know A > 10 is True, B > 10 is False, A + B = 20 is False. So, this expression, in terms of Boolean comparison values, becomes: True Or False And False How do we evaluate this? Precedence says the And is done first, then the Or. The result of ‘False And False’ is False, so the expression reduces to: True or False which has a result of True. Hence, we say the expression ‘A > 10 Or B > 10 And A + B = 20’ is True.

9 Comparison Operators Comparison operators do exactly what they say - they compare two values, with the output of the comparison being a Boolean value. That is, the result of the comparison is either True or False. Comparison operators allow us to construct logical expressions that can be used in decision making. There are 6 different types: –True –False –Not Equal to <> –Greater than 8>3 - greater than or equal to >= –Less Than 3<8 - Less than or equal to <=

10 Operator Precedence Comparison operators have equal precedence among themselves, but are lower than the precedence of arithmetic operators. This means comparisons are done after any arithmetic.

11 The IF Statement Actually, the If statement is not a single statement, but rather a group of statements that implements some decision logic. The If statement checks a particular logical expression. It executes different groups of BASIC statements, depending on whether that expression is True or False. The BASIC structure for this logic is: If Expression Then [BASIC code to be executed if Expression is True] Else [BASIC code to be executed if Expression is False] End If

12 ELSE and ELSE IF The Else keyword and the statements between Else and End If are optional. If Temperature > 90 Then Cost = 50 ElseIf Temperature > 80 Then Cost = 40 ElseIf Temperature > 70 Then Cost = 30 Else Cost = 25 End If

13 What type of error does this produce? If Temperature > 70 Then Cost = 30 ElseIf Temperature > 80 Then Cost = 40 ElseIf Temperature > 90 Then Cost = 50 Else Cost = 25 End If

14 Random Number Generator Why do you need random numbers? Visual Basic Express has several methods for generating random numbers. The generator uses what is called the Random object.

15 TASK: Random Numbers Let’s try it. Start Visual Basic Express and start a new project. Put a button (default name Button1) and label control (default name Label1) on the form. Add this line to create the random number object:

16 Random Numbers Dim MyRandom As New Random Then, put this code in the Button1_Click event procedure: Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Label1.Text = Str(MyRandom.Next(10)) End Sub

17 Programming Applications This opens up a lot of possibilities to you as a programmer. Every computer game, video game, and computer simulation, like sports games and flight simulators, use random numbers. A roll of a die can produce a number from 1 to 6. To use our MyRandom object to roll a die, we would write: DieNumber = MyRandom.Next(6) + 1 For a deck of cards, the random integers would range from 1 to 52 since there are 52 cards in a standard playing deck. Code to do this: CardNumber = MyRandom.Next(52) + 1 If we want a number between 0 and 100, we would use: YourNumber = MyRandom.Next(101)


Download ppt "Computer Science Up Down Controls, Decisions and Random Numbers."

Similar presentations


Ads by Google