VB FUNCTIONS AND OPERATORS

Size: px
Start display at page:

Download "VB FUNCTIONS AND OPERATORS"

Transcription

1 VB FUNCTIONS AND OPERATORS In der to compute inputs from users and generate results, we need to use various mathematical operats. In Visual Basic, other than the addition (+) and subtraction (-), the symbols f the operats are different from nmal mathematical operats, as shown in the table below: OPERATOR MATHEMATICAL FUNCTION EXAMPLE ^ Exponents 2 ^ 4 = 16 * Multiplication 4 * 3 = 12 / Division 12 / 4 = 3 + Addition = 12 - Subtraction 7 5 = 2 Mod Modulus (returns the remainder from an integer division) 15 Mod 4 = 3 \ Integer Division (discards the decimal places) 19 \ 4 = 4 + & String concatenation ICS & 2O1 = ICS2O1 ORDER OF OPERATIONS Visual Basic evaluates a numeric expression using a specific der of operations, operat precedence. The following table outlines the der of operations used in Visual Basic: ORDER OPERATOR FUNCTION 1 st ^ Exponents 2 nd +, - Unary 3 rd *, / Multiplication and Division 4 th \ Integer Division 5 th Mod Modulus 6 th +, - Addition and Subtraction VB Functions and Operats Page 1 of 11

2 Operat precedence can, of course, be changed by including parentheses in a numeric expression. The operations within parentheses are evaluated first. COMBINED ASSIGNMENT OPERATORS Sometimes you might find it necessary to change the value of a variable by doubling it subtracting a certain amount from it while keeping the result in the variable itself. Combined assignment operats (sometimes called compound operats) are designed to do just that. Without compound operats, you would have to type the variable name twice once f the left ption of the equation and again in the right ption of the equation. The following table outlines the compound operats available in Visual Basic: COMPOUND OPERATOR EXAMPLE EQUIVALENT += x += 5 x = x + 5 -= x -= 5 x = x - 5 *= x *= 5 x = x * 5 /= x /= 5 x = x / 5 \= x \= 5 x = x \ 5 &= name &= lastname name = name & lastname VB Functions and Operats Page 2 of 11

3 IMPLICIT TYPE CONVERSION When you assign a value to one date type to a variable of another data type, Visual Basic attempts to convert the value being assigned to the data type of the receiving variable. This is known as implicit type conversion. F example, let s say we want to assign the integer 5 to a variable of type Single named num: Dim num As Single = 5 When the statement executes, the integer 5 is automatically converted into the real number 5.0, which is then sted in the variable num. This conversion is what s called a widening conversion because no data is lost. NARROWING CONVERSIONS If you assign a real number to an integer variable, Visual Basic attempts to perfm what s called a narrowing conversion. Oftentimes, some data is lost. F example, the following statement assigns 12.2 to an integer variable: Dim num As Integer = 12.2 The value 12.2 is rounded down to 12. Similarly, the next statement rounds up to the nearest integer (i.e. 13) when the fractional part of the number is greater than.5: Dim num As Integer = 12.6 Another narrowing conversion occurs when assigning a Double value to a variable type Single. Both hold floating-point values, but Double permits me significant digits: Dim numone As Double = Dim numtwo As Single = numone The value sted in numtwo is rounded up to because variables of Single type can only hold seven significant digits. CONVERTING STRINGS TO NUMBERS Under some circumstances, Visual Basic will try to convert string values to numbers. In the following statement, f example, 12.2 is a string containing a numeric expression: Dim num As String = 12.2 VB Functions and Operats Page 3 of 11

4 The string 12.2 is a string of characters in a numeric-like fmat that cannot be used f calculations. When the following statements execute, the string 12.2 is converted to the number 12.2: Dim num As Single num = 12.2 If we assign the string 12.2 to an Integer variable, the result is rounded down to 12: Dim num As Integer num = 12.2 A similar effect occurs when the user enters a number into a TextBox control. We usually want to assign the contents of the text box s Text property to a numeric value. The Text property is by definition type String, so its contents are implicitly converted to a number when we assign it to a numeric variable: Dim num As Integer num = txtvalue.text OPTION STRICT Visual Basic has a configuration option named Option Strict that determines whether certain implicit conversions are legal. If you set Option Strict to ON, only widening conversions are permitted (such as Integer to Single). The following diagram shows how implicit conversion between numeric types must be in a left-to-right direction: Byte Integer Long Decimal Single Double A Single value can be assigned to a variable of type Double, an Integer can be assigned to a variable of type Single, etc. If, on the other hand, Option Strict is set to OFF, all types of numeric conversions are permitted, with possible loss of data. Option Strict can be set in two different ways: 1. By inserting an Option Strict statement at the top of the source code file: Option Strict On 2. Right-click the project name in the Solution Expler window, select Properties, and then select the Compile tab. From the Option Strict drop-down list, you can select ON OFF. VB Functions and Operats Page 4 of 11

5 Click COMPILE Click ON OFF It is recommended that you use Option Strict to catch errs that result when you accidentally assign a value of the wrong type to a variable. When set to ON, Option Strict fces you to use a conversion function, making your intentions clear, and helps to avoid runtime errs. EXPLICIT TYPE CONVERSIONS The following table outlines the most commonly used Visual Basic conversion functions. The input to each of the conversion functions is an expression, which is another name f a variable name, an arithmetic expression, etc. These conversion functions are required when Option Strict is set to ON and you need to assign a wider numeric type to a narrower numeric type (e.g. Long to Integer, Double to Single, etc.) FUNCTION CChar(expr) Convert.ToChar(expr) CBool(expr) Convert.ToBoolean(expr) CDbl(expr) Convert.ToDouble(expr) CInt(expr) Convert.ToInt32(expr) CLng(expr) Convert.ToInt64(expr) DESCRIPTION Converts a string expression to a Char. If the string contains me than one character, only the first character is returned. Converts a string expression (that must equal True False) to a Boolean. If the expression does not equal True, False Nothing, a runtime err is generated. Converts a numeric string expression to a Double. If the expression converts to a value outside the range of a Double, is not a numeric value, a runtime err is generated. Converts a numeric string expression to an Integer. If the expression converts to a value outside the range of an Integer, is not a numeric value, a runtime err is generated. Converts a numeric string expression to a Long. If the expression converts to a value outside the range of a Long, is not a numeric value, a runtime err is generated. CSng(expr) Converts a numeric string expression to a Single. VB Functions and Operats Page 5 of 11

6 Convert.ToSingle(expr) CStr(expr) Convert.ToString(expr) If the expression converts to a value outside the range of a Single, is not a numeric value, a runtime err is generated. Converts a numeric, Boolean, Date String expression to a String. THE ToString() METHOD Each Visual Basic data type has a ToString() method, which returns a string representation of the variable calling the method. You call the ToString() method using the following general fmat: VariableName.ToString() The following code segment shows an example of the method s use: Dim num As Integer = 123 lblnumber.text = num.tostring() VB Functions and Operats Page 6 of 11

7 Numbers may be fmatted in various ways f output. Visual Basic provides a number of fmatting functions that allow you to convert values to strings and fmat their appearances. THE FmatNumber FUNCTION By default the FmatNumber function fmats a number to two decimal places and includes commas. F example, the number 1999 would be fmatted as 1, The number would be fmatted as 123,456, The following code is an example of how the FmatNumber function is used: Dim num As Double num = lblmessage.text = FmatNumber(num) The last statement passes the variable num to the FmatNumber function and returns the string 7,823.28, which is assigned to the lblmessage.text property. The FmatNumber function takes an optional second argument, which specifies the number of decimal places. The following example fmats a variable called laptime to four (4) decimal places: lblmessage.text = FmatNumber(lapTime, 4) THE FmatCurrency FUNCTION The FmatCurrency function fmats a number as currency (e.g. $5.85). The number is rounded to two (2) decimal places and includes commas as needed. The following is an example of how the FmatCurrency function is used: Dim totalcost As Double totalcost = lbltotal.text = FmatCurrency(totalCost) The last statement passes the variable totalcost to the FmatCurrency function and returns the string $45,123.98, which is assigned to the lbltotal.text property. THE FmatPercent FUNCTION The FmatPercent function fmats a number as a percent by multiplying the argument by 100, rounding the result to two decimal places, and adding a percent symbol. The following is an example of how the FmatPercent function is used: VB Functions and Operats Page 7 of 11

8 Dim average As Double average = lblaverage.text = FmatPercent(average) The last statement passes the variable average to the FmatPercent function and returns the string 56.79%, which is assigned to the lblaverage.text property. Like FmatNumber and FmatCurrency, the FmatPercent function takes an optional second argument, which specifies the number of decimal places. The following example fmats the variable called average to zero (0) decimal places: lblaverage.text = FmatPercent(average, 0) VB Functions and Operats Page 8 of 11

9 Games, simulats, screen savers, and many other types of applications require random numbers. Visual Basic includes a Random class that generates a random number. In der to use the Random class, the first thing you need to do is create a new Random object as follows: 'Declare and initialize Random object Dim rnd As New Random The next thing you ll need to do is declare a variable that will ste the number that is randomly generated by the Random object that we created above: 'Declare and initialize variable to ste random number Dim randomnumber As Integer To generate a random number, we will need to use the Next() method included in the Random class which generates a positive integer value greater than equal to zero and less than 2,147,483,647. 'Generate and ste random number randomnumber = rnd.next() Let s now add a user interface to see how this program would wk. Create a fm and add a label and button as follows: lblnumber btngenerate Add the following code to the button: Private Sub btngenerate_click(byval sender As System.Object, ByVal e As System.EventArgs) Handles btngenerate.click 'Declare and initialize Random object Dim rnd As New Random 'Declare and initialize variable to ste random number VB Functions and Operats Page 9 of 11

10 End Sub Dim randomnumber As Integer 'Randomly generate and ste random number randomnumber = rnd.next() 'Output and fmat the random number lblnumber.text = FmatNumber(randomNumber, 0,,, TriState.True) When you run the program, and click the GENERATE button, a random number is outputted in the label. SPECIFYING A RANGE OF NUMBERS We can specify a range of numbers by passing a single argument to the Next() method. F example, if I wanted to generate a random number between 0 and 5, I would use the Next() method as follows: randomnumber = rnd.next(6) When a single argument is passed to the Next() method, the values returned by Next() will be in the range from 0 to (but not including) the value of that argument. So, in the above example, the program will generate one of six possible numbers in the range 0-5. Let s say I wanted to generate a random number between 1 and 6. In this case, there are two ways we can do this. The first is simply pass a single argument to the Next() method to represent the amount of possible values the program will generate and simply add the minimum value to the equation, as follows: randomnumber = rnd.next(6) + 1 Alternatively, you can produce the same result by passing two arguments to the Next() method: the first representing the minimum value in our range and the second representing the maximum value + 1. randomnumber = rnd.next(1, 7) VB Functions and Operats Page 10 of 11

11 GENERATING FLOATING-POINT VALUES The Random() class includes a NextDouble() method to generate a random floating-point value greater than equal to 0 and less than 1. randomnumber = rnd.nextdouble() To specify a range of values, such as , we will need to multiply the result of NextDouble() by the amount of possible integer values - 1 (i.e. 10 1) and then add the minimum value, as follows: randomnumber = rnd.nextdouble() * VB Functions and Operats Page 11 of 11

Programming Language 2 (PL2)

Programming Language 2 (PL2) Programming Language 2 (PL2) 337.1.1 - Explain rules for constructing various variable types of language 337.1.2 Identify the use of arithmetical and logical operators 337.1.3 Explain the rules of language

More information

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1 Topics 1. Expressions 2. Operator precedence 3. Shorthand operators 4. Data/Type Conversion 1/15/19 CSE 1321 MODULE 2 2 Expressions

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

More information

Unit 4. Lesson 4.1. Managing Data. Data types. Introduction. Data type. Visual Basic 2008 Data types

Unit 4. Lesson 4.1. Managing Data. Data types. Introduction. Data type. Visual Basic 2008 Data types Managing Data Unit 4 Managing Data Introduction Lesson 4.1 Data types We come across many types of information and data in our daily life. For example, we need to handle data such as name, address, money,

More information

Microsoft Visual Basic 2005: Reloaded

Microsoft Visual Basic 2005: Reloaded Microsoft Visual Basic 2005: Reloaded Second Edition Chapter 3 Variables, Constants, Methods, and Calculations Objectives After studying this chapter, you should be able to: Declare variables and named

More information

Full file at

Full file at Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

ECE 122 Engineering Problem Solving with Java

ECE 122 Engineering Problem Solving with Java ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction Outline Problem: How do I input data and use it in complicated expressions Creating complicated expressions

More information

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

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4. Introduction to Visual Basic and Visual C++ Arithmetic Expression Lesson 4 Calculation I154-1-A A @ Peter Lo 2010 1 I154-1-A A @ Peter Lo 2010 2 Arithmetic Expression Using Arithmetic Expression Calculations

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

Microsoft Visual Basic 2015: Reloaded

Microsoft Visual Basic 2015: Reloaded Microsoft Visual Basic 2015: Reloaded Sixth Edition Chapter Three Memory Locations and Calculations Objectives After studying this chapter, you should be able to: Declare variables and named constants

More information

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

Arithmetic Expressions in C

Arithmetic Expressions in C Arithmetic Expressions in C Arithmetic Expressions consist of numeric literals, arithmetic operators, and numeric variables. They simplify to a single value, when evaluated. Here is an example of an arithmetic

More information

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

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1 CPE 101 mod/reusing slides from a UW course Overview (4) Lecture 4: Arithmetic Expressions Arithmetic expressions Integer and floating-point (double) types Unary and binary operators Precedence Associativity

More information

Programming in C++ 6. Floating point data types

Programming in C++ 6. Floating point data types Programming in C++ 6. Floating point data types! Introduction! Type double! Type float! Changing types! Type promotion & conversion! Casts! Initialization! Assignment operators! Summary 1 Introduction

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University January 15, 2015 Chapter 2: Data and Expressions CS 121 1 / 1 Chapter 2 Part 1: Data

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data Declaring Variables Constant Cannot be changed after a program is compiled Variable A named location in computer memory that can hold different values at different points in time

More information

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands Performing Computations C provides operators that can be applied to calculate expressions: tax is 8.5% of the total sale expression: tax = 0.085 * totalsale Need to specify what operations are legal, how

More information

Outline. Data and Operations. Data Types. Integral Types

Outline. Data and Operations. Data Types. Integral Types Outline Data and Operations Data Types Arithmetic Operations Strings Variables Declaration Statements Named Constant Assignment Statements Intrinsic (Built-in) Functions Data and Operations Data and Operations

More information

SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS

SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS Add your company slogan SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS Computer Programming (YPG) LOGO 306.1 Review Selected Programming Environment 306.1.1 Explain the concept of reserve words,

More information

Number Representation & Conversion

Number Representation & Conversion Number Representation & Conversion Chapter 4 Under the covers of numbers in Java 1 How (Unsigned) Integers Work Base 10 Decimal (People) Base 2 Binary (Computer) 10 2 10 1 10 0 2 3 4 2 7 2 6 2 5 2 4 2

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual:

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. A Guide to this Instructor s Manual: Java Programming, Eighth Edition 2-1 Chapter 2 Using Data A Guide to this Instructor s Manual: We have designed this Instructor s Manual to supplement and enhance your teaching experience through classroom

More information

Values, Variables, Types & Arithmetic Expressions. Agenda

Values, Variables, Types & Arithmetic Expressions. Agenda Values, Variables, Types & Arithmetic Expressions Lecture 2 Object-Oriented Programming Agenda Inside of a Computer Value Variable Data Types in Java Literals Identifiers Type conversions Manipulating

More information

MODULE 02: BASIC COMPUTATION IN JAVA

MODULE 02: BASIC COMPUTATION IN JAVA MODULE 02: BASIC COMPUTATION IN JAVA Outline Variables Naming Conventions Data Types Primitive Data Types Review: int, double New: boolean, char The String Class Type Conversion Expressions Assignment

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University April 21, 2015 Chapter 2: Data and Expressions CS 121 1 / 53 Chapter 2 Part 1: Data Types

More information

Java Primer 1: Types, Classes and Operators

Java Primer 1: Types, Classes and Operators Java Primer 1 3/18/14 Presentation for use with the textbook Data Structures and Algorithms in Java, 6th edition, by M. T. Goodrich, R. Tamassia, and M. H. Goldwasser, Wiley, 2014 Java Primer 1: Types,

More information

Information Science 1

Information Science 1 Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

More information

Lab 3 The High-Low Game

Lab 3 The High-Low Game Lab 3 The High-Low Game LAB GOALS To develop a simple windows-based game named High-Low using VB.Net. You will use: Buttons, Textboxes, Labels, Dim, integer, arithmetic operations, conditionals [if-then-else],

More information

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

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operators Overview Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators Operands and Operators Mathematical or logical relationships

More information

Chapter 02: Using Data

Chapter 02: Using Data True / False 1. A variable can hold more than one value at a time. ANSWER: False REFERENCES: 54 2. The int data type is the most commonly used integer type. ANSWER: True REFERENCES: 64 3. Multiplication,

More information

Unit 3. Operators. School of Science and Technology INTRODUCTION

Unit 3. Operators. School of Science and Technology INTRODUCTION INTRODUCTION Operators Unit 3 In the previous units (unit 1 and 2) you have learned about the basics of computer programming, different data types, constants, keywords and basic structure of a C program.

More information

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI CSCI 2010 Principles of Computer Science Data and Expressions 08/09/2013 CSCI 2010 1 Data Types, Variables and Expressions in Java We look at the primitive data types, strings and expressions that are

More information

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a) Operators Representing and storing primitive data types is, of course, essential for any computer language. But, so, too, is the ability to perform operations on data. Java supports a comprehensive set

More information

CS313D: ADVANCED PROGRAMMING LANGUAGE

CS313D: ADVANCED PROGRAMMING LANGUAGE CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

More information

Programming Using C Homework 4

Programming Using C Homework 4 Programming Using C Homewk 4 1. In Homewk 3 you computed the histogram of an array, in which each entry represents one value of the array. We can generalize the histogram so that each entry, called a bin,

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 1. The built-in value types (continued)

Murach s Visual Basic 2012, C4 2013, Mike Murach & Associates, Inc. Slide 1. The built-in value types (continued) The built-in value types Keyword Bytes Type Description Byte 1 Byte Positive integer value from 0 to 255 SByte 1 SByte Signed integer value from -128 to 127 Short 2 Int16 Integer from 32,768 to +32,767

More information

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2 Java Foundations Introduction to Program Design and Data Structures 4th Edition Lewis TEST BANK Full download at : https://testbankreal.com/download/java-foundations-introduction-toprogram-design-and-data-structures-4th-edition-lewis-test-bank/

More information

Copyright 2014 Pearson Education, Inc. Chapter 3. Variables and Calculations. Copyright 2014 Pearson Education, Inc.

Copyright 2014 Pearson Education, Inc. Chapter 3. Variables and Calculations. Copyright 2014 Pearson Education, Inc. Chapter 3 Variables and Calculations Topics 3.1 Gathering Text Input 3.2 Variables and Data Types 3.3 Performing Calculations 3.4 Mixing Different Data Types 3.5 Formatting Numbers and Dates 3.6 Class-Level

More information

More Programming Constructs -- Introduction

More Programming Constructs -- Introduction More Programming Constructs -- Introduction We can now examine some additional programming concepts and constructs Chapter 5 focuses on: internal data representation conversions between one data type and

More information

Zheng-Liang Lu Java Programming 45 / 79

Zheng-Liang Lu Java Programming 45 / 79 1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius

More information

Type Conversion. and. Statements

Type Conversion. and. Statements and Statements Type conversion changing a value from one type to another Void Integral Floating Point Derived Boolean Character Integer Real Imaginary Complex no fractional part fractional part 2 tj Suppose

More information

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence Data and Variables Data Types Expressions Operators Precedence String Concatenation Variables Declaration Assignment Shorthand operators Review class All code in a java file is written in a class public

More information

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on: Data and Expressions Data and Expressions Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration And Use Of Variables Expressions

More information

Information Science 1

Information Science 1 Topics covered Information Science 1 Terms and concepts from Week 8 Simple calculations Documenting programs Simple Calcula,ons Expressions Arithmetic operators and arithmetic operator precedence Mixed-type

More information

Data and Operations. Outline

Data and Operations. Outline Data and Operations Data and Operations 1 Outline Data Types Arithmetic Operations Strings Variables Declaration Statements Named Constant Assignment Statements Intrinsic (Built-in) Functions Data and

More information

3.1. Chapter. CSC 252 (MIS 385) Chapter 3 Input, Variables, Exceptions, and Calculations. Introduction. Page 1

3.1. Chapter. CSC 252 (MIS 385) Chapter 3 Input, Variables, Exceptions, and Calculations. Introduction. Page 1 Chapter 3 Input, Variables, Constants, And Calculations Slide 3-1 Introduction This chapter covers the use of text boxes to gather input from users It also discusses the use of variables named constants

More information

Programming Lecture 3

Programming Lecture 3 Programming Lecture 3 Expressions (Chapter 3) Primitive types Aside: Context Free Grammars Constants, variables Identifiers Variable declarations Arithmetic expressions Operator precedence Assignment statements

More information

Informatics Ingeniería en Electrónica y Automática Industrial

Informatics Ingeniería en Electrónica y Automática Industrial Informatics Ingeniería en Electrónica y Automática Industrial Operators and expressions in C Operators and expressions in C Numerical expressions and operators Arithmetical operators Relational and logical

More information

Topic 2: Introduction to Programming

Topic 2: Introduction to Programming Topic 2: Introduction to Programming 1 Textbook Strongly Recommended Exercises The Python Workbook: 12, 13, 23, and 28 Recommended Exercises The Python Workbook: 5, 7, 15, 21, 22 and 31 Recommended Reading

More information

Learning the Language - V

Learning the Language - V Learning the Language - V Fundamentals We now have locations to store things so we need a way to get things into those storage locations To do that, we use assignment statements Deja Moo: The feeling that

More information

1.3b Type Conversion

1.3b Type Conversion 1.3b Type Conversion Type Conversion When we write expressions involved data that involves two different data types, such as multiplying an integer and floating point number, we need to perform a type

More information

Reserved Words and Identifiers

Reserved Words and Identifiers 1 Programming in C Reserved Words and Identifiers Reserved word Word that has a specific meaning in C Ex: int, return Identifier Word used to name and refer to a data element or object manipulated by the

More information

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary

1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary Topic 2 1. Variables 2. Arithmetic 3. Input and output 4. Problem solving: first do it by hand 5. Strings 6. Chapter summary Arithmetic Operators C++ has the same arithmetic operators as a calculator:

More information

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

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

Primitive Data Types: Intro

Primitive Data Types: Intro Primitive Data Types: Intro Primitive data types represent single values and are built into a language Java primitive numeric data types: 1. Integral types (a) byte (b) int (c) short (d) long 2. Real types

More information

Data Types. Strings Variables Declaration Statements Named Constant Assignment Statements Intrinsic (Built-in) Functions

Data Types. Strings Variables Declaration Statements Named Constant Assignment Statements Intrinsic (Built-in) Functions Data and Operations Outline Data Types Arithmetic Operations Strings Variables Declaration Statements Named Constant Assignment Statements Intrinsic (Built-in) Functions Data and Operations 1 Data and

More information

Basic Operations jgrasp debugger Writing Programs & Checkstyle

Basic Operations jgrasp debugger Writing Programs & Checkstyle Basic Operations jgrasp debugger Writing Programs & Checkstyle Suppose you wanted to write a computer game to play "Rock, Paper, Scissors". How many combinations are there? Is there a tricky way to represent

More information

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

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual Contents 1 Introduction...2 2 Lexical Conventions...2 3 Types...3 4 Syntax...3 5 Expressions...4 6 Declarations...8 7 Statements...9

More information

COMP 110 Introduction to Programming. What did we discuss?

COMP 110 Introduction to Programming. What did we discuss? COMP 110 Introduction to Programming Fall 2015 Time: TR 9:30 10:45 Room: AR 121 (Hanes Art Center) Jay Aikat FB 314, aikat@cs.unc.edu Previous Class What did we discuss? COMP 110 Fall 2015 2 1 Today Announcements

More information

C++ Programming: From Problem Analysis to Program Design, Third Edition

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

4. Inputting data or messages to a function is called passing data to the function.

4. Inputting data or messages to a function is called passing data to the function. Test Bank for A First Book of ANSI C 4th Edition by Bronson Link full download test bank: http://testbankcollection.com/download/test-bank-for-a-first-book-of-ansi-c-4th-edition -by-bronson/ Link full

More information

Visual Basic distinguishes between a number of fundamental data types. Of these, the ones we will use most commonly are:

Visual Basic distinguishes between a number of fundamental data types. Of these, the ones we will use most commonly are: Chapter 3 4.2, Data Types, Arithmetic, Strings, Input Data Types Visual Basic distinguishes between a number of fundamental data types. Of these, the ones we will use most commonly are: Integer Long Double

More information

Program Fundamentals

Program Fundamentals Program Fundamentals /* HelloWorld.java * The classic Hello, world! program */ class HelloWorld { public static void main (String[ ] args) { System.out.println( Hello, world! ); } } /* HelloWorld.java

More information

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz) SOFTWARE DEVELOPMENT 1 Operators 2018W (Institute of Pervasive Computing, JKU Linz) OPERATORS Operators are required to form expressions. Depending on the number of operands they take, they are called:

More information

Homework #3 CS2255 Fall 2012

Homework #3 CS2255 Fall 2012 Homework #3 CS2255 Fall 2012 MULTIPLE CHOICE 1. The, also known as the address operator, returns the memory address of a variable. a. asterisk ( * ) b. ampersand ( & ) c. percent sign (%) d. exclamation

More information

LECTURE 3 C++ Basics Part 2

LECTURE 3 C++ Basics Part 2 LECTURE 3 C++ Basics Part 2 OVERVIEW Operators Type Conversions OPERATORS Operators are special built-in symbols that have functionality, and work on operands. Operators are actually functions that use

More information

Vba Variables Constant and Data types in Excel

Vba Variables Constant and Data types in Excel Vba Variables Constant and Data types in Excel VARIABLES In Excel VBA, variables are areas allocated by the computer memory to hold data. Data stored inside the computer memory has 4 properties: names,

More information

2.Simplification & Approximation

2.Simplification & Approximation 2.Simplification & Approximation As we all know that simplification is most widely asked topic in almost every banking exam. So let us try to understand what is actually meant by word Simplification. Simplification

More information

CIS 110: Introduction to Computer Programming

CIS 110: Introduction to Computer Programming CIS 110: Introduction to Computer Programming Lecture 3 Express Yourself ( 2.1) 9/16/2011 CIS 110 (11fa) - University of Pennsylvania 1 Outline 1. Data representation and types 2. Expressions 9/16/2011

More information

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Lesson 02 Data Types and Statements. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Lesson 02 Data Types and Statements MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL Topics Covered Statements Variables Data Types Arithmetic

More information

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods COMP-202 Unit 2: Java Basics CONTENTS: Using Expressions and Variables Types Strings Methods Assignment 1 Assignment 1 posted on WebCt and course website. It is due May 18th st at 23:30 Worth 6% Part programming,

More information

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types. Class #07: Java Primitives Software Design I (CS 120): M. Allen, 13 Sep. 2018 Two Types of Types So far, we have mainly been dealing with objects, like DrawingGizmo, Window, Triangle, that are: 1. Specified

More information

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved. C How to Program, 6/e 1992-2010 by Pearson Education, Inc. An important part of the solution to any problem is the presentation of the results. In this chapter, we discuss in depth the formatting features

More information

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables

Overview: Programming Concepts. Programming Concepts. Names, Values, And Variables Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating

More information

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript

Overview: Programming Concepts. Programming Concepts. Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Chapter 18: Get With the Program: Fundamental Concepts Expressed in JavaScript Fluency with Information Technology Third Edition by Lawrence Snyder Overview: Programming Concepts Programming: Act of formulating

More information

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators JAVA Standard Edition Java - Basic Operators Java provides a rich set of operators to manipulate variables.

More information

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur Operators and Expressions 8/24/2012 Dept of CS&E 2 Arithmetic operators Relational operators Logical operators

More information

Types and Expressions. Chapter 3

Types and Expressions. Chapter 3 Types and Expressions Chapter 3 Chapter Contents 3.1 Introductory Example: Einstein's Equation 3.2 Primitive Types and Reference Types 3.3 Numeric Types and Expressions 3.4 Assignment Expressions 3.5 Java's

More information

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

Ex: If you use a program to record sales, you will want to remember data: Data Variables Programs need to remember values. Ex: If you use a program to record sales, you will want to remember data: A loaf of bread was sold to Sione Latu on 14/02/19 for T$1.00. Customer Name:

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming Part I 1 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from this chapter, you will learn how to solve practical

More information

Computer System and programming in C

Computer System and programming in C 1 Basic Data Types Integral Types Integers are stored in various sizes. They can be signed or unsigned. Example Suppose an integer is represented by a byte (8 bits). Leftmost bit is sign bit. If the sign

More information

ARG! Language Reference Manual

ARG! Language Reference Manual ARG! Language Reference Manual Ryan Eagan, Mike Goldin, River Keefer, Shivangi Saxena 1. Introduction ARG is a language to be used to make programming a less frustrating experience. It is similar to C

More information

CIS133J. Working with Numbers in Java

CIS133J. Working with Numbers in Java CIS133J Working with Numbers in Java Contents: Using variables with integral numbers Using variables with floating point numbers How to declare integral variables How to declare floating point variables

More information

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one. http://www.tutorialspoint.com/go/go_operators.htm GO - OPERATORS Copyright tutorialspoint.com An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.

More information

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5. Week 2: Console I/O and Operators Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.1) CS 1428 Fall 2014 Jill Seaman 1 2.14 Arithmetic Operators An operator is a symbol that tells the computer to perform specific

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

Object Oriented Programming with Visual Basic.Net

Object Oriented Programming with Visual Basic.Net Object Oriented Programming with Visual Basic.Net By: Dr. Hossein Hakimzadeh Computer Science and Informatics IU South Bend (c) Copyright 2007 to 2015 H. Hakimzadeh 1 What do we need to learn in order

More information

CHAPTER 3 Expressions, Functions, Output

CHAPTER 3 Expressions, Functions, Output CHAPTER 3 Expressions, Functions, Output More Data Types: Integral Number Types short, long, int (all represent integer values with no fractional part). Computer Representation of integer numbers - Number

More information

Prof. Navrati Saxena TA: Rochak Sachan

Prof. Navrati Saxena TA: Rochak Sachan JAVA Prof. Navrati Saxena TA: Rochak Sachan Operators Operator Arithmetic Relational Logical Bitwise 1. Arithmetic Operators are used in mathematical expressions. S.N. 0 Operator Result 1. + Addition 6.

More information

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension. Java How to Program, 9/e Education, Inc. All Rights Reserved. } Covered in this chapter Classes Objects Methods Parameters double primitive type } Create a new class (GradeBook) } Use it to create an object.

More information

Arithmetic Operations

Arithmetic Operations 232 Chapter 4 Variables and Arithmetic Operations Arithmetic Operations The ability to perform arithmetic operations on numeric data is fundamental to computer programs. Many programs require arithmetic

More information

Operators. Java operators are classified into three categories:

Operators. Java operators are classified into three categories: Operators Operators are symbols that perform arithmetic and logical operations on operands and provide a meaningful result. Operands are data values (variables or constants) which are involved in operations.

More information

Chapter 2: Data and Expressions

Chapter 2: Data and Expressions Chapter 2: Data and Expressions CS 121 Department of Computer Science College of Engineering Boise State University August 21, 2017 Chapter 2: Data and Expressions CS 121 1 / 51 Chapter 1 Terminology Review

More information

VBScript: Math Functions

VBScript: Math Functions C h a p t e r 3 VBScript: Math Functions In this chapter, you will learn how to use the following VBScript functions to World Class standards: 1. Writing Math Equations in VBScripts 2. Beginning a New

More information

Lecture 1. Types, Expressions, & Variables

Lecture 1. Types, Expressions, & Variables Lecture 1 Types, Expressions, & Variables About Your Instructor Director: GDIAC Game Design Initiative at Cornell Teach game design (and CS 1110 in fall) 8/29/13 Overview, Types & Expressions 2 Helping

More information