Using loops and debugging code

Similar documents
VB Net Debugging (Console)

An Introduction to Python (TEJ3M & TEJ4M)

Chapter 2.6: Testing and running a solution

The for Loop. Lesson 11

We first learn one useful option of gcc. Copy the following C source file to your

Chapter 1 Getting Started

You will have mastered the material in this chapter when you can:

CSCI0330 Intro Computer Systems Doeppner. Lab 02 - Tools Lab. Due: Sunday, September 23, 2018 at 6:00 PM. 1 Introduction 0.

2 Getting Started. Getting Started (v1.8.6) 3/5/2007

7 The Integrated Debugger

Laboratory 5: Implementing Loops and Loop Control Strategies

LOOPS. Repetition using the while statement

CSCI Object Oriented Design: Java Review Errors George Blankenship. Java Review - Errors George Blankenship 1

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

CSCI Object-Oriented Design. Java Review Topics. Program Errors. George Blankenship 1. Java Review Errors George Blankenship

ME 142 Engineering Computation I. Debugging Techniques

Debugging in Small Basic is the process of analysing a program to detect and fix errors or improve functionality in some way.

Introduction. C provides two styles of flow control:

CS50 Supersection (for those less comfortable)

Under the Debug menu, there are two menu items for executing your code: the Start (F5) option and the

SECTION 5: STRUCTURED PROGRAMMING IN MATLAB. ENGR 112 Introduction to Engineering Computing

The second statement selects character number 1 from and assigns it to.

d2vbaref.doc Page 1 of 22 05/11/02 14:21

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

The NetBeans IDE is a big file --- a minimum of around 30 MB. After you have downloaded the file, simply execute the file to install the software.

Getting Started (1.8.7) 9/2/2009

KEYWORDS DDE GETOBJECT PATHNAME CLASS VB EDITOR WITHEVENTS HMI 1.0 TYPE LIBRARY HMI.TAG

LAB 6 Testing the ALU

Lab 8 - Vectors, and Debugging. Directions

The For Next and For Each Loops Explained for VBA & Excel

Debugging INTRODUCTION DEBUGGER WHAT IS VBA'S DEBUGGING ENVIRONMENT?

COPYRIGHTED MATERIAL. Starting Strong with Visual C# 2005 Express Edition

Dim there(2) As Double represents? been changed. You can access the elements of the array in any

CSCI 1100L: Topics in Computing Lab Lab 11: Programming with Scratch

The first program: Little Crab

Chapter 9: Dealing with Errors

Unit E Step-by-Step: Programming with Python

COMP1730/COMP6730 Programming for Scientists. Testing and Debugging.

Animations involving numbers

Chapter 5 Errors. Bjarne Stroustrup

17 CIF Converter Tools

Integrated Software Environment. Part 2

Using Visual Basic in Arc8 Raster Processing Form Example Matt Gregory and Michael Guzy

Map symbology and ArcCatalog. Chapter 15 Setting layer symbology. Chapter 16 Using ArcCatalog Objects in ArcMap

Sub Programs. To Solve a Problem, First Make It Simpler

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #17. Loops: Break Statement

Vba Variables Constant and Data types in Excel

C5500 Compiler Build Options One Use Case

C++ for Java Programmers

Compilation and Execution Simplifying Fractions. Loops If Statements. Variables Operations Using Functions Errors

Chapter 1: Getting Started

ACT-R RPC Interface Documentation. Working Draft Dan Bothell

Laboratory 1: Eclipse and Karel the Robot

Using Eclipse and Karel

After completing this appendix, you will be able to:

Step through Your DATA Step: Introducing the DATA Step Debugger in SAS Enterprise Guide

Reliable programming

the NXT-G programming environment

COMS 469: Interactive Media II

Introduction to IntelliJ

Chapter 3: The IF Function and Table Lookup

Chapter 4 Loops. int x = 0; while ( x <= 3 ) { x++; } System.out.println( x );

Computer Science Lab Exercise 2

Safe and Secure Software. Ada An Invitation to. Safe Syntax. Courtesy of. John Barnes. The GNAT Pro Company

RECURSION. Week 6 Laboratory for Introduction to Programming and Algorithms Uwe R. Zimmer based on material by James Barker. Pre-Laboratory Checklist

Iteration. Chapter 7. Prof. Mauro Gaspari: Mauro Gaspari - University of Bologna -

This chapter is intended to take you through the basic steps of using the Visual Basic

Visual Basic Program Coding STEP 2

5. Control Statements

Outline. Debugging. In Class Exercise Solution. Review If Else If. Immediate Program Errors. Function Test Example

Civil Engineering Computation

In this section we take an aside from the normal discussion in algebra.

Programming MS Excel in Visual Basic (VBA)

2 TUTORIAL. Overview. VisualDSP Getting Started Guide 2-1 for SHARC DSPs

CPS122 Lecture: From Python to Java last revised January 4, Objectives:

Control structure: Repetition - Part 2

Chapter 4 Introduction to Control Statements

THE JAVA FOR STATEMENT

If Statements, For Loops, Functions

int j = 0, sum = 0; for (j = 3; j <= 79; j++) { sum = sum + j; System.out.println(sum); //Show the progress as we iterate thru the loop.

Lab 7 Unit testing and debugging

CS61C Machine Structures. Lecture 4 C Pointers and Arrays. 1/25/2006 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

REPETITION CONTROL STRUCTURE LOGO

Visual Basic Course Pack

An overview about DroidBasic For Android

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #29 Arrays in C

ENGG1811 Computing for Engineers Week 9 Dialogues and Forms Numerical Integration

11 Using JUnit with jgrasp

Chapter 6 Control Flow. June 9, 2015

CS354 gdb Tutorial Written by Chris Feilbach

SAMLab Tip Sheet #1 Translating Mathematical Formulas Into Excel s Language

Eclipse User Guide Vacancy Templates. Vacancy Templates

Working with JavaScript

YOGYAKARTA STATE UNIVERSITY MATHEMATICS AND NATURAL SCIENCES FACULTY MATHEMATICS EDUCATION STUDY PROGRAM

Agenda. Peer Instruction Question 1. Peer Instruction Answer 1. Peer Instruction Question 2 6/22/2011

Learning to Program with Haiku

First, let s just try to run the program. When we click the button we get the error message shown below:

1.00 Lecture 2. What s an IDE?

Have the students look at the editor on their computers. Refer to overhead projector as necessary.

Test Results Schedule Miscellanea Control Structs. Add l Oper s Break Hands on Q & A Conclusion References Files

Transcription:

Using loops and debugging code Chapter 7 Looping your code pp. 103-118 Exercises 7A & 7B Chapter 8 Fixing Bugs pp. 119-132 Exercise 8

Chapter 7 Looping your code Coding a For loop Coding a Do loop

Chapter 7 Looping your code It is quite common to have software perform some task repeatedly, whether it is: Until it has been done for each member of a set, e.g. for each record in a shapefile, do the following Until some condition is satisfied, e.g. check the distance between a point of interest and all other points in a shapefile until one is found that is less than a specified threshold This repeated execution of a few lines of code is called looping, and VBA for ArcGIS provides coding structures for both of these situations: A For loop can be used to execute code a given number of times A Do loop can be used execute code until a specified condition is satisfied

Coding a For loop For loops begin with a line that specifies a variable that will keep track of its iterations: For variable = StartValue To EndValue (Step StepValue) The StartValue and EndValue specify the range over which the variable should be iterated, e.g. in a basic example: For i = 1 To 10 the loop will be executed 10 times, for each value between 1 and 10, with the value of i being increased by 1 on each iteration {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Coding a For loop Optionally, we can also use the Step keyword to change the increments, as we will in the exercise when we will use a For loop to populate some choices in a pulldown: For intyear = 1930 To 2000 (Step 10) {1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000}

Coding a For loop For loops end with a Next statement, which simply indicates where the body of the loop ends (the body of the loop being everything between the For and the Next) Usually, the For loop will run as many times as the iterator specifies that it should, but there is one other way to write code to exit a For loop: An Exit For statement can be placed in the body of the loop, usually within an If Then statement so that if a specified condition occurs, rather than completing the loop s usual number of iterations, we can jump straight to the Next statement This is a useful approach when we plan to search through a number of items (say, all the layers in a map), until we find the right one; once we find it, there is no need to look at the rest

Coding a For loop This is a useful approach when we plan to search through a number of items (say, all the layers in a map), until we find the right one; once we find it, there is no need to look at the rest: Dim pzmap as IMap Dim x as Integer For x = o to pmaps.count 1 If pmap.item(x).name = zoning Then set pzmap = pmaps.item(x) Exit For End If Next x

Coding a Do loop Do loops are used in the other situation, when you want some code repeated until some condition is satisfied This can take two forms: Do While Runs the loop while the specified expression is true Do Until Runs the loop while the specified expression is false Structurally, Do loops look a lot like For loops: They have an opening line, that in this case specifies the expression to be checked to see if the loop should run again: Do While Until Expression Rather than ending with a Next, they end with a Loop statement Loop You can use an Exit Do to leave the loop from within its body Exit Do

Coding a Do loop For example, suppose we need to move through all the layers in a map, but we do not know how many there are; we can use a Do loop like so: Layer enum example Dim player as ILayer Dim pmaplayers as IEnumLayer Set pmaplayers = pmap.layers Set player = pmaplayers.next Do Until player is Nothing msgbox player.name set player = pmaplayers.next Loop

Chapter 8 Fixing bugs Using the debug tools

Chapter 8 Fixing bugs Now that you have completed a few of the exercises, you have almost certainly had the experience of having your code not run properly, and having had ArcGIS greet you with an error message instead It is very easy, through small errors in syntax, to get into this situation and create code with a bug Fortunately, the ArcGIS VBA environment provides us with some tools to make it easier to identify and correct any bugs in our code First, though, it is worthwhile to identify three different kinds of errors that we might encounter, what their causes are, and what we can do about them

Chapter 8 Fixing bugs Compile Errors When the code we write is converted into the form that the computer will execute, this is called compiling We can distinguish between the code we can read (the VBA code) and the code the computer s processor can read (which is binary and called machine code or assembler language) As the VBA compiles, the software can detect when something doesn t quite make sense and the VBA cannot be compiled. Some common reasons this occurs: You make a syntax error by misspelling something You make an error by misusing VBA (forgetting an argument, not closing a loop, trying to use a method without an object) VBA will highlight where you made the error

Chapter 8 Fixing bugs Run-time Errors It is possible for your code to compile successfully, but still cause errors when you try to run it. These errors are known as run-time errors Unfortunately, these cannot be detected before the fact, because even though there is nothing wrong with the syntax, what your code asks the computer to do is impossible or prohibited in some way. Some common examples of this are: Illegal math, such as a divide by zero error (syntactically valid, but mathematically impossible, e.g. Acres = 40000 / 0) Type mismatch errors, where you try to use two kinds of objects together in a way that is not viable (e.g. a mathematical expression containing a string like Acres = SqFt / 43650)

Chapter 8 Fixing bugs Logic Errors It is possible for your code to compile successfully and run successfully, but when it does running, it does not produce the desired result. When this is the case, the programmer has usually committed an error known as a logic error Unfortunately, the software itself cannot detect a logic error: You, the programmer, have to know what your software is supposed to do, and when it doesn t do that, you have to be the one who figures out what went wrong The key to detecting logic errors is to test your code, usually thoroughly, trying to make it encounter every possible condition it is likely to encounter in regular use

Using the debug tools Regardless of which of the three types of errors you encounter, you can use the VBA Debug toolbar to help you figure out what is wrong, and correct the problem The key capability of the Debug toolbar is the ability to control the rate at which your code runs, so you can check and see what is going on: Using the Step Into button, you can run your code one line at a time until you see an error occur Using Breakpoints, you can allow the code to run up until a certain point, where you can have a closer look (very useful if you have a lot of lines of code, or loops with many iterations, such that stepping through every line would take a really long time)

Using the debug tools Other buttons on the Debug toolbar are useful: The Step Over button is similar to Step Into, but will successfully execute a procedure call (and run the procedure in its entirety) before returning to the next line The Step Out button will execute the remaining lines of the current procedure, and then stop after its closing line The Run Sub/Userform button is particularly important: It will proceed to run the rest of the code, up until a breakpoint is encountered (if there is one) Another key when debugging is checking on the values of variables at various points during code execution, either by hovering the mouse over them, or using the Locals Window to see the values of all local variables

Next Topic: Making objects and using interfaces