Computer Science Up Down Controls, Decisions and Random Numbers.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Select Case Statements and Selection Input.
Advertisements

Microsoft® Small Basic
5.04 Apply Decision Making Structures
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
5.05 Apply Looping Structures
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Visual Basic Chapter 1 Mr. Wangler.
© 1999, by Que Education and Training, Chapter 5, pages of Introduction to Computer Programming with Visual Basic 6: A Problem-Solving Approach.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
CS0004: Introduction to Programming Relational Operators, Logical Operators, and If Statements.
COMPUTER PROGRAMMING I Objective 7.04 Apply Built-in String Functions (3%)
Chapter 4 The If…Then Statement
Chapter 7 Decision Making. Class 7: Decision Making Use the Boolean data type in decision-making statements Use If statements and Select Case statements.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
1 Κατανεμημένες Διαδικτυακές Εφαρμογές Πολυμέσων Γιάννης Πετράκης.
08/10/ Iteration Loops For … To … Next. 208/10/2015 Learning Objectives Define a program loop. State when a loop will end. State when the For.
Lecture 8 Visual Basic (2).
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 4 Working with Variables, Constants, Data Types, and Expressions.
More switches, Comparison Day 7 Computer Programming through Robotics CPST 410 Summer 2009.
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 5: More on the Selection Structure Programming with Microsoft Visual Basic 2005, Third Edition.
CS0004: Introduction to Programming Project 1 – Lessons Learned.
Conditional Expression One of the most useful tools for processing information in an event procedure is a conditional expression. A conditional expression.
Chapter 5: More on the Selection Structure
Programming with Microsoft Visual Basic th Edition
6c – Function Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB.NET Programming.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
1 Week 5 More on the Selection Structure. 2 Nested, If/ElseIf/Else, and Case Selection Structures Lesson A Objectives After completing this lesson, you.
1 Advanced Computer Programming Lab Calculator Project.
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Created by Alia Al-Abdulkarim 2008 Visual Basic Vs. Java.
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Craps Game Application Introducing Random-Number Generation and Enum.
COMPUTER PROGRAMMING I 5.04 Apply Decision Making Structures.
COMPUTER PROGRAMMING I SUMMER Apply operators and Boolean expressions.
31/01/ Selection If selection construct.
T U T O R I A L  2009 Pearson Education, Inc. All rights reserved Student Grades Application Introducing Two-Dimensional Arrays and RadioButton.
Controlling Program Flow with Decision Structures.
Controlling Program Flow with Decision Structures.
5.02B Decision Making Structure (part 2). Compound Boolean Expressions.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Logical Operators.  Quiz  Let's look at the schedule  Logical Operators 2.
Chapter Five More on the Selection Structure Programming with Microsoft Visual Basic th Edition.
COM148X1 Interactive Programming Lecture 8. Topics Today Review.
Introduction to Programming Python Lab 6: Relational Operators and Boolean Variables 12 February PythonLab6 lecture slides.ppt Ping Brennan
Programming with Visual Basic.NET. Quick Links Program Code The Code Window The Event Procedure Assignment Statements Using AutoList Radio Buttons Buttons.
Visual Basic Declaring Variables Dim x as Integer = 0 In the statement above, x is being declared as an Integer (whole number) and is initialised.
Slide 1 Chapter 4 The If…Then Statement  Conditional control structure, also called a decision structure  Executes a set of statements when a condition.
TUTORIAL 4 Visual Basic 6.0 Mr. Crone. Pseudocode Pseudocode is written language that is part-code part- English and helps a programmer to plan the layout.
Use TryParse to Validate User Input
Visual Basic Fundamental Concepts
5.04 Apply Decision Making Structures
UNIT 5 Lesson 15 Looping.
UNIT 4 Lesson 13 If statements.
Debugging and Random Numbers
Use TryParse to Validate User Input
Introduction to VB programming
Microsoft Visual Basic 2005 BASICS
Visual Basic..
Lesson 8: Boolean Expressions and "if" Statements
Boolean Expressions and If statements
Conditions and Ifs BIS1523 – Lecture 8.
1.الدوال Function 2.الاجراءاتSub Procedure 3.وحده نمطيه Add Module
Items, Group Boxes, Check Boxes & Radio Buttons
Project Design, Forms and Buttons
GUI Programming in Visual Studio .NET
Presentation transcript:

Computer Science Up Down Controls, Decisions and Random Numbers

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

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

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.

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

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.

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)?

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.

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 <=

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.

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

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

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

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.

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:

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

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)