Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.

Slides:



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

Object Oriented Programming A programming concept which views programs as objects with properties and ways to manipulate the object and the properties.
Programming with Microsoft Visual Basic th Edition
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
CS0004: Introduction to Programming Repetition – Do Loops.
Repeating Actions While and For Loops
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.
Iteration (Looping Constructs in VB) Iteration: Groups of statements which are repeatedly executed until a certain test is satisfied Carrying out Iteration.
Control Structures: Part 2. Introduction Essentials of Counter-Controlled Repetition For / Next Repetition Structure Examples Using the For / Next Structure.
VBA Programming Session #2. Things to Review  Variables  Procedures: Subs & Functions  If…Then  For…Next.
Repeating Program Instructions Chapter Microsoft Visual Basic.NET: Reloaded 1.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Adding Automated Functionality to Office Applications.
5.05 Apply Looping Structures
Repetition Statements Repeating an Action A specified number of times While a Condition is True Until a Condition is True.
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.
Chapter 12: How Long Can This Go On?
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture Set 5 Control Structures Part A - Decisions Structures.
ENGR 112 Decision Structures.
CONTROLLING PROGRAM FLOW
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
1 Week 6 The Repetition Structure. 2 The Repetition Structure (Looping) Lesson A Objectives After completing this lesson, you will be able to:  Code.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Compunet Corporation1 Programming with Visual Basic.NET While, Do and For – Next Loops Week 5 Tariq Ibn Aziz.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Tutorial 6 The Repetition Structure
Chapter 4 Looping Statements Adapted From: Starting Out with Visual Basic 2008 (Pearson)
Visual Basic.net Loops. Used to do multiple executions of the same block of code Do while loops Do until loops For next loops.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Visual Basic Programming
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.
Algorithm Design.
Overview of VBA Programming & Syntax. Programming With Objects u Objects –Properties: attributes or characteristics of an object (e.g., font size, color,
Advanced Program Design. Review  Step 1: Problem analysis and specification –Specification description of the problem’s inputs and output –Analysis generalize.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Advanced Repetition Structure and String Functions (Unit 10) Visual Basic for Applications.
Visual Basic Objects / Properties / Methods PropertyAdjective ObjectNoun Part of the application Attribute MethodVerb Action to do something.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Tutorial 6: The Repetition Structure1 Tutorial 6 The Repetition Structure.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
Controlling Program Flow with Decision Structures.
COMP Loop Statements Yi Hong May 21, 2015.
 2002 Prentice Hall. All rights reserved. 1 Chapter 5 – Control Structures: Part 2 Outline 5.1Introduction 5.2 Essentials of Counter-Controlled Repetition.
Fourth Quarter.  Involves loops or cycles ◦ Loops: means that a process may be repeated as long as certain condition remains true or remains false. ◦
CSCI 3327 Visual Basic Chapter 4: Control Statements in Visual Basic (Part 2B) UTPA – Fall 2011 Part of the slides is from Dr. John Abraham’s previous.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
CSET 3250 Client-Side Scripting VBScript : Operators, Control Structures, Procedures and Functions.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
IE 8580 Module 4: DIY Monte Carlo Simulation
UNIT 5 Lesson 15 Looping.
Chapter 5- Control Structures: Part 2
Visual Basic 6 (VB6) Data Types, And Operators
Lists, Loops, Validation, and More
Control Structures: Part 2
Control Structures: Part 1
CIS 16 Application Development Programming with Visual Basic
Introduction to Problem Solving and Control Statements
Problem Solving and Control Statements
Fundamentals of visual basic
ASP control structure BRANCHING STATEMENTS
Presentation transcript:

Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise

Object Browser View  Object Browser F2 (In VBE)

Control Logic – Chpt 7 Logical constructions control the sequence of statements in a procedure, and thus enable a decision making capability. The following two constructions are used most frequently in VBA:  If Constructions  Case Constructions

If Constructions (1) Basic syntax (In one single line): If condition Then statement A [Else statement B] Example: If grade > 90 Then MsgBox ″Good Performance! ″ Else _ MsgBox ″ Need to work harder. ″ More complicated syntax: If condition1 Then statements1 [ElseIf condition2 Then statements2 ElseIf condition3 Then statements3 …… Else other statements] End IF Example:

If Constructions (2) Sub GreetMe() If Time < 0.5 Then MsgBox "Good morning!" & " It’s now " & Time,, “The Time” ElseIf Time >= 0.5 And Time < 0.75 Then MsgBox "Good afternoon!" & " It’s now " & Time,, “The Time” Else MsgBox "Good evening!" & " It’s now " & Time,, “The Time” End If End Sub (In VBA, Time function returns a value that represents current time of the day. The time of day is expressed as a fractional value, i.e.12 pm: Time = 0.5, 6pm: Time = 0.75)

If Constructions (3) Nested If statements (under such circumstances, indentation is very important for ease of reading): Example: If Product = “Widgets” Then If NumberOrdered <= 200 Then UnitCost = 1.30 Else UnitCost = 1.20 End IF ElseIf Product = “Gadgets” Then If NumberOrdered <= 500 Then UnitCost = 2.70 ElseIf NumberOrdered <= 600 Then UnitCost = 2.60 Else UnitCost = 2.50 End If Else UnitCost = 2.00 End IF

Case Constructions (1) If constructions can become fairly complex when there are multiple conditions with each condition having its own codes. Case construction is a good alternative for choosing among three or more options General Syntax: Select Case VariableName Case Value1 statements1 Case Value2 statements2 … [Case Else other statements] End Select

Case Constructions (2) Example: Dim UnitPrice as single, UnitCost as single UnitCost = InputBox(“Please enter the unit cost:”,”Unit Cost” Select Case ProductIndex Case Is <= 3 [Is keyword for comparison operators] UnitPrice = 1.2 * UnitCost Case 4 To 6 [To keyword specifies a range of values] UnitPrice = 1.3 * UnitCost Case 7 UnitPrice = 1.4 * UnitCost Case Else UnitPrice = 1.1 * UnitCost End Select MsgBox “The unit cost was “ & format(UnitCost,”$##.#0) & “the unit price was “ & format(UnitPrice,”$##.#0) & “.”,,”Unit Price” ‘ Is and To are keywords

Loops Perhaps the single most useful feature of computer programs is their ability to loop – to repeat the same type of task any number of times. In VBA, the following two types of loops are used most often:  For loops – you determine when the loop is done  Do loops – program determines when the loop is done

For-Next Loops Syntax: For counter = startvalue To endvalue [Step stepvalue] statements Next [counter] counter is the name of a numeric variable and it keeps track of how many times the statements are repeated – common names: i, j, k… startvalue, endvalue, and stepvalue must be numeric and they can be either positive or negative, integer or non-integer (default stepvalue is 1) Example: add 1, 3, 5, …, 49 Sub AddOddInteger() Dim Sum As Integer, i As Integer Sum = 0 For i = 1 To 49 Step 2 Sum = Sum + i Next i MsgBox "The sum of odd numbers from 1-50 is " & Sum End Sub For loops can also be nested

For Each Loops (1) For Each loops are used to loop through all objects in a collection Syntax: Dim itm As Object For Each itm in Collection statements Next Object and Collection will vary depending on the type of collection

For Each Loops (2) Example: search through all worksheets of the active workbook for a sheet named Data. If you find one, you can exit the loop immediately. Sub FindWorksheet() Dim ws As Worksheet, Found As Boolean Found = False For Each ws in ActiveWorkbook.Worksheets If ws.Name = “Data” Then Found = True Exit For End If Next If Found = True Then MsgBox “There is a worksheet named Data.” Else MsgBox “There is no worksheet named Data.” End If End Sub

Do Loops (1)  The Do loops can be used when you need to loop while some condition holds or until some condition holds. There are following four variations of Do loops: 1. Do Until…Loop –Syntax: Do Until Condition statements Loop 2. Do While…Loop –Syntax: Do While Condition statements Loop

Do Loops (2) 3. Do… Loop Until –Syntax: Do statements Loop Until Condition 4. Do…Loop While –Syntax: Do statements Loop While Condition Major difference: the statements in the loop in the first two variations might never be executed, but they will certainly be executed at least once in the last two variations Exit Do statement can be used to exit a Do Loop prematurely Be careful of infinite loops

Do Loops (3) Example: Do While… Loop Count = 1 Do While Count <= 3 MsgBox Count Count = Count + 1 Loop Count = 1 Count <= 3 Display Count Count = Count + 1 F T Loop 3 times

Do Loops (4) Example: Do …Loop Until Count = 1 Do MsgBox Count Count = Count + 1 Loop Until Count >3 Loop 3 times Count = 1 Count > 3 Display Count Count = Count + 1 F T

Assignment 1 Rewrite the previous GreetMe example by using the Case Construction instead of the IF-Then Construction. Hint: Time is not inputted or declared. It is a VBA keyword. Create a sub and use the case construction without declaring any variables!