Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI

Slides:



Advertisements
Similar presentations
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Advertisements

Types and Arithmetic Operators
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS180 Recitation 3. Lecture: Overflow byte b; b = 127; b += 1; System.out.println("b is" + b); b is -128 byte b; b = 128; //will not compile! b went out.
1 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
Data types and variables
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Introduction to C Programming
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations System-oriented Programming, B. Hirsbrunner,
String Escape Sequences
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Chapter 2 Data Types, Declarations, and Displays.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
2440: 211 Interactive Web Programming Expressions & Operators.
EXPRESSIONS AND ASSIGNMENT CITS1001. Scope of this lecture Assignment statements Expressions 2.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
CPS120: Introduction to Computer Science Operations Lecture 9.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Primitive Variables.
Data Types Declarations Expressions Data storage C++ Basics.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI N305 Information Representation: Negative Integer Representation.
Doing math In java.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
0 Chap.2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations 2.5Arithmetic Operators 2.6Relational.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Operators A binary operator combines two values to get one result: x OP y where OP is any binary operators such as +, -, *, /, ==, !=, >, &&, or even =.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2012 CMPE-013/L Operators Gabriel Hugh Elkaim Spring 2012.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
CSE 220 – C Programming Expressions.
Chapter 2 - Introduction to C Programming
Variable Declarations, Data types, Expressions
Variable Declarations, Data types, Expressions
Variable Declaration, Data types, Expressions
Intro to C Tutorial 4: Arithmetic and Logical expressions
Chapter 2 - Introduction to C Programming
Variable Declarations, Data types, Expressions
Chapter 2 - Introduction to C Programming
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Negative Integer Representation
Chapter 2 Programming Basics.
Module 2 Variables, Data Types and Arithmetic
OPERATORS in C Programming
Dale Roberts, Lecturer IUPUI
DATA TYPES There are four basic data types associated with variables:
Introduction to C Programming
OPERATORS in C Programming
Presentation transcript:

Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI CSCI 230 Variable Declarations, Data types, Expressions - Variables and Operators

Dale Roberts Memory Concepts Variables Variable names (identifiers) correspond to locations in the computer's memory (von Neuman model) Every variable has a name, a type, a size and a value Whenever a new value is placed into a variable (through scanf, for example), it replaces (and destroys) the previous value. (Destructive write) Reading variables from memory does not change them A visual representation (memory map) int 45 i 4 bytes address value variable datatype size

Dale Roberts Keywords

Expression Expressions are computations that return a value. Like variables, a value is an instance of a data type. Examples of expressions are: 45 (int) 2+3 (int) 2.0/5.0 (double) “Hello” (string) x (the current value of variable int x)

Dale Roberts Arithmetic Operators Binary Operator (two operands) + (addition) - (subtraction) * (multiplication) / (division) % (modulus, remainder) (no ** ) 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 later Operators that have more than two operands use functional notation a = f(x,y,x). (x= = -2) (x= 1) (x= -1) (wrong expression) (x= 1, remainder) (y=5.75) Ans:

Dale Roberts Arithmetic Operators Arithmetic operators:

Dale Roberts Precedence

Relational Operators Binary Operators ==!=<> = Result is a integer:1 means TRUE 0 means FALSE No logical type variable and constants No space between the operators Example: MeaningCExpressionResult equal not equal greater less greater equal less equal == != > < >= <= 5 == 3 5 != 3 5 > 3 5 < 3 5 >= 3 5 <= ==0 int i=10, j=10, k=1; i + j <= 3 + k

Dale Roberts Relational Operators

Dale Roberts Logical (Boolean) Operators Binary Operators && (and)|| (OR) Unary Operator ! (not) Operand must be int Use float or double, the result may not predictable nonzero (TRUE)zero (FALSE) Result is int 1 (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: ExpressionResult 5||3 5||0 5&&3 5&&0 i&&j (if i=0, j=0 ) i&&j+1 (if i=5, j=0 ) !5 !0 !i (if i=5 ) (3 < 7) < 51 < 51 1 && 00

Dale Roberts Conditional (ternary) Operator Syntax expr1 ? expr2 : expr3 If expr1  0, then execute expr2 and ignore expr3 If expr1 = 0, then execute expr3 and ignore expr2 Example: x = i+j ? i+1 : j+1 Example: x = 5 ? 4 : 2;/* x = 4 */ Example: j = 4; i = 2 x = 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 */

Dale Roberts sizeof Operator Syntaxsizeof(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.

Dale Roberts Address Operator Syntax&var Get the address of the variable & means the address of var Type of var may be (a) fundamental data type (b) organized data type Example: int i=100; printf(“%d %d”, &i, i); AddressContent RAM