Expressions and Precedence. Last updated 12/10/18

Similar documents
Information Science 1

GO - OPERATORS. This tutorial will explain the arithmetic, relational, logical, bitwise, assignment and other operators one by one.

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

A flow chart is a graphical or symbolic representation of a process.

Operators. Java operators are classified into three categories:

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Basic operators, Arithmetic, Relational, Bitwise, Logical, Assignment, Conditional operators. JAVA Standard Edition

Informatics Ingeniería en Electrónica y Automática Industrial

Operators & Expressions

Operators in C. Staff Incharge: S.Sasirekha

Computers Programming Course 6. Iulian Năstac

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

JAVA OPERATORS GENERAL

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

UNIT- 3 Introduction to C++

Chapter 3 Structure of a C Program

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 1 IDL Operators

Chapter 3: Operators, Expressions and Type Conversion

Expression and Operator

Department of Computer Science

Unit 3. Operators. School of Science and Technology INTRODUCTION

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Operators and Expressions:

SECTION II: LANGUAGE BASICS

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators in java Operator operands.

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

LECTURE 3 C++ Basics Part 2

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

QUIZ: What value is stored in a after this

Data Types and Variables in C language

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

C/Java Syntax. January 13, Slides by Mark Hancock (adapted from notes by Craig Schock)

C/Java Syntax. Lecture 02 Summary. Keywords Variable Declarations Data Types Operators Statements. Functions. if, switch, while, do-while, for

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

ISA 563 : Fundamentals of Systems Programming

UNIT 3 OPERATORS. [Marks- 12]

Programming in C++ 5. Integral data types

Lesson #3. Variables, Operators, and Expressions. 3. Variables, Operators and Expressions - Copyright Denis Hamelin - Ryerson University

Program Flow. Instructions and Memory. Why are these 16 bits? C code. Memory. a = b + c. Machine Code. Memory. Assembly Code.

Operators and Type Conversion. By Avani M. Sakhapara Assistant Professor, IT Dept, KJSCE

Infix to Postfix Conversion

Part I Part 1 Expressions

Writing Program in C Expressions and Control Structures (Selection Statements and Loops)

C Language Summary. Chris J Michael 28 August CSC 4103 Operating Systems Fall 2008 Lecture 2 C Summary

Operators And Expressions

Lecture 3. More About C

DEPARTMENT OF MATHS, MJ COLLEGE

Expressions and Statementst t. Assignment Operator. C Programming Lecture 6 : Operators. Expression

Basics of Programming

OBJECT ORIENTED PROGRAMMING

ME 461 C review Session Fall 2009 S. Keres

C OVERVIEW BASIC C PROGRAM STRUCTURE. C Overview. Basic C Program Structure

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following g roups:

C OVERVIEW. C Overview. Goals speed portability allow access to features of the architecture speed

Arithmetic Operators. Portability: Printing Numbers

Chapter 2: Using Data

Function I/O. Function Input and Output. Input through actual parameters. Output through return value. Input/Output through side effects

Part I Part 1 Expressions

Prof. Navrati Saxena TA: Rochak Sachan

SOFTWARE DEVELOPMENT 1. Operators 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

Chapter 12 Variables and Operators

Function I/O. Last Updated 10/30/18

CS113: Lecture 3. Topics: Variables. Data types. Arithmetic and Bitwise Operators. Order of Evaluation

Engineering Computing I

Fundamentals of Programming CS-110. Lecture 3

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Fall, 2015 Prof. Jungkeun Park

Sir Muhammad Naveed. Arslan Ahmed Shaad ( ) Muhammad Bilal ( )

Reserved Words and Identifiers

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

The New C Standard (Excerpted material)

bitwise inclusive OR Logical logical AND && logical OR Ternary ternary? : Assignment assignment = += -= *= /= %= &= ^= = <<= >>= >>>=

Guide for The C Programming Language Chapter 1. Q1. Explain the structure of a C program Answer: Structure of the C program is shown below:

C/C++ Programming Lecture 7 Name:

Review of the C Programming Language for Principles of Operating Systems

Variables and literals

Perl: Arithmetic, Operator Precedence and other operators. Perl has five basic kinds of arithmetic:

Structures, Operators

XC Specification. 1 Lexical Conventions. 1.1 Tokens. The specification given in this document describes version 1.0 of XC.

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

RUBY OPERATORS. Ruby Arithmetic Operators: Ruby Comparison Operators:

Prepared by: Shraddha Modi

Fundamentals of Programming

Applied Computer Programming

Lecture 3 Operators MIT AITI

std::cout << "Size of long = " << sizeof(long) << " bytes\n\n"; std::cout << "Size of char = " << sizeof(char) << " bytes\n";

Continued from previous lecture

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

C/C++ Programming for Engineers: Working with Integer Variables

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Quick Reference Guide

Transcription:

Expressions and Precedence Last updated 12/10/18

Expression: Sequence of Operators and Operands that reduce to a single value Simple and Complex Expressions Subject to Precedence and Associativity Six categories Primary Postfix Prefix Unary Binary Ternary 2 tj

Simple Expressions Only 1 operator a + b Complex Expressions Multiple operators 2*3/6 3 tj

Primary Expressions One operand and no operators Name a interest_rate RATE initial1 Literal 2 123.456 a hello ee1910 Parenthetical Anything in parentheses reduces to a single value (2 + 3 * 4) (a = b + c) 4 tj

Postfix Expressions One operand followed by one operator Operand must be a variable Function Call Function name is an operand (named entity) Parenthesis are the operator printf( ) Postfix increment/decrement i++ i = i + 1 j-- j = j - 1 5 tj

Postfix Expressions Some expressions have a Value and a Side Effect int j; int x; j = 5; x = j++; Value: x = 5 Side Effect: j = 6 Consider printf( %d, j++); 5 or 6? Postfix indicates to operate after the evaluation 6 tj

Prefix Expressions One operator followed by one operand Operand must be a variable Only 2 examples Prefix increment/decrement ++j j = j + 1 --k k= k - 1 7 tj

Prefix Expressions Some expressions have a Value and a Side Effect int j; int x; j = 5; x = ++j; Value: x = 6 Side Effect: j = 6 Consider printf( %d, ++j); 5 or 6? Prefix indicates to operate before the evaluation 8 tj

Unary Expressions One operator followed by one operand Operand can be any expression sizeof(int) sizeof(x) a = 5 +a -> +5 Note: the expression is modified -a -> -5 not the variable, a = 5 in both cases char x; (int) x the value is cast as an int x is not cast as an int 9 tj

Binary Expressions Operand operator operand Familiar to us: +, -, *, / New: % - modulus (remainder of a division) Subject to type limitations 10 * 2 20 10 / 2 5 true * 2 2 true / 2 0??? A * 2 130 A / 2 32??? 15.6 * 2 31.2 15.6 / 2 7.8 3 / 5 0 12 / 5 2 3 % 5 3 12 % 5 2 Modulo only operates in integers 10 tj

Binary Expressions Special binary expression - assignment variable = expression Has both a value - result of right side And a side effect places value into the variable on the left side Simple a = b + c j = j * 2 Compound *=, /=, +=, -=, %= a *= b a = a * b a += 10 a = a + 10 a -= b + c a = a (b + c) expression is evaluated first 11 tj

Ternary Expressions 12 tj

Precedence Order in which operators are evaluated In math: * and / before + and 2/3+3*4 ((2/3) + (3*4)) Associativity Order in which operators with the same precedence are evaluated In math: left to right 2 + 3 4 + 5 (((2 + 3) 4) + 5) 13 tj

Precedence Operator Description Associativity ++ -- Suffix/postfix increment and decrement Left-to-right () Function call [] Array subscripting 1. Structure and union member access -> Structure and union member access through pointer Precedence (type){list} Operator Compound literal(c99) Description Associativity ++ -- Prefix increment and decrement ++ -- Suffix/postfix increment and decrement Left-to-rightRight-to-left + - Unary plus and minus! ~ () Logical NOT and bitwise Function NOT call (type) [] Type cast 2 Array subscripting * Indirection (dereference) 1 &. Address-of Structure and union member access sizeof Size-of _Alignof -> Alignment requirement(c11) Structure and union member access through pointer 3 * / % Multiplication, division, and remainder Left-to-right (type){list} Compound literal(c99) 4 + - Addition and subtraction 5 << >> ++ Bitwise -- left shift Prefix and right increment shift and decrement Right-to-left < <= + For - relational operators Unary plus < and and respectively 6 minus > >= For relational operators > and respectively! ~ Logical NOT and bitwise NOT 7 ==!= For relational = and respectively 8 & (type) Type cast 2 Bitwise AND 9 ^ * Bitwise XOR (exclusive Indirection or) (dereference) 10 & Bitwise OR (inclusive Address-of or) 11 && Logical AND sizeof Size-of 12 Logical OR 13?: _Alignof Ternary conditional Alignment requirement(c11) Right-to-Left = Simple assignment += -= Assignment by sum and difference 14 *= /= %= Assignment by product, quotient, and remainder <<= >>= Assignment by bitwise left shift and right shift &= ^= = Assignment by bitwise AND, XOR, and OR 15, Comma Left-to-right 14 tj

Examples (ints) a = 2, b=3, c=4 1 + 2 * 3 1 + 2 * 3 / 2 -b++ a += b *= c -= 3 --a * (1 + b) / 3 c++ * b 15 tj

Examples a = 2, b=3, c=4 1 + 2 * 3 1 + (2 * 3) = 7 1 + 2 * 3 / 2 1 + ((2 *3) /2) = 1 + (6/2) = 4 same precedence Answers (L-R) -b++ -(b++) = -3 evaluates first (b is now 4) a += b *= c -= 3 c= 1, b=3, a=5 same precedence (R-L) --a * (1 + b) / 3 c++ * b --a * 4 / 3 c++ * b --a * 4 / 3 4 * b 1 * 4 / 3 4 * b Answers 4 / 3 4 *3 1-12 -11 16 tj

Precedence and Associativity For clarity and precision Use Parenthesis freely (((--a) * (1 + b) )/ 3) ((c++) * b) ((( 1 ) * ( 4 )) / 3 ) (( 4 ) * 3 ) (( 4 / 3 ) ( 12 )) Answers ( 1 12 ) -11 17 tj

Bitwise Operators ~ bitwise not inverts the individual bits in a number ~(1001 0110) 0110 1001 bitwise or ORs the individual bits (1001 1001) (1010 1100) 1011 1101 & bitwise and ANDs the individual bits (1001 1001) & (1010 1100) 1000 1000 ^ bitwise xor XORs the individual bits (1001 1001) ^ (1010 1100) 0011 0101 18 tj

Bitwise Operators >> bitwise shift right shifts the individual bits in a number to the right (1001 1001) >> 2 0010 0110 - unsigned OR 1110 0110 - signed << bitwise shift left shifts the individual bits in a number to the left (1001 1001) << 2 0110 0100 - unsigned or signed 19 tj

Logical Operators! logical not inverts the logical value!true false!34 false logical OR evaluates both sides logically then does an OR true true true 0 0 false 34 32 true && logical AND evaluates both sides logically then does an AND true && true true 1 && 0 false 34 && 32 true 20 tj

Relational Operators ==, <, >, <=, >=,!= equals, LT, GT, LE, GE, not equal evaluates the parameters by type true == true true 34 == 32 false 34!= 32 true 5 >= 5 true 21 tj