© 2007 lawrenceville press slide 1 assignment statement an assignment statement gives a value to a...

12
© 2007 Lawrenceville Press Slide 1 Assignment Statement 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

Upload: jeremiah-barrett

Post on 26-Mar-2015

218 views

Category:

Documents


4 download

TRANSCRIPT

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

© 2007 Lawrenceville PressSlide 1

Assignment StatementAssignment StatementAssignment StatementAssignment 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

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

© 2007 Lawrenceville PressSlide 2

Variable AssignmentVariable AssignmentVariable AssignmentVariable Assignment

A variable can store only one value at any time.

int x;x = 5;x = 10;

xx

510

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

© 2007 Lawrenceville PressSlide 3

Primitive Data TypesPrimitive Data TypesPrimitive Data TypesPrimitive Data Types

Type Storage Requiredint 4 bytesdouble 8 byteschar 2 bytesboolean 1 bit

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

© 2007 Lawrenceville PressSlide 4

Abstract Data TypesAbstract Data TypesAbstract Data TypesAbstract 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);spotspot

getRadius()area()

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

© 2007 Lawrenceville PressSlide 5

C# PackagesC# PackagesC# PackagesC# 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.

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

© 2007 Lawrenceville PressSlide 6

Integer DivisionInteger DivisionInteger DivisionInteger Division

Integer division (/) is performed when both operands are integers. Only the integer portion of the quotient is returned:

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

© 2007 Lawrenceville PressSlide 7

Real DivisionReal DivisionReal DivisionReal 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

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

© 2007 Lawrenceville PressSlide 8

Modulus DivisionModulus DivisionModulus DivisionModulus Division

Modulus division (%) returns the remainder of a division operation:

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

© 2007 Lawrenceville PressSlide 9

Operator PrecedenceOperator PrecedenceOperator PrecedenceOperator 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:

5 + 6 * 4 / 2 = 17

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

© 2007 Lawrenceville PressSlide 10

Changing the Order of Changing the Order of OperationsOperations

Changing the Order of Changing the Order of OperationsOperations

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

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

© 2007 Lawrenceville PressSlide 11

Type CastingType CastingType CastingType 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

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

© 2007 Lawrenceville PressSlide 12

Programming ErrorsProgramming ErrorsProgramming ErrorsProgramming 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.