Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Similar documents
Learning the Language - V

Civil Engineering Computation

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Any Integer Can Be Written as a Fraction

Chapter 17. Fundamental Concepts Expressed in JavaScript

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

Language Basics. /* The NUMBER GAME - User tries to guess a number between 1 and 10 */ /* Generate a random number between 1 and 10 */

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

Bits, Words, and Integers

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Properties and Definitions

Text Input and Conditionals

Unit 3. Operators. School of Science and Technology INTRODUCTION

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Introduction to TURING

9. Elementary Algebraic and Transcendental Scalar Functions

REVIEW. The C++ Programming Language. CS 151 Review #2

Lecture 4 CSE July 1992

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Table of Laplace Transforms

T H E I N T E R A C T I V E S H E L L

Full file at C How to Program, 6/e Multiple Choice Test Bank

Ex: If you use a program to record sales, you will want to remember data:

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Programming Language 2 (PL2)

Lecture 2 Tao Wang 1

Midterms Save the Dates!

5. Excel Fundamentals

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

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Decisions, Decisions. Testing, testing C H A P T E R 7

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

Control Structures. Code can be purely arithmetic assignments. At some point we will need some kind of control or decision making process to occur

Arithmetic Expressions in C

LOOPS. Repetition using the while statement

Our Strategy for Learning Fortran 90

Chapter 1 Operations With Numbers

Excel Basics Fall 2016

Computer Programming CS F111

Memory Addressing, Binary, and Hexadecimal Review

age = 23 age = age + 1 data types Integers Floating-point numbers Strings Booleans loosely typed age = In my 20s

Unit 6 - Software Design and Development LESSON 3 KEY FEATURES

QUIZ: What value is stored in a after this

Section 1.1 Definitions and Properties

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

Operators. Java operators are classified into three categories:

Introduction to FORTRAN

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Creating a C++ Program

Basics of Java Programming

(Refer Slide Time: 00:26)

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

These are notes for the third lecture; if statements and loops.

C: How to Program. Week /Mar/05

Intro. Scheme Basics. scm> 5 5. scm>

Chapter 2, Part III Arithmetic Operators and Decision Making

CS 115 Data Types and Arithmetic; Testing. Taken from notes by Dr. Neil Moore

Ch. 12: Operator Overloading

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

Lecture 3 Tao Wang 1

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Excel Basics Rice Digital Media Commons Guide Written for Microsoft Excel 2010 Windows Edition by Eric Miller

ENGR 101 Engineering Design Workshop

Part II Composition of Functions

CS102: Variables and Expressions

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Programming with Python

If Statements, For Loops, Functions

Learning Language. Reference Manual. George Liao (gkl2104) Joseanibal Colon Ramos (jc2373) Stephen Robinson (sar2120) Huabiao Xu(hx2104)

The PCAT Programming Language Reference Manual

COMP Primitive and Class Types. Yi Hong May 14, 2015

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Chapter 2 - Introduction to C Programming

Introduction to the C++ Programming Language

Problem Solving for Intro to Computer Science

(Refer Slide Time: 02:59)

Information Science 1

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #23 Loops: Precedence of Operators

The compiler is spewing error messages.

CHAD Language Reference Manual

Programming for Engineers Introduction to C

Math 25 and Maple 3 + 4;

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Decision Control Structure. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

Project 3: RPN Calculator

CMSC201 Computer Science I for Majors

Divisibility Rules and Their Explanations

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Rule 1-3: Use white space to break a function into paragraphs. Rule 1-5: Avoid very long statements. Use multiple shorter statements instead.

4. Java Project Design, Input Methods

Chapter 3: Operators, Expressions and Type Conversion

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

CS112 Lecture: Variables, Expressions, Computation, Constants, Numeric Input-Output

UNIT- 3 Introduction to C++

This tutorial will teach you about operators. Operators are symbols that are used to represent an actions used in programming.

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

Transcription:

Fundamentals We build up instructions from three types of materials Constants Expressions Fundamentals Constants are just that, they are values that don t change as our macros are executing Fundamentals are names assigned to locations in computer memory where we want to store things that may change are the macro executes Think of them as named mailboxes 1

Fundamentals Expressions or statements are the instructions that tell the computer or EXCEL just what to do Fundamentals As a rule in this class, you will always start your code with your name, the class, the date, and the assignment number For example There really aren t that many kinds of things that we deal with so keeping them straight shouldn t be much of a problem Is you get confused, check the help menu for data types Boolean This is a data type that is use to store LOGICAL values True or False Nothing else 2

Integer This one is important and used lots An integer number is just a whole number No decimal places Notice the range of values, if you go beyond that range, you have to use another type -32,768 to 32,767 Integer There are no decimal places in an integer number and arithmetic with integers can lead to some interesting results because of the lack of decimal places Long If you need an integer to go beyond the range provided by the INTEGER type, you can use the LONG type This is still an integer but it has a range of + or over 2 Billion Single This is the first of the real number types It includes decimal fractions and will store ranges from +/- 3.4 x 10 38 down to +/- 1.2 x 10-45 3

Double If you need more precision than what is provided by the SINGLE type, you can use the DOUBLE type It ranges out from 10 308 to 10-308 String The only other data type we are going to worry about right now is the STRING data type This is used to hold strings made up of letters, blanks, numbers, and symbols Constants Constants come in each type A number or string that won t change in the program is a constant The syntax of the statement is Dim variablename as type variablename is the name of the location where you want to store the information 4

The syntax of the statement is Dim variablename as type type is the type of information that you want to store in the named location So the statement Dim Radius As Single Sets us a storage location named Radius to hold a single precision real number VBA allows us to dimension (type) multiple variables on the same line but you have to be careful here Each variable name must be typed independently and separated by a comma from the other types 5

Three common mistakes Dim Radius, Height, Volume as Single Dim Radius as Single, Dim Height as Single Dim as Single, Radius, Height, Volume Every cell (as well as groups of cells, charts, and other things) is actually an object in EXCEL and so can be treated as an object in VBA In this case, we are using what is known as a Range object We are selecting the cells from A1 to F17 inclusive as our object We do the selection by using the.select Method Methods are just special ways of handling objects and their properties 6

We are able to set up variables to hold objects by typing the variable according to what type of object the variable will hold For example to set up a range object we would use the specification statement Dim WhatCells as Range Assignment statements An assignment statement reads Place what the right side of the expression evaluates to into the location on the left side of the expression The left side and right side are separated by an equals sign (=) Note that the right side is evaluated and then placed into the left side The left side is just a location Assignment statements For example the assignment statement X = 1. Would evaluate the right side A constant evaluates to itself Then since there is nothing else to evaluate it would store the result in the memory location labeled X Assignment statements Since we have an evaluation, then a replacement, a statement like X = X + 1. Is completely proper and meaningful The computer looks at the right side first It gets the value stored in X Adds 1 to it Store the result in X 7

Assignment statements If X had 21 originally stored in it and the expression X = X + 1 Was executed, after the execution of the expression, X would contain 22 Since X is a variable, it can change values during the execution of the program Order of Operations For ARITHMETIC evaluations, VBA has a number of operators - (uniary minus) ^ exponentiation * and / multiply and divide + and add and subtract Order of Operations All of the operators beside the first take two arguments The uniary minus only take one argument - (uniary minus) ^ exponentiation * and / multiply and divide + and add and subtract Order of Operations The order in which arithmetic expression are evaluated are Uniary Minus Exponentiation Multiplication and Division Addition and Subtraction 8

Order of Operations For operators of equal precedence, they are evaluated in left to right order Uniary Minus Exponentiation Multiplication and Division Addition and Subtraction Order of Operations We have a way to circumvent the normal arithmetic order of operations is we choose to do so Anything that we enclose in parentheses will be evaluated first This order goes from inside to out Order of Operations Parentheses allow us to control the order of evaluation of an arithmetic expression Parentheses are evaluated from the inside out when they are nested For every open parenthesis ( there must be a closed parenthesis ) Building a macro And finally we write the modified value back in to the call A1 9

The Next Step For example, if we wanted to look at what was in a cell and decide how to treat it based on if it was even or odd (integers only) If the number is even, divide it by 2 and display the result in red If the number is odd, add 1 to it then divide by 2 and display the result in blue Making a Decision We need a way to make a decision based on what the value we just stored in checkvar Making a Decision Making a Decision expression evaluation to either or, not to a number. This is critical to know the difference because Visual Basic sometimes tries to make up for your mistakes with tragic results. There are actually two types of operators that are used in logical expressions: Relational Operators Operators 10

Making a Decision Making a Decision Relational operators are based on the relation ship of one argument to another Is X larger than Y? Is 1.2 not equal to Z? In both cases, we could answer or to the proposed relation. If X = 1, Y = 3, and Z = 5 then Is X larger than Y? False Is 1.2 not equal to Z? True Making a Decision While we can store the evaluation of a relational expression int o a Boolean variable, what we usually do is use the results of the evaluation of the expression to control execution of the program. We do this by using what is known as an IF-THEN-ELSE-ENDIF construct. 11

The diagram is known as a flow chart and represents what action the program takes as it flows through execution. You can think of it as moving along one way streets, from top to bottom, as the program executes. In this case we get to the decision, based on the decision we can either go down the right or left path, and then eventually they converge and we go on. In our case, the logical condition will be the evaluation of a relational expression Relation expressions are made up of two arguments connected by a relational operator very similar to arithmetic expressions which have an arithmetic operator and two arguments The relational operators are comparison operators and always evaluate to a logical value (TRUE or FALSE) The relational operators are = equal to <> Unequal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to 12

Here is where confusion can creep in. Now we are using the equals sign as a relational operator which is comparing the value of two arguments. = equal to <> Unequal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to Previously, we utilized it as a an assignment operation to replace the left side with the evaluation of the right side Here we use it to give the logical value based on the equality of its arguments = equal to <> Unequal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to Since we almost always use these relational operators within the context of process control, the context will determine which equal sign meaning we have It isn t as bad as it seems if you are careful = equal to <> Unequal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to In our problem, we need to decide if a number is even or not An even number is evenly divisible by 2 so we need to incorporate that into our programming We can do this by taking advantage of integer arithmetic 13

Remember that integers have no decimal part and that we actually set up places in memory, variables, to specifically hold integers If we took 3. and divided it by 2 we would get 1.5 If we then tried to store the 1.5 into an integer, we would store a result that we are not sure of If we run this code, we get 2 in our selected cell Luckily, Visual Basic has a function which allows us to divide two integers by each other and gives us the remainder The function is know as MOD and takes two arguments Notice that we use it is the same way we used binary arithmetic operators Argument Operator Argument Also, like other arithmetic operators, it evaluates to a number In this case, it is the remainder from the division of one integer divided by another You may have noticed that one of the arguments isn t an integer Visual Basic made it one for this expression Argument Operator Argument If you look for the Mod operator in the help section you will get the result shown here so we will know how to use the operator in our expression Any even number divided by 2 will have 0 as a remainder so all we have to test is what the Mod operator returns from our number and 2 14

No we can begin to put all of this together First we need to put in the decision structure IF-THEN-ENDIF What we have done is to build the paths we saw earlier We start with If then an expression that evaluates to a logical value (True or False) We then provide instructions to execute if the logical expression evaluates to and another set of instruction to evaluate if the logical expression is It will always be one or the other, never both The form of the statement is important I indent to know just where I am and to show that I know that there are two branches Now we can add the code to do just what we originally set out to do. In this code we have done a number of things We check to see if the value we captured from the cell is evenly divisible by 2 We make a calculation based on that check and we change the property of the selected cell We display the results of our calculations in the cell 15

Homework The current calendar, call the Gregorian calendar, was introduced in 1582. Every year divisible by 4 was declared to be a leap year, with the exception of years ending in 00 and not divisible by 400. Write a macro that gets the year in four digits from a cell and in the adjacent cell (to the right) writes the number of days in that year. - DUE WED 21 January 2004 16