CS 120 - Programming I
Lecture 2: Expressions and Assignment
Jubail Industrial College
Computer Science & Engineering Department
2
• Declaration Statements
• Primitive Data Types
• Identifiers
• Assignment Statements
• Arithmetic Operators and Expressions
• Precedence Rules
Outline
• Round-Off Errors in Floating-Point
Numbers
• Integer and Floating-Point Division
• The % Operator
• Type Casting
• Increment and Decrement Operators
3
Java Data Types
Basic data types in Java are called primitive types.
3
4
Primitive Data Types
4
5
Variable Declaration
• 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
6
Names of variables (Identifiers)
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:
7
Naming Conventions
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
8
Assignment Statements
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;
9
Assignment Statements With Primitive Types
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;
10
Initialize Variables
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;
11
Shorthand Assignment Statements
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;
12
Arithmetic Operators and Expressions
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
13
Integer and Floating-Point Division
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
14
Assignment Compatibility
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?
15
Type Casting
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?
16
Precedence Rules
16
• Precedence rules indicate which operator should be evaluated first in an
expression with more than 1 operator.
17
The % (modulo) Operator
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 0,
number is even
18
Increment and Decrement Operators
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
19
Increment and Decrement Operators
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
20
The
end

Lecture 2 Expressions and Assignment.pptx

  • 1.
    CS 120 -Programming I Lecture 2: Expressions and Assignment Jubail Industrial College Computer Science & Engineering Department
  • 2.
    2 • Declaration Statements •Primitive Data Types • Identifiers • Assignment Statements • Arithmetic Operators and Expressions • Precedence Rules Outline • Round-Off Errors in Floating-Point Numbers • Integer and Floating-Point Division • The % Operator • Type Casting • Increment and Decrement Operators
  • 3.
    3 Java Data Types Basicdata types in Java are called primitive types. 3
  • 4.
  • 5.
    5 Variable Declaration • Avariable 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
  • 6.
    6 Names of variables(Identifiers) 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:
  • 7.
    7 Naming Conventions 7 • Startthe 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
  • 8.
    8 Assignment Statements 8 • InJava, 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;
  • 9.
    9 Assignment Statements WithPrimitive Types 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;
  • 10.
    10 Initialize Variables 10 • Avariable 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;
  • 11.
    11 Shorthand Assignment Statements 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;
  • 12.
    12 Arithmetic Operators andExpressions 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
  • 13.
    13 Integer and Floating-PointDivision 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
  • 14.
    14 Assignment Compatibility 14 • Theresult 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?
  • 15.
    15 Type Casting 15 • Atype 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?
  • 16.
    16 Precedence Rules 16 • Precedencerules indicate which operator should be evaluated first in an expression with more than 1 operator.
  • 17.
    17 The % (modulo)Operator 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 0, number is even
  • 18.
    18 Increment and DecrementOperators 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
  • 19.
    19 Increment and DecrementOperators 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
  • 20.