Variable Declarations, Data types, Expressions

Slides:



Advertisements
Similar presentations
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Advertisements

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Pointers Pointer Arithmetic Dale Roberts, Lecturer Computer.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
CS150 Introduction to Computer Science 1
Compound Operators 03/07/11. More Operators, First Section 4.4.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
1 Structured Programming in C Welcome to CPSC 206.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Characters and Strings Literals and Variables Dale Roberts,
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Dale Roberts Object Oriented Programming using Java - Class Constructors Dale Roberts, Lecturer Computer Science, IUPUI Department.
Operators and Expressions
C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions.
Lecture 17: 10/29/2002CS149D Fall CS149D Elements of Computer Science Ayman Abdel-Hamid Department of Computer Science Old Dominion University Lecture.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
LESSON 6 – Arithmetic Operators
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Dale Roberts Program Control using Java - Boolean Expressions Dale Roberts, Lecturer Computer Science, IUPUI Department of.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
Dale Roberts Object Oriented Programming using Java - OOD to OOP: ATM Case Study Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Program Control using Java - Repetition Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
Pointers: Basics. 2 What is a pointer? First of all, it is a variable, just like other variables you studied  So it has type, storage etc. Difference:
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Dale Roberts Object Oriented Programming using Java - Final and Static Keywords Dale Roberts, Lecturer Computer Science, IUPUI
Welcome to Concepts of Pointers. Prepared by:- Sumit Kumar PGT(Computer Science) Kv,Samba.
Lecture 2. Outline Sample programming question Sample C program Identifiers and reserve words Program comments Preprocessor directives Data types and.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Introduction to Java - Input, Program Control and Instantiation Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Object Oriented Programming using Java - Getters and Setters Dale Roberts, Lecturer Computer Science, IUPUI
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Dale Roberts 1 Operator Overloading Member Functions Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Pointers Call-by-Reference.
Object Oriented Programming using Java - Composition
CSE 220 – C Programming Expressions.
Pointers Introduction CSCI 230
GUI Programming using Java - Key Events
Computing Fundamentals
Functions Examples CSCI 230
Arrays Declarations CSCI N305
Abstract Data Types Polynomials CSCI 240
A First C Program (mixing datatypes)
A First C Program (mixing datatypes)
Object Oriented Programming using Java - Class Instance Variables
Variable Declarations, Data types, Expressions
Variable Declaration, Data types, Expressions
Variable Declarations, Data types, Expressions
Functions Declarations CSCI 230
Structure of a C Program
More C expressions ANSI-C.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Negative Integer Representation
Unary Operators ++ and --
Pointers Call-by-Reference CSCI 230
Associativity and Prescedence
Classes Static Members
Dale Roberts, Lecturer IUPUI
Chapter 4: Expression and Operator
Analysis of Algorithms Growth Rates
Chapter 2 Primitive Data Types and Operations
Dale Roberts, Lecturer IUPUI
Analysis of Algorithms Big-Omega and Big-Theta
HNDIT11034 More Operators.
Characters and Strings
Classes Introduction CSCI 240
Classes Class Data Members & Initializers
Classes Member Qualifiers
Structures Declarations CSCI 230
Presentation transcript:

Variable Declarations, Data types, Expressions Department of Computer and Information Science, School of Science, IUPUI CSCI 230 Variable Declarations, Data types, Expressions - Assignments Dale Roberts, Lecturer Computer Science, IUPUI E-mail: droberts@cs.iupui.edu

Assignment Operators Syntax: var = expression; Assign the value of expression to variable (var) Example: int x, y, z; x = 5; y = 7; z = x + y; x = y = z = 0; int x = y = z = 0; int i, j; float f, g; i = f = 2.5; g = j = 3.5;  Z = (x = 5) + (y = 7) much faster  same as x = (y = (z = 0));  wrong ! int x = 0, y = 0, z = 0;  i = 2; f = 2.5;  g = 3.0; j = 3;

Short Hand Assignment Syntax f = f op g can be rewritten to be f op= g such as: a = a + 2  a += 2, a = a - 2  a -= 2, a = a * 2  a *= 2, a = a / 2  a /= 2, a = a % 2  a %= 2, a = a << 2  a <<= 2, a = a & 2  a &= 2, a = a | 2  a |= 2, a = a ^ 2  a ^= 2 No blanks between op and = x *= y + 1 is actually x = x * (y+1) rather than x = x * y + 1 Example: q = q / (q+2)  q /= q+2 j = j << 2  j <<= 2 Advantage: help compiler to produce more efficient code More complicated examples: int a=1, b=2, c=3, x=4, y=5; a += b += c *= x + y - 6; printf(“%d %d %d %d\n”,a,b,c,x,y); a += 5 + b += c += 2 + x + y; a += 5 + (b+= c += 2 + x + y); /* result is 12 11 9 4 5 */ /* wrong */ /* result is 22 16 14 4 5 */

Increment / Decrement Operators Prefix Operator Before the variable, such as ++n or –n Increments or decrements the variable before using the variable Postfix Operator After the variable, such as n++ or n-- Increments or decrements the variable after using the variable ++n Increment n 2. Get value of n in expression --n Decrement n 2. Get value of n in expression n++ Get value of n in expression 2. Increment n n-- Get value of n in expression 2. Decrement n

Increment / Decrement Operators (cont.) Simple cases ++i; i++; (i = i + 1; or i += 1;) --i; i--; (i = i - 1; or i -= 1;) Example: i = 5; i++; (or ++i;) printf(“%d”, i)  6 i = 5; i--; (or --i;) printf(“%d”, i)  4

Complicated cases i = 5; i j j = 5 + ++i; i = 5; j = 5 + i++; 6 11 6 11 6 10 4 9 4 10

Increment / Decrement Operators (cont.) Invalid cases ++3 3++ --3 3-- ++(x+y+z) (x+y+z)++ --(x+y+z) (x+y+z)-- ++x++ --x-- ++x-- --x++ Note: Can not increment or decrement constant and expression i ++j or i –-j (WRONG) i + ++j i + –-j i - --j i - ++j (OK) Example: i - - j (valid, second – is Unary - ) i + + j (invalid, no + Unary)