Department of Computer and Information Science, School of Science, IUPUI

13
Dale Robert Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected] CSCI 230 Variable Declarations, Data types, Expressions - Variables and Operators

description

Department of Computer and Information Science, School of Science, IUPUI. CSCI 230. Variable Declarations, Data types, Expressions - Variables and Operators. Dale Roberts, Lecturer Computer Science, IUPUI E-mail: [email protected]. address. variable. 36443. i. - PowerPoint PPT Presentation

Transcript of Department of Computer and Information Science, School of Science, IUPUI

Page 1: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Department of Computer and Information Science,School of Science, IUPUI

Dale Roberts, LecturerComputer Science, IUPUIE-mail: [email protected]

CSCI 230

Variable Declarations, Data types, Expressions

- Variables and Operators

Page 2: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Memory ConceptsMemory Concepts

Variables Variables Variable names (identifiers) correspond to locations in the Variable names (identifiers) correspond to locations in the computer's memory (von Neuman model)computer's memory (von Neuman model)

Every variable has a name, a type, a size and a valueEvery variable has a name, a type, a size and a value

Whenever a new value is placed into a variable (through Whenever a new value is placed into a variable (through scanfscanf, for example), it replaces (and destroys) the , for example), it replaces (and destroys) the previous value. (Destructive write)previous value. (Destructive write)

Reading variables from memory does not change themReading variables from memory does not change them

A visual representation (memory map)A visual representation (memory map)

36443

int 45 i

4 bytes

addressvaluevariable

datatype size

Page 3: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

KeywordsKeywords

Keywords

auto double int struct

break else long switch case enum register typedef char extern return union const float short unsigned

continue for signed void default goto sizeof volatile do if static while

Page 4: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

ExpressionExpression

Expressions are computations that return a Expressions are computations that return a value.value.

Like variables, a value is an instance of a Like variables, a value is an instance of a data type.data type.

Examples of expressions are:Examples of expressions are:

45 (int)45 (int)

2+3 (int)2+3 (int)

2.0/5.0 (double)2.0/5.0 (double)

““Hello” (string)Hello” (string)

x (the current value of variable int x)x (the current value of variable int x)

Page 5: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Arithmetic OperatorsArithmetic OperatorsBinary Operator (two operands)Binary Operator (two operands)+ (addition) - (subtraction) * (multiplication)

/ (division) % (modulus, remainder) (no ** )

Unary Operator (single operands)Unary Operator (single operands)- (no + ) Example:

int i=1, j=2, k=3, x;x=i+2*j-22/k; x=-1+j; x=1+-j; x=+i+j; x=22%k;

float f=1.5, g=0.5, y;y=2.5*f+4.0*g;

Exercise: Try -5%3 -5%-3 5%-3

(hint: -5/3=-1 -5/-3=1 5/-3=-1 and R=x-y*i)

Mixed data types will be discussed laterOperators that have more than two operands use functional notation a = f(x,y,x).

(x=1 + 4 -7 = -2)(x= 1)(x= -1)

(wrong expression)(x= 1, remainder)(y=5.75)

Ans: -2 -2 2

Page 6: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Arithmetic OperatorsArithmetic Operators

Arithmetic operators:Arithmetic operators:

C operation

Arithmetic operator

Algebraic expression

C expression

Addition + f + 7 f + 7

Subtraction - p – c p - c

Multiplication * bm b * m

Division / x / y x / y

Modulus % r mod s r % s

Page 7: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

PrecedencePrecedence

Operator(s) Operation(s) Order of evaluation (precedence)

() Parentheses Evaluated first. If the parentheses are nested, the expression in the innermost pair is evaluated first. If there are several pairs of parentheses “on the same level” (i.e., not nested), they are evaluated left to right.

*, /, or % Multiplication,Division, Modulus

Evaluated second. If there are several, they are evaluated left to right.

+ or - Addition Subtraction

Evaluated last. If there are several, they are evaluated left to right.

Page 8: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Relational OperatorsRelational OperatorsBinary OperatorsBinary Operators== != < > <= >=

Result is a integer: 1 means TRUE0 means FALSE

No logical type variable and constants No space between the operators

Example:

Meaning C Expression Result

equal

not equal

greater

less

greater equal

less equal

==

!=

>

<

>=

<=

5 == 3

5 != 3

5 > 3

5 < 3

5 >= 3

5 <= 3

01

1

0

1

0

1

0

0==0int i=10, j=10, k=1;

i + j <= 3 + k

Page 9: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Relational OperatorsRelational Operators

Standard algebraic equality operator or relational operator

C equality or relational operator

Example of C condition

Meaning of C condition

Equality Operators = == x == y x is equal to y

not = != x != y x is not equal to y Relational Operators > > x > y x is greater than y

< < x < y x is less than y

>= >= x >= y x is greater than or equal to y

<= <= x <= y x is less than or equal to y

Page 10: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Logical (Boolean) OperatorsLogical (Boolean) OperatorsBinary OperatorsBinary Operators

&& (and) || (OR)

Unary OperatorUnary Operator! (not)

Operand must be Operand must be intintUse float or double, the result may not predictable

nonzero (TRUE) zero (FALSE)

Result is Result is intint1 (TRUE) 0 (FALSE)

Express connected by && or || are evaluated from left to right, and evaluation stops as soon as the truth or falsehood of the result is known. i.e. ‘expr1 && expr2’ is not equal to ‘expr2 && expr1’. This is called short-circuit evaluation.

‘inward == 0’ normally be written as ‘!inward’

Example:

3 < 7 < 5

3 < 7 && 7 < 5

Example:

Expression Result5||35||05&&35&&0i&&j (if i=0, j=0)i&&j+1 (if i=5, j=0)!5!0!i (if i=5)

11

1001010

(3 < 7) < 5 1 < 5 1

1 && 0 0

Page 11: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Conditional (ternary) OperatorConditional (ternary) OperatorSyntaxSyntax

expr1 ? expr2 : expr3expr1 ? expr2 : expr3If expr1 0, then execute expr2 and ignore expr3If expr1 = 0, then execute expr3 and ignore expr2

Example: x = i+j ? i+1 : j+1Example:

x = 5 ? 4 : 2; /* x = 4 */Example:

j = 4;i = 2x = i+j ? i+1 : j-1 /* x = 3 */

Example:l = a > b ? a : b; /* the larger of a and b */

Example:max =(a > b)?((a>c)?a:c):(b>c)?b:c);

/* the maximum number among a, b, and c */Example:

x = a > 0 ? a: -a; /* the absolute value of a */

Page 12: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

sizeof Operatorsizeof OperatorSyntaxSyntax

sizeof(expr)sizeof(expr)

The number of bytes occupied by expr

For most computers

sizeof(3) 2 or 4 (bytes)

(depending on16 bit CPU or 32 bit CPU), where 3 is an integer

sizeof(3L) 4 (long int)

sizeof(3.0) 8 (double float)

Example:

double i;

printf(“%d”,sizeof(i)); 8

Usually, this operator is used to get the size of an organized variable (like struct, union, …)

This is one of a few functions that are built-in. No #include is required.

Page 13: Department of Computer and Information Science, School of Science, IUPUI

Dale Roberts

Address OperatorAddress OperatorSyntaxSyntax

&var&varGet the address of the variable

& means the address of varType of var may be(a) fundamental data type(b) organized data type

Example:

int i=100;

printf(“%d %d”, &i, i);

Address Content

RAM 100100210011000

100310041005