© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

CSci 1130 Intro to Programming in Java
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
Types and Arithmetic Operators
Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
© 2006 Pearson Addison-Wesley. All rights reserved Reference Types A variable of reference type refers to (is bound to) an object Data of reference.
Variables and Constants
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Variables, Data Types, & Constants. Topics & Objectives Declaring Variables Assignment Statement Reserve Words Data Types Constants Packages & Libraries.
OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Data Types Declarations Expressions Data storage C++ Basics.
Chapter 2 Variables.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
Operator precedence.  Evaluate a + b * c –multiplication first? a + (b * c) –addition first? ( a + b) * c  Java solves this problem by assigning priorities.
Numeric Data Types There are six numeric data types: byte, short, int, long, float, and double. Sample variable declarations: int i, j, k; float numberOne,
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
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.
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Chapter 4 Assignment Statement
Chapter 3 Syntax, Errors, and Debugging
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
Assignment and Arithmetic expressions
Primitive and Reference Data Values
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Arithmetic Expressions & Data Conversions
Chapter 2 Variables.
Expressions and Assignment
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.
Data Types and Expressions
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is assigned to x x = y + 2; the value of an expression (y + 2) is assigned to x x = z; the value of another variable ( z ) is assigned to x

© 2007 Lawrenceville Press Slide 2 Variable Assignment A variable can store only one value at any time. int x; x = 5; x = 10; x 5 10

© 2007 Lawrenceville Press Slide 3 Primitive Data Types TypeStorage Required int 4 bytes double 8 bytes char 2 bytes boolean 1 bit

© 2007 Lawrenceville Press Slide 4 Abstract Data Types A variable declared with a class is called an object. For example, the object spot is type Circle: Circle spot = new Circle(4);spot getRadius() area()

© 2007 Lawrenceville Press Slide 5 C# Packages Numerous packages are included with C# Packages contain classes Packages can be added to an application with an Using statement. For example, the statement Using System; makes the Scanner class and its methods accessible to the application.

© 2007 Lawrenceville Press Slide 6 Integer Division Integer division ( / ) is performed when both operands are integers. Only the integer portion of the quotient is returned:

© 2007 Lawrenceville Press Slide 7 Real Division Real division ( / ) is performed when one or both operands are type double. The entire quotient, including the decimal portion is returned: double result; result = 20.0/7.0;//result is 2.857

© 2007 Lawrenceville Press Slide 8 Modulus Division Modulus division ( % ) returns the remainder of a division operation:

© 2007 Lawrenceville Press Slide 9 Operator Precedence Operators in Java have the following precedence: 1. multiplication and division 2. addition and subtraction Operators of the same precedence are evaluated in order from left to right. For example, multiplication is performed first, then division, and finally addition: * 4 / 2 = 17

© 2007 Lawrenceville Press Slide 10 Changing the Order of Operations The order in which operators are evaluated can be changed by using parentheses. For example, addition is performed first, then multiplication, and finally division: (5 + 6) * 4 / 2 = 22

© 2007 Lawrenceville Press Slide 11 Type Casting Type Casting converts a number of one type to a number of a different, but compatible type. Type casting is used to: 1.make the operand types in an expression match. For example, wholeNum = (int)y * 2 2.truncate the decimal portion of a double. For example, wholeNum = (int)z 3.change the way in which a division ( / ) operation will be performed. For example, realDivision = (double)a / (double)b

© 2007 Lawrenceville Press Slide 12 Programming Errors Syntax errors violate the rules of C#. Logic errors, also called semantic errors, occur in statements that are syntactically correct, but produce undesired or unexpected results. Run-time errors, also called exceptions, halt program execution at the statement that cannot be executed. One type of exception is called DividByZeroExecption.