Visual Basic Programming

Slides:



Advertisements
Similar presentations
“Going loopy with Visual Basic!”
Advertisements

Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
1 Loops zDo While Loop: yRepeats a block of code WHILE a condition remains TRUE Do While condition Code to execute Loop.
IS437: Fall 2004 Instructor: Dr. Boris Jukic Program Flow Control: Decisions and Conditions (Branching) Controlled Repetition (Looping)
Slide 1 VB Program Flow Control. Slide 2 Making Decisions v Decision Statement: control the execution of parts of the program based on conditions. v The.
Loops Counting down from N to 1 in assembly we used JUMP statements to repeat previous instructions thus looping READ LOOP: WRITE SUB decrement JPOSLOOP.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
5.05 Apply Looping Structures
Muffin Shop - if, calculations etc. (muffins, muffins2) Please use speaker notes for additional information!
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
Chapter 7 Code Tables. VB Code Box 7-1 Event Procedure for Compute Button Private Sub hsbExemptions_Change() txtExemptions.Text =Str(hsbExemptions.Value)
CIS 115 Lecture 8. There are 3 control structures common to most computer languages that determine the flow, or path of execution, of the code:  Sequential.
情報基礎 B Lecture 8 Takeshi Tokuyama Tohoku University Graduate School of Information Sciences System Information Sciences Design and Analysis of Information.
Chapter 4: The Selection Process in Visual Basic.
1 CC111 Lec9 : Visual Basic Visual Basic (3) Lecture 9.
ENGR 112 Decision Structures.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
Chapter Six: Working With Arrays in Visual Basic.
CONTROLLING PROGRAM FLOW
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
1 Flow Control II Code: Select-Case and For-Next Controls: Frames and OptionButtons.
Introduction to MIS1 Copyright © by Jerry Post Introduction to MIS Appendix 12 Visual Basic.
Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright by Ronald P. Kessler, Ph.D.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.
Lab 4 Range Review, Control Logic and Loops ► Range Review ► Control Logic and Loops ► Exercise.
ASP-5-1 ASP Control Structures Colorado Technical University IT420 Tim Peterson.
CSC115 Introduction to Computer Programming Zhen Jiang Dept. of Computer Science West Chester University West Chester, PA 19383
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
1 Flow Control Ifs, loops. 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.
Chapter 16: Programming Structures Spreadsheet-Based Decision Support Systems Prof. Name Position (123) University Name.
CS 101 Test 2 Study Guide Acronyms RAD - Rapid Application Development IDE - Integrated Development Environment GUI - Graphical User Interface VB - Visual.
1 CS105 Discussion 5 – Variables and If Announcements MP 1 due on Monday Midterm 1 on Tuesday If you need a conflict, request it NOW!!
Copyright © 2001 by Wiley. All rights reserved. Chapter 6: Using Arrays Control Arrays List Arrays Finding Items in Arrays Multiple Forms 2-Dimensional.
4-1 Chapter 4 The Selection Process in VB.NET. 4-2 Learning Objectives Understand the importance of the selection process in programming. Describe the.
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.
Visual Basic Programming I 56:150 Information System Design.
Looping Structures Do Loops, For Next Do...Loop While structures check the condition after executing the code and repeat a code block until the test.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
1 Microsoft® Visual Basic®.NET Language # 2. 2 Flow-Control Statements If … End If Select Case … End Select For… Next Do … Loop Exit.
Controlling Program Flow with Looping Structures
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Controlling Program Flow with Decision Structures.
Input Boxes, List Boxes, and Loops Chapter 5. 2 Input Boxes Method for getting user’s attention to obtain input. InputBox() for obtaining input MessageBox()
Loop and repetition. Today Passing values to and back from Sub procedures Option Buttons Do While Loops For Loops Population Growth.
Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
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.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 6 Controlling Program Flow with Looping Structures.
Recursion and Branching By: Engr. Faisal ur Rehman CE-105T Spring 2007 By: Engr. Faisal ur Rehman CE-105T Spring 2007.
Introduction to Programming Lecture 2
IE 8580 Module 4: DIY Monte Carlo Simulation
UNIT 5 Lesson 15 Looping.
Visual Basic 6 (VB6) Data Types, And Operators
Chapter 5: Loops and Files.
CSC115 Introduction to Computer Programming
حلقات التكرار.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Visual Basic 6 Programming.
Prepared By: Deborah Becker
GCSE Computing:: Selection (IF statements)
Final Revision sheet- term2
Final Revision sheet- term2
Repetition - Counting and Accumulating
Introduction to Computer Programming IT-104
Presentation transcript:

Visual Basic Programming Making Decisions: Loops & Decision Structures ©Copyright 1999-2012 by Ronald P. Kessler, Ph.D.

Program Control: If…Then…Else Statement Conditional statements allow us to make logical evaluations: If Sales > 1200 then Commission= 0.08 BLOCK STYLE: If Sales < 500 Then Commission= .08 ElseIf Sales > 500 And Sales < 1000 Then Commission= .10 Else Commission = .25 End If Nothing after Then ElseIf is never alone Optional: It “catches” “other” conditions Must have End If for each If

If…Then…Else Statement Used to Make Logical Decision If MessageBox.Show("Are you sure”,”Ron’s Store” , MessageBoxButtons.YesNo) = _ Windows.Forms.DialogResult.Yes Then Me.Close() Else MessageBox.Show("Then make up your mind") End If

“Nested” If…Then…Else If txtPIN.Text = “123” Then MsgBox "Congratulations, you remembered your myPIN” btnOK.Enabled = True If optReceipt.Checked = True Then btnPrint.Enabled = True Else btnPrint.Enabled = False End If MsgBox “You dope. You can't even remember your myPIN. ” txtPIN.Text = "" txtPIN.Focus

Program Control: Select Case Statement Select Case statements are used when we want to perform different actions for each value of a variable Dim state as String = “” Dim salesTax as Single = 0 Select Case state Case “CA” salesTax= .085 Case “AZ” , “NJ” salesTax= .075 Case “NV” salesTax= 0.3 Case Else salesTax= 0 End Select Variable to evaluate Case can be value or string or condition Optional: It “catches” “other” conditions Must have End Select

Select Case Statement: Testing a Range of Values Dim discount as Single = 0 Dim unitsOrdered as Integer = 0 Select Case unitsOrdered Case 1 to 10 discount = 0 Case 11 to 19 discount = .05 Case Is >= 20 discount = .1 End Select

Using Loops...

For…Next Loop Statement Used to Add Numbers to a List Box For…Next loops are called Counted loops Dim x as Integer = 0 ‘loop control variable For x = 1 To 100 lbNumbers.Items.Add(x) Next x 1 2 3 4

Nested For…Next Loop Statement Used As a Counter... 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 Dim x as Integer = 0 Dim y as Integer = 0 For x = 1 To 3 For y = 1 To 5 lbNumbers.Items.Add(y) Next y Next x

For…Next Loop Statement Used As a Backwards Counter... Dim x as Integer = 0 For x = 10 To 1 Step -1 ListBox1.Items.Add (x) Next x 10 9 8 7 6 5 4 3 2 1

For…Next Loop Statement Used To Count by 2’s Dim x as Integer = 0 For x = 2 To 20 Step 2 ListBox1.Items.Add (x) Next x 2 4 6 8 10 12 14 16 18 20

For…Next Loop Statement Used To Count Backwards by 2’s Dim x as Integer = 0 For x = 20 To 2 Step -2 ListBox1.Items.Add (x) Next x 20 18 16 14 12 10 8 6 4 2

Do…While Statements (Conditional Loops) Do While... Loop: Dim myPIN as String =“” Do While myPIN = “ ” myPIN =InputBox(“Enter myPIN”) Loop Label1.Text = myPIN Pre-test condition Do ...While Loop: Dim myPIN as String Do myPIN = InputBox(“Enter myPIN”) Loop While myPIN = “ ” Label1.Text = myPIN Post-test condition

Do Until Statements (Conditional Loops) Do Until... Loop: Dim myPIN as String =“” Do Until myPIN = “Ron ” myPIN =InputBox(“Enter myPIN”) Loop Label1.Text = myPIN Do …Until Loop: Dim myPIN as String = “” Do myPIN = InputBox(“Enter myPIN”) Loop Until myPIN = “Ron ” Or myPIN = “99” Label1.Text = myPIN

Using Counts & Totals...

Counts vs Totals... Counts increase or decrease by a fixed amount... Dim count as Integer = 0 Do myPIN =InputBox(“Enter PIN”) count = count +1 Loop Until myPIN = “9” or count =5 Label1.Text = count Totals change by a variable amount… Dim myPrice as Currency = 0 Dim myTotal as Currency = 0 Total = 0 Do Price = InputBox(“Enter Price”) myTotal = myTotal + myPrice Loop Until myPrice = 0 Label1.Text = Total

Using the InputBox Function InputBox is a “Built-In” VB function ALL functions return a value price = InputBox (“Please enter a price” , “Ron’s Store”) Sub Form1_Load () Dim price as Decimal = 0 Do price = InputBox (“Enter price of item...”) Loop Until Price = 0 End Sub InputBox returns Price as a Currency variable. If they hit Cancel, then InputBox returns “” which will cause an error because “” is a string, not currency.

That’s All Folks!