Lecture 2 Expressions and Assignment.pptx

amirbukhari 102 views 20 slides Sep 09, 2025
Slide 1
Slide 1 of 20
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20

About This Presentation

Declaration Statements
Primitive Data Types
Identifiers
Assignment Statements
Arithmetic Operators and Expressions
Precedence Rules
Round-Off Errors in Floating-Point Numbers
Integer and Floating-Point Division
The % Operator
Type Casting
Increment and Decrement Operators


Slide Content

CS 120 - Programming I Lecture 2:  Expressions and Assignment Jubail Industrial College Computer Science & Engineering Department

Declaration Statements Primitive Data Types Identifiers Assignment Statements Arithmetic Operators and Expressions Precedence Rules Outline 2 Round-Off Errors in Floating-Point Numbers Integer and Floating-Point Division The % Operator Type Casting Increment and Decrement Operators

Java Data Types Basic data types in Java are called primitive types. 3 3

Primitive Data Types 4 4

Variable Declaration 5 A variable in Java is a name for a location in memory Every variable in a Java program must be declared before it can be used A variable declaration tells the compiler what kind of data ( type ) will be stored in the variable General form:     Data_Type var_name = some_value ; Examples: Data_Type var_name;  int a = 16 ; double b = 5.5, c = 8.11; int x; double y, z; x = 5; y = 7.32; z = 9.8; 5

Names of variables (Identifiers) 6 6 Variable names (identifier) must: Not start with a digit Consist of only letters, digits, or underscore Not be a reserved word ( public, class, void, static… ) Java is a case-sensitive language: Rate , rate , and RATE are the names of three different variables. Which of these are legal:

Naming Conventions 7 7 Start the names of variables, methods, and objects with a lowercase letter, indicate "word" boundaries with an uppercase letter, and restrict the remaining characters to digits and lowercase letters topSpeed        bankRate1       timeOfArrival Start the names of classes with an uppercase letter and, otherwise, adhere to the rules above FirstProgram            MyClass           String

Assignment Statements 8 8 In Java, the assignment statement is used to change the value of a variable General form: var_name = expression; Expression could be a constant, mathematical, or logical expression. The expression is first evaluated, and then the variable on the left-hand side of the equal sign is set equal to the value of the expression. What are the values of x, y and z: int x = 5, y, z; y = x * 2; z = y + x - 20;

Assignment Statements With Primitive Types 9 9 A variable can also occur on both sides of the assignment operator. This is used to increase or decrease the value of the variable. The assignment operator is automatically executed from right-to-left, so assignment statements can be chained int count = 5; count = count + 2;  number1 = number2 = 7; 

Initialize Variables 10 10 A variable that has been declared but not given a value by some means is said to be uninitialized What is the value of v1, v2? In certain cases an uninitialized variable is given a default value It is best not to rely on this and always initialize your variables int v1 = 5; int v2; 

Shorthand Assignment Statements 11 11 This notation is a shorthand for an assignment to a variable that involves arithmetic operator to the same variable. In general the form:                                var_name Operator= expression; Is shorthanded by:               var_name = var_name Operator expression; 

Arithmetic Operators and Expressions 12 12 These are : + (addition), - (subtraction), * (multiplication), / (division), and % (modulo, remainder) Each of these operators works on two operands. An operand can be a variable, constant or result of another operator. If the two operands of an arithmetic operator are of the same type, then the resulting type is the same as them. If different types of operands are in an operator, then the resulting type is the larger one. The ordering of types from small to large in Java is: Byte    short    int    long    float    double

Integer and Floating-Point Division 13 13 When one or both operands are a floating-point type, division results in a floating-point type 15.0/2 evaluates to 7.5 When both operands are integer types, division results in an integer type Any fractional part is discarded The number is not rounded 15/2 evaluates to 7 Be careful to make at least one of the operands a floating-point type if the fractional portion is needed

Assignment Compatibility 14 14 The result of an expression (or a constant) should be assigned to variable of the same type as the result. More generally, a value of any type in the following list can be assigned to a variable of any type that appears to the right of it Byte    short    int    long    float    double Which of these are valid?

Type Casting 15 15 A type cast takes a value of one type and produces a value of another type with an "equivalent" value   int x = (int) 2.9; When type casting from a floating-point to an integer type, the number is truncated, not rounded:   (int) 2.9 evaluates to 2 , not 3 In some situation, type casting from integer to floating point is needed. What is the value of d?

Precedence Rules 16 16 Precedence rules indicate which operator should be evaluated first in an expression with more than 1 operator.

The % (modulo) Operator 17 17 The % operator is used with operands of type int to recover the information lost after performing integer division 15/2 evaluates to the quotient 7   15%2 evaluates to the remainder 1 The % operator can be used to count by 2's , 3's , or any other number To count by twos, perform the operation number % 2 , and when the result is , number is even

Increment and Decrement Operators 18 18 The increment operator ( ++ ) adds one to the value of a variable If n is equal to 2 , then n++ or ++n will change the value of n to 3 The decrement operator ( -- ) subtracts one from the value of a variable If n is equal to 4 , then n-- or --n will change the value of n to 3

Increment and Decrement Operators 19 19 When either operator precedes its variable, and is part of an expression, then the expression is evaluated using the changed value of the variable If n is equal to 2 , then 2*(++n) evaluates to 6 When either operator follows its variable, and is part of an expression, then the expression is evaluated using the original value of the variable, and only then is the variable value changed If n is equal to 2 , then 2*(n++) evaluates to 4

The end 20