Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token.

Slides:



Advertisements
Similar presentations
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.
Advertisements

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.
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 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 =
Expressions, Data Conversion, and Input
***** SWTJC STEM ***** Chapter 2-3 cg 29 Java Operators Recall Java’s programming components: Packages - Collection of classes (Programs) Classes - Collections.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 2 part #4 Operator
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.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To be able to list and describe the six expression categories ❏ To understand.
PROCESSING Numeric Types. Objectives Be able to … Explain the difference between a literal, a variable and a constant Declare numeric variables and constants.
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.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
CSCI 3133 Programming with C Instructor: Bindra Shrestha University of Houston – Clear Lake.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 1.
Module B - Computation1/61 Module-B-Computation Variables Basic Memory Operations 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.
Programming Principles Operators and Expressions.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Prepared By: K. U. Khimani Assistant Professor IT Department.
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.
CompSci 230 S Programming Techniques
CSE 220 – C Programming Expressions.
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
INSPIRING CREATIVE AND INNOVATIVE MINDS
Computing Fundamentals
Object Oriented Programming
Relational Operations
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Programmazione I a.a. 2017/2018.
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Functions Chapter 4 Instructor: Bindra Shrestha
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Character Set Uppercase Alphabets A,
Arithmetic Expressions & Data Conversions
C Operators, Operands, Expressions & Statements
Chapter-3 Operators.
Expressions and Assignment
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 3 Operators and Expressions
Data Types and Expressions
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Chapter 4: Expression and Operator
OPERATORS AND EXPRESSIONS IN C++
OPERATORS in C Programming
Operator King Saud University
DATA TYPES There are four basic data types associated with variables:
Data Types and Expressions
OPERATORS in C Programming
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Presentation transcript:

Expressions An Expression is a sequence of operands and operators that reduces to a single value. An operator is a language-specific syntactical token that requires action to be taken. For example, multilply (*) is an operator . There is no limit to the number of operator and operand sets that you can combine to form an expression. Example: 5 + 3 * 4 The value of above expression is 17. Multiply, divide, and modulus operators have the highest priority among binary operators and have the fourth level in the Precedence Table

The modulus ( % ) operator divides the first operand by the second operand and returns the remainder rather than the quotient. For eg. 5 % 2 returns 1 For modulus operator both operands must be integer types. Contd..

Following example will general compile error because Modulus operands must be integers. #include <stdio.h> int x; int main () { x = 7.5 % 3; return 0; }

The assignment operator = assigns a value to a variable. For example, x= y If y has the value of 6, then the value of the variable y (which is 6 )is assigned to the variable x. The value of the expression x =y is also 6 The left operand must always be a single variable

A compound assignment is a shorthand notation for a simple assignment. Compound assignment operators are: *=, /=, %=, +=, and -= For example:For x*=y, equivalent simple expression is: x = x * y Similarly, x /= y is x = x/y x -= y+ 4 is x = x- ( y+ 4) x %= y is x = x % y x *= y+2 is x = x* ( y+ 2).

The postfix increment/decrement operates at the second level of the Precedence Table ( see inside front cover) a ++ Here a is an operand while ++ is a postfix operator. a++ has the same effect as a =a+1. Although the result of both expression is the same, there is a major difference.For example: If variable a contains 4 before the expression is evaluated, the value of the expression a ++ is 4 After the evaluating the expression and its side effects, then only a becomes 5.

#include <stdio.h> int main (void) /* example of Postfix increment*/ { int a; a = 5; printf("%d\t", a); printf("%d\t", a++); printf("%d\n", a); return 0; } /* Results: 5 5 6 */

Prefix increment/decrement are also shorthand notations for adding or subtracting 1 similar to the Postfix operators. The one major difference is that in Prefix operators, the effect takes place before the expression that contains the operator is evaluated. Which one to choose? When you need the values to be the current contents of a variable, use a postfix operator. When you need the values to be the new contents of the variable, use a prefix operator.

#include <stdio.h> int main (void) /* example of Prefix increment*/ { int a; a = 5; printf("%d\t", a); printf("%d\t", ++a); printf("%d\n", a); return 0; } /* Results: 5 6 6 */

The sizeof operator tell you the size, in bytes, of whatever type is specified. For example: sizeof(int), sizeof ( -746.565), sizeof operator returns different values according to the specific hardware. For example: int a; int b = sizeof(a);// will give 2 bytes in most PCs while 4 bytes for mainframes and 16 bytes for supercomputers.

Precedence is used to determine the order in which different operators in a complex expression are evaluated. 2+ 5*4 means 2 + (5 * 4) The value of above expression is 22 because multiplicity has a higher precedence. -a++ means ( - ( a ++) ) ++ is evaluated first because postfix increment has a higher precedence.

#include <stdio.h> int main (void) { int a = 2; int b = 3; int c = 4; printf ("a * b + c is: %d\n", a * b + c); printf ("a * (b + c) is: %d\n", a * (b + c)); return 0; } /* a * b + c is: 10 a * (b + c) is: 14 */

Associativity can be either from the left or the right. Associativity is used only when the operators have the same level precedence. Example of left-to-right: * / % * 4* 8 / 2 % 4 * 2 means ((((4* 8) / 2) % 4)* 2 ) Similarly for right-to left, a +=b *=c -=5 means (a +=(b *=(c -=5 )))

A Side Effect is an action that results from the evaluation of an expression. int x = 3; x = x +2; Here if the original value of x is 3, the value of the expression on the right is 5, the whole expression also has a value of 5 and as a side effect x receives the value 5.

Implicit type conversion : C will automatically convert any intermediate type values from one format to another according to the promotion hierarchy . This is done automatically by the compiler For Example : To add integer and float, first integer is converted to float and then after adding, the float will be converted to integer if the value has to be stored in as an integer.

Uses cast expression operator Example program: Explicit type conversion is when you convert data from one type to another type yourself rather than the compiler. Uses cast expression operator Example program: contd..

char aChar = '\0'; int intNum1= 100, int intNum2 = 45; double fltNum1= 100.0 , double fltNum2= 45.0; double fltNum3; fltNum3 = (double)(intNum1 / intNum2); /* result 2.00 */ fltNum3 = (double)intNum1 / intNum2; /* result 2.22 */ aChar = (char)(fltNum1 / fltNum2); /* result 2 */

Statement causes an action to be performed. An expression becomes a statement when there is a semilcolon ( ; ) at the end. A compound statement consists of zero or more statements. It is also known as block Main part of a program is an example of a compound statement