Expressions An expression is a sequence of operands and operators that reduces to a single value. 5 + 2 expression operator operand An operator is a language-specific.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

Operators and Expressions Rohit Khokher. Operators and Expression OperatorsSymbols Arithmetic+ - * / % Relational >= == != Logical&& || ! Assignment=
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
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.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
0 Chap. 2. Types, Operators, and Expressions 2.1Variable Names 2.2Data Types and Sizes 2.3Constants 2.4Declarations Imperative Programming, B. Hirsbrunner,
JavaScript, Third Edition
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Expressions, Data Conversion, and Input
Operaciones y Variables
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Operators Using Java operators An operator takes one or more arguments and produces a new value. All operators produce a value from their.
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.
CH2 – Using Data. Constant Something which cannot be changed Data Type Format and size of a data item Intrinsic Data Types Pg. 47 – Table 2-1 Basic ones.
Chapter 2: Using Data.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
OPERATORS.
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
1 C++ Data Types structured array struct union class address pointer reference simple integral enum char short int long bool floating float double long.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
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.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
Recap……Last Time [Variables, Data Types and Constants]
Operators & Expressions
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.
C-1 9/30/99 CSE / ENGR 142 Programming I Arithmetic Expressions © 1999 UW CSE.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
1 Chapter 3 – Operators and Expressions Outline 3.1Introduction 3.2Arithmetic operators 3.3Relational operators 3.4Logical operators 3.5Assignment operators.
ISBN Chapter 7 Expressions and Assignments Statements.
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.
INSPIRING CREATIVE AND INNOVATIVE MINDS
Object Oriented Programming
Assignment and Arithmetic expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Conversions of the type of the value of an expression
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Chapter 7 Additional Control Structures
Chapter-3 Operators.
Associativity and Prescedence
Expressions and Assignment
Chapter 3 Operators and Expressions
Data Types and Expressions
Chapter 4: Expression and Operator
OPERATORS in C Programming
Operator King Saud University
Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.
Data Types and Expressions
OPERATORS in C Programming
Data Types and Expressions
Presentation transcript:

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 syntactical token that requires an action to be taken. There is no limit to the number of operator and operand sets that can be combined to for an expression. An operand receives an operator’s action. There may be one, two or more operands in an expression. An operand may be constants, variables or other arithmetic expressions.

Expressions can be classified into several different formats Primary Postfix Unary Binary Ternary Assignment Comma identifer, constant, or parenthetical expression primary expression operator postfix expression unary expression operator binary expression operator expression operator expression variable operator expression operator expression Expressions Primary expressions are the most elementary type. A primary expression consists of only one operand with no operator. It can be either a name, a constant or a parenthetical expression. identifermiles price calc_tax constant56 ‘Z’ parenthetical expression( 4 * 9 / 2 )

Expressions Binary Expressions are formed by an operand-operator - operand combination. Some of the binary operators The value of these expressions are the same type as the type of the operands in the expression. The result of the modulus operator can only be an integer.

Expressions Examples of binary expressions 2 + 1/* the result is 3 */ 5 * 6 /* the result is 30 */ 5 / 2 /* the result is 2 */ 3 / 5 /* the result is 0 */ 7 % 4 /* the result is 3 */ Assignment Expressions evaluates the operand on the right side of the assignment operator ( = ) and places its value in the contents of the memory cell represented by the variable on the left hand side. The operand on the left hand side of the assignment operator must be a variable. The value of the total expression is the value of the expression on the right of the assignment operator. The assignment the value to the variable on the left hand side is called the side effect. The expression x = 3 * 2 is read, x “gets” or “takes the value of” rather than “equals” because it is not equivalent to the “equal sign” of mathematics.

Expressions Compound assignment is shorthand notation for a simple assignment. It requires that the left operand be repeated as a part of the right expression. There are 5 compound assignments operators *=, /=, %=, +=, -=. Example x = x + 2 can also be expressed as x += 2 both of these expression achieve the same result and value. Example int x = 10; int y = 5; printf(“x = %d and y = %d\n”, x,y); printf(“the value of x *= y is %d\n”, x *= y); printf(“the value of x is now %d\n”, x,); the output is x = 10 and y = 5 the value of x *= y is 50 the value of x is now 50

Postfix expressions consists of one operand, which must be a primary expression, followed by one operator. Two of the postfix operators are the increment and decrement operators. Expressions In the postfix increment the value of the expression is the current value of the variable and the side effect is that the variable is increased by one. In the postfix decrement the value of the expression is the current value of the variable and the side effect is that the variable is decreased by one. Example a++

Unary expressions consists of one operator and one operand. Expressions Example ++a In the prefix increment the value of the expression is the current value of the variable plus one and the side effect is that the variable is increased by one. In the prefix decrement the value of the expression is the current value plus one of the variable and the side effect is that the variable is decreased by one.

Expressions The sizeof operator looks like a function but it is actually an operator. Its purpose matches its name: It tells you the size, in bytes, of whatever type is specified. Example sizeof( int ); /* returns the number of bytes for a integer type */ sizeof( ); /* returns the number of bytes needed to hold the expression */ Precedence Precedence is used to determine the order in which different operators in a complex expression are evaluated How should the following expression be evaluated: * 4 should you do the addition first then the multiplication or the multiplication and then the addition. The difference is the result.

Precedence Rules for evaluating expressions: 1)Parentheses rule: All expressions in parentheses must be evaluated separately. Nested parenthesized expressions must be evaluated from the inside out, with the innermost expression evaluated first. 2)Operator precedence rule: operators in the same expression are evaluated in the following order: (see inside cover of text). 3)Associativity rule: unary operators in the same subexpression and at the same precedence level (such as + and - ) are evaluated right to left ( right associativity ). Binary operators I the same subexpression and at the same precedence level (such as *, /, and % ) are evaluated left to right (left associativity ). (see inside cover of text). Expressions Use extra parentheses to clarify the order of evaluation. 3 * 8 / 4 % 4 * 5 all operators are at the same precedence level so associativity determines the evaluation should be done from left to right. ( ( ( ( 3 * 8 ) / 4 ) % 4 ) * 5 )

Expressions Mixed type Expressions these are expressions which have two or more operands with different types. When this happens what it the type of the resulting value of the expression. Example in the above example is the resulting value a floating point or 5.5 or an integer 5 ? C will automatically convert any intermediate values to the proper so that the expression can be evaluated. When C automatically converts a type from on format to another, it is known as implicit type conversion. Rules for conversion: 1)C uses the rule that in all expressions except assignments, any implicit type conversion will always be made to the more general type according to the promotion order shown below. 2With an assignment expression the final expression value must have the same type as the left operand or the operand that receives the value.

Expressions char Long double float unsigned long int unsigned int short Promotion Hierarchy Using this hierarchy, if we were to add an integer and a double and store the result into and integer, we would first convert the integer in the binary + expression to a double, because double is higher in the promotion hierarchy, and then after the addition, convert the result back into and integer for assignment to the integer variable.

Expressions Example of implicit type conversion: char ch; int i result; float f; double d; result = (ch / i ) + ( f * d ) - ( f + i ) ; intdoublefloat double int double

Expressions Explicit type conversion ( cast operator ) The explicit type conversion uses the cast expression operator. To cast data from one type to another your specify the new type in parentheses before the value you want converted. Example to convert the integer a to float: ( float ) a the operand must be a unary expression: Example to cast the expression x + y as a double: ( double ) ( x + y ) Example int a, ; float b; a = 5; b = ( float ) ( a / 10 ); /* b receives 0.0 */ b = ( float ) a / 10; /* b receives 0.5 */ a is explicitly converted to a float which causes 10 to be implicitly converted to a float which results in a value of type float.