CSI 101 Elements of Computing Spring 2009

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
JavaScript, Third Edition
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
1 MATERI PENDUKUNG OPERATOR Matakuliah: M0074/PROGRAMMING II Tahun: 2005 Versi: 1/0.
CSI 101 Elements of Computing Spring 2009 Lecture #1 – From Math to Computers Wednesday, January 21, 2009.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Spreadsheets Objective 6.02
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
2440: 211 Interactive Web Programming Expressions & Operators.
Chapter 3: Data Types and Operators JavaScript - Introductory.
IMS 3253: Math 1 Dr. Lawrence West, MIS Dept., University of Central Florida Topics Five Fundamental Math Operations Precedence of Math.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Variables & Function Calls. Overview u Variables  Programmer Defined & Intrinsic  Data Types  Calculation issues u Using Functions  The val() function.
Assignment statement: Assigns a value to a variable Variable must appear on the left side, value on the right side of the assignment operator Right side.
Introduction to Programming Lecture Note - 2 Visual Basic Programming Fundamentals.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 2A Reading, Processing and Displaying Data (Concepts)
CIVIL AND GEOMATIC ENGINEERING FT Okyere. CIV 257- COMPUTER PROGRAMMING Lecture 3.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Chapter 4: Variables, Constants, and Arithmetic Operators Introduction to Programming with C++ Fourth Edition.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
CompSci 230 S Programming Techniques
Chapter Topics The Basics of a C++ Program Data Types
Arithmetic operations and operators, converting data types and formatting programs for output. Year 11 Information Technology.
Chapter 7: Expressions and Assignment Statements
Expressions.
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Basic Elements of C++.
Assignment statement:
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
The Selection Structure
Variables, Expressions, and IO
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Operators and Expressions
10 Real Numbers, Equations, and Inequalities.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Number and String Operations
Chapter 2: Basic Elements of Java
Numbers.
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Computing in COBOL: The Arithmetic Verbs and Intrinsic Functions
Microsoft Visual Basic 2005: Reloaded Second Edition
Spreadsheets 2 Explain advanced spreadsheet concepts and functions
Chapter 3: Selection Structures: Making Decisions
Spreadsheets Objective 6.02
Chapter 3: Selection Structures: Making Decisions
Spreadsheets Objective 6.02
Class code for pythonroom.com cchsp2cs
Arithmetic Expressions & Data Conversions
Presentation transcript:

CSI 101 Elements of Computing Spring 2009 Lecture #7 – Visual Basic Operations and Conditions Monday, February 23rd, 2009 thru Wednesday, February 25th, 2009

Visual Basic Operations How to get things done

Types of Operations Mathematical Operators Keywords Methods Symbols that represent various mathematical operations Keywords Reserved words in the Visual Basic language Perform specific actions Methods A specific function attached to an object

Mathematical Operations Many calculations in Visual Basic are mathematical Using a value contained in a variable, altering it somehow, and finishing with a result Big difference between math and Visual Basic The answer, or receiving variable, is ALWAYS to the left side of the equal sign Example: intCount = intCount + 1 This adds one to the value of variable intCount

Arithmetic Operators + - * / \ Mod ^ Note there is no symbol for roots Addition Subtraction Multiplication Division Integer Division (has no decimal/remainder) Modular division (returns remainder only) Exponentiation

Arithmetic Examples GPA = sumGrades / totHours dblAve = (int1 + int2 + int3) / 3 Sqr = intVal ^ 2 intDiff = A – B intRank = Total \ num 5 /4 is 1.25 5 \ 4 is 1 6 MOD 4 is 2

Order of Operations Uses same order as math: Parentheses (inside out) Exponentiation Multiplication or division (left to right) This includes integer division and modular division Addition and subtraction (left to right)

Comparison Operators Less than < Greater than > Equal to = Less than or equal to Greater than or equal to Not equal to Joins two conditions: True if BOTH conditions are true Negates current condition result Joins two conditions True if AT LEAST ONE of the conditions are true Joins two conditions: True if ONLY ONE of them is True < > = <= >= <> AND NOT OR XOR

Comparison Examples intCount < 10 intSum > 200 AND intSum < 400 (Note that 200 < intSum < 400 is NOT known to Visual Basic) boolCheck = True OR Length < 10

Special Operators & += -= *= /= &= Concatenate strings Increment Decrement Multiplication assignment Division assignment Add string to end

Special Operator Examples Start with intSample = 1 intSample += 2 sets intSample to 3 intSample -= 1 sets intSample to 2 intSample *= 3 sets intSample to 6 intSample /= 2 sets intSample to 3

Concatenation Examples txtA = “Test A”, txtB =“BB”, txtC = “C” txtC = txtA & txtB leaves A and B unchanged, txtC now is “TestABB” txtA &= txtB leaves B unchanged, txtA now has “Test ABB” txtC = txtA & “ “ & txtC leaves A unchanged, txtC now has “TestABB TestABB”

Conversion Operations How to change things

Why use conversion? Sometimes you have data of one data type (like string) and need to use it in a calculation as another type (like integer) Does not alter the variable, since it was declared as a particular type Allows the program to copy that value to a new variable of the desired data type

Convert from String Use the Parse method of a data type For instance, to change to Integer, use the Integer.Parse( ) method intValue = Integer.Parse(strValue) The following data types have Parse methods: Integer, Short, Long Decimal, Single, Double The following data types do not have Parse methods: Boolean Date

Convert To String All numeric types have a ToString method txtTest = intCount.ToString( ) Boolean variables do NOT have a ToString method

Formatting Options You can specify how you want the converted text value to look Specify option within parentheses of ToString method

Numeric Formats Currency Fixed Number Digits Percent Two decimal places, dollar sign preceding value, negative values enclosed in parentheses Variable decimal places, no comma separators, negative sign preceding negative values Fixed with comma separators Number with no decimals Multiplies value by 100, adds percent sign to end

Numeric Format Options C or c Fn or fn Nn or nn Dn or dn Pn or pn Currency Fixed with n decimal places Example: F3 has 3 decimal places If no digit specified, uses two decimal places Number with n decimal places Digits of n digits long Percent with n decimal places

Numeric Format Examples intA = 12 and decA = 6.45 decA.ToString(“C”) yields $6.45 decA.ToString(“F3”) yields 6.450 decA.ToString(“N”) yields 6.45 decA.ToString(“N1”) yields 6.5 intA.ToString(“D3”) yields 012 intA.ToString(“P”) yields 12.00 % intA.ToString(“P0”) yields 12 % Note that it rounds up if necessary

Condition Statements IF How to decide what to do

Decision statements Select one action or another Similar to the diamond-shaped box in flowcharts Either True or False (Yes or No) Condition evaluates to either True or False Uses comparison operators IF block contains condition(s)

IF statement forms Single-line: IF condition THEN action [ELSE action] If condition is True, then perform action following the THEN keyword The ELSE part is optional If condition is False, action following ELSE is performed Note that only one operation can follow the THEN or ELSE Example: IF intCount < 10 THEN intCount += 1 Explain use of square brackets to denote optional

IF statement forms, cont Multi-line: Used if more than one operation is performed if condition is True or False IF condition THEN <actions> ELSE END IF

ELSEIF statement Provides a different condition Performed if condition on IF was False Inserted before ELSE Now ELSE is run if BOTH conditions are False

ELSEIF format IF condition THEN <actions> ELSEIF new_condition THEN ELSE END IF

IF Example IF intCount < 10 THEN intSum += intNums(index) index += 1 intCount += 1 ELSE dblAve = intSum / 10 END IF

ElseIf Example IF index< limit THEN intSum += intNums(index) index += 1 intCount += 1 ELSEIF dblAve = 0 THEN dblAve = intSum / intCount ELSE END IF

Nested IF A multi-line IF block contained within another multi-line IF block Used to process actions dependent upon two independent conditions

Nested IF Example Find smallest of three values: IF A < B Then IF A < C Then Min = A Else Min = C ELSE IF B < C Then Min = B Else Min = C END IF

Condition Statements SELECT How to have lots to choose from

Select Case Statement Allows for multiple conditions Conditions are values, not comparisons that result in True or False Each condition can have multiple values Will perform actions for first case that is found true Never runs more than one, so whatever one is first wins

Select Case Format Select Case expression Case values <actions> : [Case Else] [<actions>] End Select

Case Value lists Single value List of values Bounded values Range of values Case 5 TO 10

Select Case Example Select Case intValue Case < 5 intValue += index index += 1 Case < 10 intValue += 2 Case < 20 intValue /= 2 End Select