Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.

Slides:



Advertisements
Similar presentations
1 9/13/06CS150 Introduction to Computer Science 1 Type Casting.
Advertisements

1 9/17/07CS150 Introduction to Computer Science 1 Type Casting.
Overview Order of presentation different than published handouts Run a program on ccc Finish Arithmetic operations Data types integer char floating point.
CS150 Introduction to Computer Science 1
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
CS150 Introduction to Computer Science 1
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
CS 1400 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example: int.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CS Jan 2007 Chapter 3: sections Variable initialization Variables may be initialized when declared –Form; type name = initial_value; –Example:
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.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Basic Elements of C++ Chapter 2.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
Chapter 2: Using Data.
OPERATORS.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
CPS120: Introduction to Computer Science Operations Lecture 9.
1 Operations Making Things Happen (Chap. 3) Expressions.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Computer Engineering 1 st Semester Dr. Rabie A. Ramadan 3.
COMP Primitive and Class Types Yi Hong May 14, 2015.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
1 Operations Chapter 4 & Section Expressions As we've seen, in a C++ program, any finite sequence of objects and operations that combine to produce.
Simple Data Types Chapter Constants Revisited t Three reasons to use constants –Constant is recognizable –Compiler prevents changes in value.
Programming Fundamentals. The setw Manipulator setw changes the field width of output. The setw manipulator causes the number (or string) that follows.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
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.
Gator Engineering Project 1 Grades released Re-grading –Within one week –TA: Fardad, or office hours: MW 2:00 – 4:00 PM TA Huiyuan’s office hour.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Unary, Binary, logical Operations, Explicit type conversion Lecture 6 Instructor: Haya Sammaneh.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
Rational Expressions relational operators logical operators order of precedence.
ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.
Chapter Topics The Basics of a C++ Program Data Types
Lecture 3 Java Operators.
Programming Fundamentals
2.0 FUNDAMENTALS OF JAVA PROGRAMMING LANGUAGE
Computing Fundamentals
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Fundamental Data Types
Basic Elements of C++ Chapter 2.
Operators and Expressions
Conversions of the type of the value of an expression
Lecture 3 Expressions Richard Gesick.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
More about Numerical Computation
Chapter 7 Additional Control Structures
Associativity and Prescedence
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Fundamental Data Types
Type Conversion It is a procedure of converting one data type values into another data type In C programming language we are having two types of type conversion.
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Operator King Saud University
DATA TYPES There are four basic data types associated with variables:
HNDIT11034 More Operators.
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Presentation transcript:

Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail

Lecture 072 Arithmetic Operators Increment and decrement operators x = x + 1; Or ++x; Or x++; (Prefix and postfix) x = x – 1; Or --x; Or x--; Subtleties Example: int x=5, y=5; cout << x++ << endl; cout << ++y << endl; cout << x << ‘,’ << y; x = 10; y = ++x; x = 10; y = x++;

Lecture 073 More Operators = Assignment operator Compound assignment operators operatorexampleequivalent statement +=x += 2; x = x+2; -=x -= 2;x = x-2; *=x *= y;x = x*y; /=x /= y;x = x/y;

Lecture 074 Operator Precedence & Associativity Precedence: If two operators apply to same operand, higher precedence operator applied first e.g. (3 + 5 * 6) has value 33 Associativity: If two operators have same precedence, associate L-R or R-L as defined e.g. (120 / 6 * 5) has value 100 as associate from L-R

Lecture 075 Evaluation Order of Operators

Lecture 076 Type Conversions in Expressions Operators, variables and constants form expressions e.g. a = b + 2; bool, char, unsigned char, signed char, and short automatically converted to int In an expression with mixed data types, smaller is promoted to larger one e.g. 9.0/5 has 5 promoted to double status

Lecture 077 Type Conversions on Assignment Type converted to that of the receiving variable float tree = 3; //int to float int guess = ; //float to int int debt = 3.0E12; //erroneous What output if we cout tree, guess and debt ? Potential problems: Loss of precision, or loss of fractional part, or out of range. Compiler complains usually

Lecture 078 Casting A programmer forces an expression to be of the specified type (as opposed to automatic conversions) (type) expression or type (expression) e.g. (float) 5/2; char(77); long (shorty); int (‘Z’);

Lecture 079 Relational & Logical Operators Relational for comparison and Logical for connecting true and false values Outcome of a relational or logical expression is bool (true or false) For relational operators, operands and expressions of any data type that can be compared Any non-zero value converted to true, zero to false

Lecture 0710 Relational & Logical Operators Fill table of (p AND q), (p OR q), (NOT p). Also for 3 variables. Precedence lower than arithmetic operators #include int main() { int p, q; cout << "Enter P (0 or 1): "; cin >> p; cout << "Enter Q (0 or 1): "; cin >> q; cout << "P AND Q: " << (p && q) << endl; cout << "P OR Q: " << (p || q) << endl; return 0; }

Lecture 0711 Relational & Logical Operators #include int main() { int p=5, q=2; bool bl=(p<=q); cout<<bl<<endl; bl=!bl; cout<<bl<<endl; return 0; } (score >= 0) && (score <= 10) –is true if score is between 0 and 10 (including 0 and 10) (score 10) –is true exactly in the cases where the previous condition would be false ! ( (score >= 0) && (score <= 10) )

Lecture 0712 Relational & Logical Operators Pitfall: Do not use strings of inequalities. They may be legal C++ code but will not do what you (for now) expect them to do if (x < y < z) // WRONG! cout << "y is between x and z“ << endl; Instead, you have to write: if ((x < y) && (y < z)) // CORRECT! cout << "y is between x and z" << endl;

Lecture 0713 sizeof operator sizeof operator to test sizes e.g. cout << sizeof(int); cout << sizeof i; //i is a variable