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

Similar documents
Chapter 3 Structure of a C Program

Operators and Expressions:

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

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

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

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Operators. Java operators are classified into three categories:

Operators & Expressions

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

LECTURE 3 C++ Basics Part 2

Expressions and Precedence. Last updated 12/10/18

Arithmetic Operators. Portability: Printing Numbers

Operators in C. Staff Incharge: S.Sasirekha

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

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

Chapter 3: Operators, Expressions and Type Conversion

Data Types and Variables in C language

Information Science 1

Declaration and Memory

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

The Arithmetic Operators

Unit 3. Operators. School of Science and Technology INTRODUCTION

Programming for Engineers: Operators, Expressions, and Statem

UNIT- 3 Introduction to C++

Operators in java Operator operands.

ECE 122 Engineering Problem Solving with Java

Programming in C++ 5. Integral data types

SECTION II: LANGUAGE BASICS

Lecture 3. More About C

1.3b Type Conversion

COMP Primitive and Class Types. Yi Hong May 14, 2015

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

Java enum, casts, and others (Select portions of Chapters 4 & 5)

More Programming Constructs -- Introduction

Chapter 2: Using Data

Department of Computer Science

Operators And Expressions

Chapter 4: Basic C Operators

Lecture Set 4: More About Methods and More About Operators

Computer System and programming in C

Reserved Words and Identifiers

CHAPTER 3 Expressions, Functions, Output

Basics of Programming

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

Operators and Expressions

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.

Structures, Operators

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

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

CS313D: ADVANCED PROGRAMMING LANGUAGE

3. Java - Language Constructs I

Visual C# Instructor s Manual Table of Contents

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

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

Operators. Java Primer Operators-1 Scott MacKenzie = 2. (b) (a)

JAVA OPERATORS GENERAL

Prof. Navrati Saxena TA: Rochak Sachan

These are reserved words of the C language. For example int, float, if, else, for, while etc.

Chapter 2: Using Data

Lecture Set 4: More About Methods and More About Operators

In Fig. 3.5 and Fig. 3.7, we include some completely blank lines in the pseudocode for readability. programs into their various phases.

Information Science 1

C Programming

Programming for Engineers Iteration

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

CIS133J. Working with Numbers in Java

CS110: PROGRAMMING LANGUAGE I

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Programming in C++ 6. Floating point data types

UNIT 3 OPERATORS. [Marks- 12]

Data Conversion & Scanner Class

Basics of Java Programming

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

Part I Part 1 Expressions

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

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Engineering Computing I

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

Fundamentals of Programming Session 7

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

Structured Programming. Dr. Mohamed Khedr Lecture 4

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

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

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

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

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

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

Chapter 7. Expressions and Assignment Statements ISBN

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

Chapter 12 Variables and Operators

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

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

1 Lexical Considerations

C-LANGUAGE CURRICULAM

Lecture 3 Operators MIT AITI

ISA 563 : Fundamentals of Systems Programming

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Transcription:

Performing Computations C provides operators that can be applied to calculate expressions: tax is 8.5% of the total sale expression: tax = 0.085 * totalsale Need to specify what operations are legal, how operators evaluated, etc. C operations are referred to as Expressions Outline II. Program Basics G. Expressions 1. Primary identifiers, constants, parenthesized expressions 2. Unary postfix: function call, increment, decrement prefix: sizeof, unary +, unary -, cast, increment, decrement 3. Binary arithmetic ops: + - * / % assignment shorthand assignment Outline (cont) II. Program Basics 4. Casting implicit promotion hierarchy explicit 5. Side effects 6. Resolving Expressions Precedence Associativity H. Statements 1. Expression 2. Compound Expressions in C total * TAXRATE Consists of a sequence of operators and operands operands: total, TAXRATE can be expressions themselves operator: * operation calculating a result based on the operand(s) Some Expression Formats primary expressions that directly map to a value unary prefix: operator operand postfix: operand operator binary operand1 operator operand2 ternary (operator in two parts) Types for Operands Operators only apply to certain types e.g., addition applies to numbers Each operand must be of the appropriate type Operators calculate a value Value can then be used as operands to other operators 1

Primary Expressions identifiers variable defined constant literal constants operands can be values of the appropriate type operator can have constant (e.g., 5, -34) as operand Binary Expressions expr1 op expr2 arithmetic operators + - binary addition (e.g., 5 + 3 result: 8) - - binary subtraction (e.g., 5-3 result: 2) * - multiplication (e.g., 5 * 3 result: 15) / - division (e.g., 5 / 3 result: 1) % - remainder (e.g., 5 % 3 result: 2) arithmetic operators apply to number types, result type depends on operand types Arithmetic Operators Promotion Hierarchy Two int operands -> int result Two float operands -> float result / - division operator, is whole number division for ints, floating point division for other operands % - remainder operator, only works for int values Mixed operands (float/int) - result produced is based on a promotion hierarchy, result cast to higher type in hierarchy e.g., 5 / 3.0 produces 1.666666 double float long int int short char Operands of the lower type can be implicitly converted to the higher type (in C) Occurs when an operator expects the higher type (adding together two floats when one is an int) Assignment Expression varname = expr value of expr determined result of expression is the value of expr as a side effect the value calculated is also stored in the named variable (the current value stored in the variable is replaced) current value of total is 5 total = 3 replaces the value 5 with the value 3 in the memory location corresponding to total Assignment Expression (cont) varname = expr expr value must be of the proper type to be stored in the variable (error otherwise) the value may be converted automatically using the promotion hierarchy float f; f = 3; /* 3 cast from an integer to a float value */ 2

Shorthand Assignments += -= *= /= %= x += 3 is a shorthand for x = x + 3 assuming x is 7 before x += 3 is calculated, the right-hand side x + 3 is calculated, producing 10, this value is then stored in x, and the result of the expression is 10 thus, these operators also have side effects Unary Expressions Postfix fname(arglist) - function call subprogram is called (invoked) which performs some set of computations and returns a value (more later) Prefix sizeof (expr) - expr can be any primary expression or the name of a type, operator returns num bytes used to represent type (int) + expr - unary plus (applies to numbers, no effect) - expr - unary minus (inverts sign of numbers) Cast Operator (Unary Prefix Op) (TypeName) expression conversion forces expression value from current type to the provided type (float) 5 results in 5.0 especially useful for division totalscore, totalgrades (ints) totalscore / totalgrades produces integer (float) totalscore / totalgrades casts totalscore to a floating point number, then division occurs Prefix Increment, Decrement ++variable - prefix increment --variable - prefix decrement prefix unary operators ++x is a shorthand for x = x + 1 similarly --x is a shorthand for x = x - 1 apply to integral values (char, int) have side effect of changing value of variable Postfix Increment, Decrement variable++ - postfix increment variable-- - postfix decrement postfix unary operators similar to prefix versions BUT different in the value they produce x++ is a shorthand for the statement x = x + 1 but the result of x++ is the value of x BEFORE the value is changed assume x is 8, x++ changes x to 9 but returns the value 8 the postfix decrement operator works similarly Side Effects Certain operations not only produce values, but also perform other actions assignment statements - change the value of variable on left-hand side of operator scanf operator - returns a value (number of arguments read), but also changes variables in scanf expression scanf( %d,&total) changes total 3

Complex Expressions Expressions can be composed to produce more complex expressions operand of expression can be an expression: total = totalsale + TAXRATE * totalsale three operators, =, +, *, how is this expression resolved?? In C, TAXRATE * totalsale is calculated, then totalsale is added to this value, then this value is stored in total But why?? Resolving Complex Expressions C has precedence, associativity rules it uses to determine the order operators are evaluated in Precedence: some operators are executed before others Associativity: when more than one operator in an expression has the same precedence then associativity rules are used to order Operator Precedence 18: Identifier, Constant, Parenthesized Expression 17: Function call 16: Postfix increment/decrement, 15: Prefix increment/decrement, sizeof, unary +, unary - 14: (Type) - cast operator 13: * / % 12: + - 2: = += -= *= /= %= Using Operator Precedence total = totalsale + TAXRATE * totalsale * has highest precedence, so TAXRATE * totalsale is calculated first + has next highest precedence, so the result of TAXRATE * totalsale is added to totalsale = has the lowest precedence, so the value calculated in the previous operations is stored in total Parenthesized Expressions What if the precedence order is not what you want? sale1 + sale2 * TAXRATE in this case the multiplication would happen first, but you might want the addition first answer: parenthesize the expressions you want to happen first result: (sale1 + sale2) * TAXRATE parenthesized expressions have highest precedence Programming Tip: Parenthesizing Does not hurt to include parentheses for all expressions Thus you can guarantee the order of evaluation total = totalsale + TAXRATE * totalsale becomes total = (totalsale + (TAXRATE * totalsale)) 4

Associativity Rules determining how to evaluate expressions containing more than one operator with the same precedence is 5-4 - 3 ((5-4) - 3) or (5 - (4-3)) its important because the values may differ (e.g., -2 or 4 for the above possibilities) Left associativity: operators are evaluated left to right Right associativity: evaluated right to left Operator Associativity 18: Identifier, Constant, Parenthesized Expression 17: Function call (LEFT) 16: Postfix increment/decrement (LEFT) 15: Prefix increment/decrement, sizeof, unary +, unary - (RIGHT) 14: (Type) - cast operator (RIGHT) 13: * / % (LEFT) 12: + - (LEFT) 2: = += -= *= /= %= (RIGHT) Using Operator Associativity Evaluate operators by precedence When evaluating operators with the same precedence, consult associativity rules 5-4 - 3, two - operators, so associativity (left) is used 5-4 - 3 evaluates to ((5-4) - 3) or -2 Again, use parentheses to make sure things are evaluated the way you expect Composing Assignments x = y = 3 (x = (y = 3)) with Right associativity Ok to do, but better to avoid x is 4, what is the result of x = y = x + 3? x + 3 evaluated (producing 7), this value stored to y, then the result stored back to x Multiple Assignments In ANSI C, changing a variable multiple times in the same expression produces an undefined result (b-- + b--) is undefined, C may execute both b-- operators together, the left b-- first, or the right b-- first solution: better not to do it Statements Programs consist of sequences of statements Expressions can be used as part of various types of statements Expressions can also be used as stand-alone statements generally assignment expressions are issued as separate statements 5

Expression Statements Format: Expression ; When the statement is reached in the program the expression is evaluated Assignment statements cause side effects (change the value of variables) printf and scanf are actually functions that we call (and produce values), so these are expression statements Compound Statements A block of code containing zero or more statements Contained between { and } Format: { Local Declarations Statements } Compound Statements (cont) { int x; /* local declaration */ x = 3; /* statements */ y = x + 4; printf( %d\n,y); } Block of statements treated as a unit Compound Statements (cont) Local declarations exist during the execution of the block/compound statement Declarations are made one at a time Statements are executed from the first to the last Declarations go away once the statements in the block are executed 6