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

Slides:



Advertisements
Similar presentations
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Advertisements

CS0004: Introduction to Programming Repetition – Do Loops.
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.
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.
Repetition Chapter 7. Overview u For Loop u Do Loop  Do While Loop  Do Until Loop  Do Loop While  Do Loop Until u Nested Loops.
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.
© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 8: Chapter 5: Slide 1 Unit 8 List Boxes and the Do While Looping Structure.
ISAT 252 Visual Basic Repetition. Assignment Should have read Chapter 5 ( ) on loops Do tutorial 5-4.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
‘Tirgul’ # 3 Enterprise Development Using Visual Basic 6.0 Autumn 2002 Tirgul #3.
CS285 Visual Basic 2 Department of Computing UniS 1 Statements in Visual Basic A statement is the fundamental syntactical element of a program smallest.
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.
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.
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.
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.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
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()
Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT.
Looping Structures. A B dialog box that pops up and prompts the user for input Text area that pops up and prompts the user for to wait while it gets.
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
UNIT 5 Lesson 15 Looping.
Visual Basic 6 (VB6) Data Types, And Operators
Lists, Loops, Validation, and More
حلقات التكرار.
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Visual Basic 6 Programming.
Three Special Structures – Case, Do While, and Do Until
Visual Basic 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 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 Else MsgBox “You dope. You can't even remember your myPIN. ” txtPIN.Text = "" txtPIN.Focus End If

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 Dim x as Integer= 0 ‘loop control variable For x = 1 To 100 lbNumbers.Items.Add(x) Next x For…Next loops are called Counted loops

Nested For…Next Loop Statement Used As a Counter... 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

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

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

Do…While Statements (Conditional Loops) Do While... Loop: Dim myPIN as String =“” Do While myPIN = “ ” myPIN =InputBox(“Enter myPIN”) Loop Label1.Text = myPIN Do...While Loop: Dim myPIN as String Do myPIN = InputBox(“ Enter myPIN ”) Loop While myPIN = “ ” Label1.Text = myPIN Pre-test condition 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 Sub Form1_Load () Dim price as Decimal = 0 Do price = InputBox (“Enter price of item...”) Loop Until Price = 0 End Sub InputBox is a “Built-In” VB function ALL functions return a value price = InputBox (“Please enter a price”, “Ron’s Store”) 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!