© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.

Slides:



Advertisements
Similar presentations
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Advertisements

Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.
More on Numerical Computation CS-2301 B-term More on Numerical Computation CS-2301, System Programming for Non-majors (Slides include materials from.
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Logical and Relational Expressions Nested if statements.
Introduction Computer program: an ordered sequence of instructions whose objective is to accomplish a task. Programming: process of planning and creating.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
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.
More about Numerical Computation CS-2301, B-Term More about Numerical Computation CS-2301, System Programming for Non-Majors (Slides include materials.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
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.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
Expressions, Data Conversion, and Input
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Integer Data representation Addition and Multiplication.
Operaciones y Variables
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Data representation and Data Types Variables.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
LESSON 6 – Arithmetic Operators
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
Chapter 2: Using Data.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
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.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Introduction to Algorithmic Processes CMPSC 201C Fall 2000.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
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.
CSE 220 – C Programming Expressions.
Chapter Topics The Basics of a C++ Program Data Types
Chapter 7: Expressions and Assignment Statements
Lecture 3: Operators, Expressions and Type Conversion
ITEC113 Algorithms and Programming Techniques
Basic Elements of C++.
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Java Programming: From Problem Analysis to Program Design, 4e
Basic Elements of C++ Chapter 2.
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Introduction to C++ Programming
Chapter 2: Basic Elements of Java
C Operators, Operands, Expressions & Statements
Data Types and Expressions
Operator King Saud University
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style

© Janice Regan, CMPT 102, Sept Components of a C Program  More types of tokens (smallest individual units of a program) include:  Other separators blanks, tabs, line feed, blank lines …  Operators actions used to combine constants = and + in the expression y = x + THREE;

© Janice Regan, CMPT 102, Sept C: Binary Arithmetic Operators  A Binary Operator operates on two arguments  + addition  -subtraction  * multiplication  / division  % modulus or remainder (only for integers, 5%2=1)  Evaluated left to right

© Janice Regan, CMPT 102, Sept Unary Arithmetic Operators in C  A Unary Operator operates on one argument  + positive  -negative  ++ increment  --decrement  ~ones complement  Evaluated right to left

© Janice Regan, CMPT 102, Sept Expressions in C  An expression can be a single variable, or can be a series of variables combined using operators  Arithmetic expressions manipulate numeric variables following the rules of algebra  Relational expressions compare numerical values and give a logical answer (more later)  The two variables operated on by a binary arithmetic or relational operator should both have the same type  If they have different types one must have it’s type converted to the type of the other

© Janice Regan, CMPT 102, Sept Precedence of operators in C  ( ) []. innermost first (post)  (pre) + - ! ~ & *(unary operators)  * / %  + -  = += -= *= /= %=  Evaluate 2 and 5 right to left  Evaluate 1, 3, and 4 left to right

© Janice Regan, CMPT 102, Sept Expressions: arithmetic operators  A + B + C X + C  Let A=10, B=5, C=2 + A B X C A B C + + Value of expression

© Janice Regan, CMPT 102, Sept Expressions: arithmetic operators  Order of operations is determined by operator precedence rules / before +  A + B / C A + X  Let A=10, B=5, C=2 + A B X C / A B C / + Value of expression

© Janice Regan, CMPT 102, Sept Importance of order of operations  Order of operations is determined by operator precedence rules () before /  (A + B) / C X / C  Let A=10, B=5, C=2 2 A B C + / + A B X C Value of expression

© Janice Regan, CMPT 102, Sept Expressions: arithmetic operators  Types of operands: float vs. integer divide  An operator always operates on two operands of the same type  A + B / C A + X  Let A=23.7, B=55.4, C=1.2 + A B X C / Value of expression A B C / +

© Janice Regan, CMPT 102, Sept Expressions: arithmetic operators  Let A=27, B=5, C=2, D=3, E=8  ((A + B) / C) – D % E ( X / C) – D % E Y – D % E Y – Z + A B X C / Y D 3 8 % Z E A B C + % D E / _

© Janice Regan, CMPT 102, Sept Expressions: arithmetic operators  Order of operations is determined by operator precedence rules unary -, before /, before +  -A + B / C X + B / C X + Y  Let A=10, B=5, C=2 + X B Y C / A B C / + - A 10 -

© Janice Regan, CMPT 102, Sept Expressions: arithmetic operators  ++A – B pre increment  --B + C pre decrement  Pre-increment: increment variable then use in expression  A * C++ post increment  A / C-- post decrement  Post increment: use variable in expression then increment the variable

© Janice Regan, CMPT 102, Sept Assignment Statements  Basic statement used to perform calculations  Form: result = expression;  Example: A = B + C * D;  NOT the same as an = in an equation  Each variable is associated with a location in memory  Evaluate the expression on the left (B+C*D) Multiply the value in the memory location associated with variable C by the value in the memory location associated with variable C Add the product to the value of the in the memory location associated with variable B The sum is the value of the expression  The value of the expression on the right hand side is placed in the memory location associated with variable A

© Janice Regan, CMPT 102, Sept Expressions: arithmetic operators  Order of operations is determined by operator precedence rules * before + before =  A = B + C * D A = B + X A = Y  A=10, B=5, C=2, D=12 + X C B D / A B C * + = D Y A =

© Janice Regan, CMPT 102, Sept Assignment Statements:  Form: result = expression;  Example: X = X * Y;  Each variable is associated with a location in memory  Evaluate the expression on the left (X*Y) Multiply the value in the memory location associated with variable X by the value in the memory location associated with variable Y The product is the value of the expression  The product is placed in the memory location associated with variable X overwriting the previous value of X

© Janice Regan, CMPT 102, Sept Assignment operators  A = B assign value of expression B to variable A, store result in A  A += B add the value of expression B to the value of variable A, store result in A  A -= B subtract the value of expression B from the value of variable A, store result in A  A *= B multiply the value of expression B by the value of variable A, store result in A  A /= B divide the value of expression A by the value of variable B, store result in A

© Janice Regan, CMPT 102, Sept Numerical Values and Expressions  When you evaluate an expression in C you may combine two values (operands) according to a binary operation.  Two operands A and B combine with operation +  A+B  Both operands of a binary operation should have the same type (int, float, double ….)  Conversions can be done automatically or can be done explicitly by the programmer

© Janice Regan, CMPT 102, Sept Numerical Values and Expressions  In C if you use two types of operands with the same binary operator, one of the operands will be converted to the same type as the other operand.  When evaluating expressions such conversions and promotions are automatically performed according to a defined set of rules, the usual arithmetic conversions.  To avoid unexpected results you should understand how these conversions are done.  In some cases to be sure you get the results you want you should do conversions explicitly yourself

© Janice Regan, CMPT 102, Sept Explicit conversion: The cast operation  In C you can explicitly convert the type of a variable or expression within a larger expression using a cast operator  The value of the variable or expression is not changed  The value used in the larger expression is converted to the requested type  Sample expressions including casts  Integerone + (int)(Floatone+Floattwo)  (float)Integerone + Float1 + Float2  (double)unsigned1+(double)unsigned2 * double2

© Janice Regan, CMPT 102, Sept Conversions  When evaluating an arithmetic expression not including an assignment statement the usual arithmetic conversions are used  When executing an assignment statement (A=B) the value of expression B is placed in location A in memory.  In this case the type of variable B must be converted to the type of variable A  This can result in a loss of accuracy

© Janice Regan, CMPT 102, Sept Usual arithmetic conversions  If either operand is long double the other is converted to a long double  Otherwise, If either operand is a double the other is converted to a double  Otherwise, If either operand is a float the other is converted to a float  Otherwise, Integral promotions are performed on both operands  short integer and character variables are converted to integers if all their values can be represented as integers  Otherwise they are converted to unsigned integers

© Janice Regan, CMPT 102, Sept Usual arithmetic conversions THEN  If either operand is an unsigned long int the other is converted to an unsigned long int  Otherwise, If one operand is a long int and the other is an unsigned int then  If the long int can represent all possible values of the unsigned integer then the unsigned int is converted to a long int  Otherwise both are converted to unsigned long int  Otherwise, If one operand is a long int the other is converted to a long int  Otherwise, If one operand is a unsigned int the other is converted to a unsigned int  Otherwise, both operands have type int

© Janice Regan, CMPT 102, Sept How are conversions done 1  An integer being converted to floating point number will take on the closest number to the value of the integer with a representable value. This value may not be exactly equal to the integer value.  For example if the closest representable values to 2745 are and Then the converted value of 2745 will be  A long double being converted to a double will similarly take on the closest representable double value  A double converted to a long double has the same value.  All representable double values are representable long double values  Some long double values are exactly respresentable as double values

© Janice Regan, CMPT 102, Sept How are conversions done 2  When a float is converted to an integer the fractional part is discarded. (the result is not defined if the result can’t be represented as an integer of the specified type)  When an integer is converted to a float the value is the next higher of lower representable value.  An integer is converted to an unsigned type by “finding the smallest non-negative value that is congruent to that integer modulo one more than the largest value”  An unsigned integer converted to a signed integer is unchanged so long as it can be represented  A higher precision float converted to a lower precision float the result is the closest representable value  A lower precision float converted to a higher precision float is unchanged

© Janice Regan, CMPT 102, Sept Program Style  The language being used, in our case C++ has its own syntax (structure and rules)  Projects usually set up a standard style for program layout  Capitalization, spacing, form of comments  Specifics of chosen style not as important as the fact that there is a common style  Common style is usually more restrictive than the syntax of the language  Common style for consistency, clarity, ease of understanding and modification

© Janice Regan, CMPT 102, Sept Program Style  Bottom-line: Make programs easy to read and modify  Comments, two methods:  Comment on separate line (usually explains a block of code) /*Delimiters indicates everything between is ignored*/  Comment after line of code A = B + C; /* Comment explaining this line */  Both methods commonly used  Identifier naming  ALL_CAPS for constants  lowerToUpper for variables  Most important: MEANINGFUL NAMES!

© Janice Regan, CMPT 102, Sept Program Style for CMPT 102 (1)  One aspect of common style used for this course and in many computing based workplaces is to begin any class or method with a block of comments explaining what the class or method does, the variables and input and output, and program authorship and date.  Comments are also used to explain what each block or section within the code does.  Provide additional information in comments  Explain why, explain how this helps solve the overall problem  Do not give and English 'translation' of the code in the block of code

© Janice Regan, CMPT 102, Sept Program Style for CMPT 102 (2)  When writing programs, use comments throughout for clarity  Use the C++ form of comments  Each comment must be preceded by a //  Identifiers for Constants should include only UPPER CASE letters.  GRAVITATIONAL_CONSTANT  Identifiers for variables should begin with a lower case letters  sumOfSquares

© Janice Regan, CMPT 102, Sept Program Style for CMPT 102 (3)  Identifiers for methods should begin with an upper case letter  CalculateMean  Identifiers containing more than one word should have the second and each successive word capitalized  squareRootOfSum  All identifiers should be meaningful names that indicate what the variable they identify represents