259 Lecture 11 Spring 2013 Advanced Excel Topics – Loops.

Slides:



Advertisements
Similar presentations
While loops.
Advertisements

Looping Structures: Do Loops
CS0004: Introduction to Programming Repetition – Do Loops.
Loops (Part 1) Computer Science Erwin High School Fall 2014.
Repeating Actions While and For Loops
ITC 240: Web Application Programming
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Program Design and Development
Loops – While, Do, For Repetition Statements Introduction to Arrays
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
5.05 Apply Looping Structures
Control Structures - Repetition Chapter 5 2 Chapter Topics Why Is Repetition Needed The Repetition Structure Counter Controlled Loops Sentinel Controlled.
Python – Part 4 Conditionals and Recursion. Modulus Operator Yields the remainder when first operand is divided by the second. >>>remainder=7%3 >>>print.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Fac of Ene & Surveying U.S.Q.1 E0001 Computers in Engineering DO.... LOOP.
Chapter 12: How Long Can This Go On?
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
COIT29222 Structured Programming Slide 1 COIT29222-Structured Programming Lecture Week 06  Reading: Study Guide Book 2, Modules 9 & 10 Textbook (4 th.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Visual Basic Programming
PROBLEM SOLVING WITH LOOPS Chapter 7. Concept of Repetition Structure Logic It is a computer task, that is used for Repeating a series of instructions.
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.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Using Loops. Goals Understand how to create while loops in JavaScript. Understand how to create do/while loops in JavaScript. Understand how to create.
Before we get started…. First, a few things… Weighted Grading System Programming Style Submitting your assignments… The char and string variable types.
Counting Loops.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 6 Looping Structures. Do…LoopDo…Loop Statement Can operate statements repetitively Do intx=intx + 1 Loop While intx < 10 –The Loop While operates.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
For…Next and Do...While Loops! Happy St. Patrick’s Day!
Controlling Program Flow with Looping Structures
1 b Boolean expressions b truth tables b conditional operator b switch statement b repetition statements: whilewhile do/whiledo/while forfor Lecture 3.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Lab 6 (1) Range Review, Control Logic and Loops ► Control Logic and Loops ► Exercise.
Intro to Loops 1.General Knowledge 2.Two Types of Loops 3.The WHILE loop 1.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
National Diploma Unit 4 Introduction to Software Development Data Structures – Loops and selection.
CPSC 233 Tutorial 5 February 2 th /3 th, Java Loop Statements A portion of a program that repeats a statement or a group of statements is called.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
Python – Part 4 Conditionals and Recursion. Conditional execution If statement if x>0:# CONDITION print (‘x is positive’) Same structure as function definition.
COMPUTER PROGRAMMING I 5.05 Apply Looping Structures.
CE En 270 Brigham Young University Norm Jones
REPETITION CONTROL STRUCTURE
Advanced Excel Topics – Loops
JavaScript: Control Statements.
ITM 352 Flow-Control: Loops
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Repetition Statements (Loops) - 2
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

259 Lecture 11 Spring 2013 Advanced Excel Topics – Loops

Topics  Loops FOR…NEXT DO…WHILE DO…UNTIL “Infinite”  Zip() Worksheet Functions  Unzip() 2

Loops  Loops are used to repeat portions of VBA code over and over again.  Loops can be thought of as a type of “Conditional Statement”.  Most loops can be classified under the following four categories: For Next Do While Do Until Infinite 3

FOR…NEXT Loop  Used when the exact number of cycles can be pre-determined. 4

FOR…NEXT Loop  Syntax: For counter = start To end 'Do something here Next counter  counter is a variable that identifies which step of the loop we are on.  start is the starting value of the counter.  end is the ending value of the counter. 5

FOR…NEXT Loop  Example 1: Roll a die 12 times keeping track of the total. Total = 0 For N = 1 To 12 Roll = Int(Rnd()*6)+1 Total = Total + Roll Next N 6

FOR…NEXT Loop  Example 2: Compute the sum of the first 15 positive even integers.  Two possible ways to do this in VBA! Total = 0 For N = 2 To 30 Step 2 Total = Total + N Next N Total = 0 For N = 1 To 15 Total = Total + 2*N Next N 7

DO…WHILE Loop  Used to continue looping while a condition is TRUE.  The condition is checked at the beginning of each cycle of the loop.  This type of loop can be used when the exact number of cycles cannot be pre-determined.  It is possible for this type of loop to perform zero cycles. 8

DO…WHILE Loop  Syntax: Do While (Expression) ‘Code to Execute Loop  (Expression) is any logical expression that evaluates to TRUE or FALSE. 9

DO…WHILE Loop  Example 3: Keep rolling a die while the total is less than 200, keeping track of the number of rolls required to accomplish this task.  Show in flowchart form. Num_Rolls = 0 Total = 0 Do While (Total < 200) Roll = Int(Rnd()*6)+1 Total = Total + Roll Num_Rolls = Num_Rolls + 1 Loop 10

DO…UNTIL Loop  Used to continue looping until a condition is TRUE.  The condition is checked at the end of each cycle of the loop.  This type of loop can also be used when the exact number of cycles cannot be pre-determined.  Since the condition is checked at the end of the cycle, this type of loop will always perform at least one cycle. 11

DO…UNTIL Loop  Syntax: Do ‘Code to Execute Loop Until (Expression)  (Expression) is any logical expression that evaluates to TRUE or FALSE. 12

DO…UNTIL Loop  Example 4: Keep rolling a die until the total exceeds 300, keeping track of the number of rolls required to accomplish this task.  Show in flowchart form. Num_Rolls = 0 Total = 0 Do Roll = Int(Rnd()*6)+1 Total = Total + Roll Num_Rolls = Num_Rolls + 1 Loop Until (Total>300) 13

“Infinite” Loop  Usually created by “ACCIDENT” but sometimes created on purpose.  Infinite loops can be dangerous.  To exit from an “infinite loop”, keep pressing the ESC key and “hope” that you saved the work that you have done.  Also try pressing CTRL+BREAK to stop an infinite loop.  The “tighter” the loop is, the harder it is to recover from. A “tight loop” heavily uses I/O or processing resources, failing to adequately share them with other programs running in the operating system. 14

“Infinite” Loop  Example 5: Create an infinite loop by accident by making a simple “logic error”.  Note: The DoEvents function surrenders execution of VBA code so that the operating system can process other events.  The DoEvents function passes control from the application to the operating system. Num_Rolls = 0 Sum = 0 Do Num_Rolls = Num_Rolls - 1 Roll = Int(Rnd()*6)+1 Sum = Sum + Roll DoEvents Loop Until (Num_Rolls > 15) 15

Putting it All Together!  The next two examples show how we can create a user defined function that combines conditional statements with loops! Zip() Unzip() 16

Zip()  Example 6: Create a user defined function Zip(String1,String2) that will combine two strings taking alternating characters from String1 and String2.  For example, Zip(“MNMIER!”,”YAESAL ”) should return the string “MYNAMEISEARL!” Function Zip(String1 As String, String2 As String) As String ‘combines two strings taking alternating characters from String1 and String2 Dim T As String, K As Integer, Ch1 As String, Ch2 As String T = “” For K = 1 To Max(Len(String1),Len(String2)) ‘get character from String1 if possible If K <= Len(String1) Then Ch1 = Mid(String1,K,1) Else Ch1 = “” End If ‘Get character from String2 if possible If K <= Len(String2) Then Ch2 = Mid(String2,K,1) Else Ch2 = “” End If ‘String Accumulator T = T + Ch1 + Ch2 Next K Zip = T End Function 17

Worksheet Functions  If an “built-in” Excel function does not coincide with a “built-in” VBA function, the Excel function can be used in VBA by calling the Excel function as a Worksheet Function.  In the VBA code for Zip(), the function “Max()” is undefined.  To get the Zip() user defined function to work, change “Max” to “Application.WorksheetFunction.Max”. 18

Unzip()  Example 2:Create a user defined function UnZip(String1,1) and UnZip(String1,2) that will return the odd numbered characters and even numbered characters of a string, String1, respectively.  For example, Unzip(“MYNAMEISEARL!”,1) should return the string “MNMIER!” and Unzip(“MYNAMEISEARL!”,2) should return the string “YAESAL” Function UnZip(String1 As String, N As String) As String ‘Returns the “odd” numbered characters from String1, if N=1 ‘Returns the “even” numbered characters from String1, if N=2 Dim T As String, Y As Integer, Ch As String T = “” Do While N <= Len(String1) T = T + Mid(String1,N,1) N = N + 2 Loop UnZip = T End Function 19

20 References  Loops Notes – John Albers  /vba-for-loop-for-next-and-for-each- in-next/ /vba-for-loop-for-next-and-for-each- in-next/  nding-do-and-while-loops nding-do-and-while-loops  TIGHT%20LOOP TIGHT%20LOOP  468/en-us 468/en-us