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

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

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

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

Expressions and Precedence. Last updated 12/10/18

Operators in C. Staff Incharge: S.Sasirekha

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

JAVA OPERATORS GENERAL

Operators and Expressions:

Department of Computer Science

Data Types and Variables in C language

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

Information Science 1

Operators. Java operators are classified into three categories:

Arithmetic Operators. Portability: Printing Numbers

Operators. Lecture 3 COP 3014 Spring January 16, 2018

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

Operators in java Operator operands.

Operators & Expressions

LECTURE 3 C++ Basics Part 2

Unit-2 (Operators) ANAND KR.SRIVASTAVA

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

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

Computers Programming Course 6. Iulian Năstac

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

Infix to Postfix Conversion

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

UNIT- 3 Introduction to C++

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

The Arithmetic Operators

Chapter 3: Operators, Expressions and Type Conversion

Prepared by: Shraddha Modi

UNIT 3 OPERATORS. [Marks- 12]

Prof. Navrati Saxena TA: Rochak Sachan

SECTION II: LANGUAGE BASICS

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

Expression and Operator

Engineering Computing I

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

Programming in C++ 5. Integral data types

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

CSC 1214: Object-Oriented Programming

DEPARTMENT OF MATHS, MJ COLLEGE

JAVA Programming Fundamentals

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Chapter 2: Using Data

Java Programming Fundamentals. Visit for more.

Lecture 02 C FUNDAMENTALS

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

Computer System and programming in C

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

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

Fall, 2015 Prof. Jungkeun Park

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

CT 229. Java Syntax 26/09/2006 CT229

Programming in C++ 6. Floating point data types

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

ISA 563 : Fundamentals of Systems Programming

C Programming Language (Chapter 2 of K&R) Variables and Constants

Programming for Engineers: Operators, Expressions, and Statem

Work relative to other classes

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

Part I Part 1 Expressions

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

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

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

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

UNIVERSITY OF LIMERICK OLLSCOIL LUIMNIGH COLLEGE OF INFORMATICS & ELECTRONICS DEPARTMENT OF ELECTRONIC & COMPUTER ENGINEERING

Reserved Words and Identifiers

Expressions and Casting

Fundamentals of Programming

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

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Programming. Elementary Concepts

Declaration and Memory

UNIT IV 2 MARKS. ( Word to PDF Converter - Unregistered ) FUNDAMENTALS OF COMPUTING & COMPUTER PROGRAMMING

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

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Fundamentals of Programming CS-110. Lecture 3

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

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Basic Assignment and Arithmetic Operators

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Java Notes. 10th ICSE. Saravanan Ganesh

Java Primer 1: Types, Classes and Operators

Computational Applications in Nuclear Astrophysics using Java Java course Lecture 2

Basics of Java Programming

Computer Programming CS F111

CS313D: ADVANCED PROGRAMMING LANGUAGE

Structures, Operators

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Basic Science and Humanities

Basics of Programming

PESIT Bangalore South Campus Hosur Road (1km before Electronic City), Bengaluru Department of Basic Science and Humanities

3. Java - Language Constructs I

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

Information Science 1

Variables and Operators 2/20/01 Lecture #

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

Fundamental of Programming (C)

Transcription:

Informatics Ingeniería en Electrónica y Automática Industrial Operators and expressions in C Operators and expressions in C Numerical expressions and operators Arithmetical operators Relational and logical operators Bitwise operators Assignment operators and expressions Other operators Conditional operator Comma operator Address and indirection operators sizeof operator Precedence and order of evaluation Type conversions 2 1

Numerical expressions and operators A numerical expression is formed by Operators Operands An operator is a symbol that indicates how the operands must be processed in the expressions An operand is the object that is processed: variables, constants, etc. 3 Arithmetical operators If the operands are of different types, the lower precision ones are transformed to the greater type OPERATOR OPERATION OPERANDS + Addition Integers or reals - Subtraction Integers or reals * Multiplication Integers or reals / Division Integers or reals % - Modulus: Remainder of integer division Unary minus (sign change) Integers Just one operand (integer or real) 4 2

Relational and logical operators (I) Operands can be of any type but the result is always an integer with just two possible values: 1 (true) or 0 (false) LOGICAL OPERATORS OPERATOR && OPERATION AND RESULT Logical AND. Result is 1 if both operands are non-zero (ie. if one or both are 0, result is 0). Logical OR. Result is 1 if any of the operands is non-zero (ie. result is 0 just when both operands are 0).! Logical NOT. Result is 1 if the operand is 0, and 0 otherwise. 5 Relational and logical operators (II) OPERATOR RELATIONAL OPERATORS OPERATION AND RESULT < Result is 1 if the left operand is lower than the right one; 0 otherwise. > Result is 1 if the left operand is greater than the right one; 0 otherwise <= Result is 1 if the left operand is lower than or equal to the right one; 0 otherwise. >= Result is 1 if the left operand is greater than or equal to the right one; 0 otherwise.!= Result is 1 if the operands are different; 0 otherwise. == Result is 1 if the operands are equal; 0 otherwise. 6 3

Bitwise operators They operate with the individual bits of the operands, which must be integer type (int or char) OP. & AND between bits of the operands OPERATION AND RESULT OR between bits of the operands ^ XOR (Exclusive OR) between bits of the operands ~ 1 Complement of the operand (at the right of the operator) << >> Left shift of the left operand by the number of positions given by the positive right operand (filling vacants with zeros). Right shift of the left operand by the number of position given by the positive right operand. If the operand is unsigned fills vacants with zeros, if signed, fill vacants with sign bit (arithmetic shift). 7 Assignment operators (I) They assign values to one variable Simple assignment operator = a = 2 Increment ++ and decrement -- operators Increment/decrement by 1 the value of a variable in a expression ++variable. First increment, later use. x = 1; y = ++x; // x is now 2, y is also 2 y = x++; // x is now 3, y is 2 variable++. First use, later increment x = 3; y = x--; // x is now 2, y is 3 y = --x; // x is now 1, y is also 1. 8 4

Assignment operators (II) Operation and asignment variable (op)= expresion; Is equivalent to variable = variable (op) expresion; (op) is the assignment operator expresion is the expression that will be evaluated along with variable to obtain its new value i += 2 i = i + 2 are equivalent expressions 9 Assignment operators (III) OP. OPERACIÓN Y RESULTADO *= Multiplication and assignment. C *= A equiv. to C = C*A /= Division and assignment. C /= A equiv. to C = C/A %= Modulus and assignment. C %= A equiv. to C = C%A += Addition and assignment. C += A equiv. to C = C+A -= Subtraction and assignment. C -= A equiv. to C = C-A 10 5

Assignment operators (IV) OP. OPERATION (bit level) AND RESULT <<= Left shift AND assignment C <<= 2 equiv. to C = C<<2 >>= Right shift AND assignment C >>= 2 equiv. to C = C>>2 &= Bitwise AND and assignment C &= 2 equiv. to C = C&2 = Bitwise OR and assignment C = 2 equiv. to C = C 2 ^= Bitwise XOR and assignment. C ^= 2 equiv. to C = C^2 11 Other operators (I) Condicional Operator «?:» expr1? expr2 : expr3 If expr1 is true, then expr2, is evaluated If expr1 is false, then expr3 is evaluated Ex. (a >= b)? puts( a>=b ) : puts( b>a ); Comma Operator «,» Mostly used in the for statement. When used to concatenates expressions and variables or to separates elements in argument lists is NOT an operator (do not guarantee left to right evaluation) 12 6

Other operators (II) Address operator «&» &variable obtains the memory address of variable Indirection operator «*» *identifier refers to the content of memory address identifier Operator «sizeof» Returns the number of bytes that the operand occupies in memory 13 Precedence and order of evaluation (I) Order OPERATORS ASOCIATIVITY 1º () []. -> sizeof Left to Right 2º -! * ++ -- (tipo) Right to Left 3º * / % Left to Right 4º + - Left to Right 5º << >> Left to Right 6º < <= > >= Left to Right 7º ==!= Left to Right 8º & Left to Right 9º ^ Left to Right 10º Left to Right 11º && Left to Right 12º Left to Right 13º?: Right to Left 14º = *= /= %= += -= <<= >>= &= = ^= Right. to Left 15º, Left to Right 14 7

Precedence and order of evaluation (II) Precedence and order of evaluation (table) Operators in the same line have the same priority Priority decreases from top to bottom Parenthesis are evaluated from inside to outside (as usual) Some ambiguities may exists dependin on the compiler USE PARENTHESIS!! when doubting 15 Type conversions (I) In expressions operands are type-converted automatically With reals involved, all are converted to the high precision one Real constants are double by default char and short are converted to int or to unsigned int With integers involved, all are converted to the longest one EXAMPLE long a char b; int c, f; float d; f = a + b*c/d ; b is converted c type (int) and b*c is int b*c is converted to float and divided by d a is converted to float and added to b*c/d. float a+b*c/d is converted to int (eliminating fractional part) and saved in integer f Better try not to mix types 16 8

Type conversions (III) Explicit conversion: «(cast)» operator (newtype)expresion; Example: 7/2 gives 3 as result (float)7/2 gives 3.5 as result 17 9