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

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

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

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

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

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

Operators in C. Staff Incharge: S.Sasirekha

Operators. Java operators are classified into three categories:

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

JAVA OPERATORS GENERAL

Arithmetic Operators. Portability: Printing Numbers

Operators & Expressions

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Prepared by: Shraddha Modi

Fundamentals of Programming CS-110. Lecture 3

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

Operators in java Operator operands.

Expressions and Precedence. Last updated 12/10/18

Department of Computer Science

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

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

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

The Arithmetic Operators

Data Types and Variables in C language

Operators and Expressions:

Expression and Operator

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

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

SECTION II: LANGUAGE BASICS

Operators And Expressions

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

UNIT- 3 Introduction to C++

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

UNIT 3 OPERATORS. [Marks- 12]

Chapter 3: Operators, Expressions and Type Conversion

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

Chapter 4: Basic C Operators

ISA 563 : Fundamentals of Systems Programming

Information Science 1

Programming for Engineers Iteration

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

Prof. Navrati Saxena TA: Rochak Sachan

More Programming Constructs -- Introduction

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

Programming. Elementary Concepts

Basic Assignment and Arithmetic Operators

Chapter 12 Variables and Operators

1.3b Type Conversion

Computer System and programming in C

Lecture 3. More About C

Chapter 12 Variables and Operators

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

Fundamental of Programming (C)

Declaration and Memory

Fundamentals of Programming

Fundamentals of Programming Session 7

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

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

Constants and Variables

DEPARTMENT OF MATHS, MJ COLLEGE

ME 461 C review Session Fall 2009 S. Keres

CT 229. Java Syntax 26/09/2006 CT229

Computers Programming Course 6. Iulian Năstac

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

CSC 1214: Object-Oriented Programming

Chapter 3 Structure of a C Program

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

Work relative to other classes

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

Lecture 3 Tao Wang 1

Course Outline Introduction to C-Programming

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

Operators and Expressions

Programming for Engineers: Operators, Expressions, and Statem

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

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

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

Part I Part 1 Expressions

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

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

Programming in C++ 5. Integral data types

Operators. Lecture 3 COP 3014 Spring January 16, 2018

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

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

CIS 110: Introduction to Computer Programming

Chapter 2 - Introduction to C Programming

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

C expressions. (Reek, Ch. 5) 1 CS 3090: Safety Critical Programming in C

Variables and literals

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

CS313D: ADVANCED PROGRAMMING LANGUAGE

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

Infix to Postfix Conversion

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

Beginning C Programming for Engineers

Chapter 3 Structured Program Development in C Part II

Transcription:

Expressions and Statementst t Expression C Programming Lecture 6 : Operators Combination of constants,variables,operators, operators and function calls a+b 3.0*x 9.66553 tan(angle) Statement An expression terminated with a semicolon sum = x + y + z; printf( Dragons! ); Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang Assignment Operator Arithmetic ti operators The equal sign = is an assignment operator Used to give a variable the value of an expression x=34.8; sum=a+b; slope=tan(rise/run); midinit= J ; j=j+3; x=y=z=13.0; Initialization int i=0; Binary operators Addition :+ Subtraction : - Multiplication : * Division : / Modulus : % // only works for integers values Unary operators +, - Integer division 1/2 = 0 (?), 3/2 = 1 (?)

Arithmetic ti operators In binary operators If two operands are int type : the result is int type If one or two operands are floating-point type : the result is floating-point it type 2 + 3.14 2.0 + 3.14 = 5.14 12.0/5 12.0/5.0 0 = 2.4 increment/decrement t Increment operator ++ i=i+1; i+1 i++; // postfix form ++i; // prefix form decrement operator i=i-1; i--; // postfix form --I; // prefix form Difference between i++ and ++i? prefix vs. postfix Difference shows up when the operators are used as a part of a larger expression ++k : k is incremented before the expression is evaluated. k++ : k is incremented after the expression is evaluated. difference? int a; int b; int i=0, j=0; int i=0, j=0; a= (++i) + (++j); b= (i++) + (j++); General syntax Shorthand Operators variable = variable op expression; is equivalent to variable op= expression; Common forms +=, -=, *=, /=, %= Examples j=j*(3+x); j j *= 3+x; a=a/(s-5); a /= s-5;

Precedence, Associativity it of Operators Operator Precedence determines the order in which operations are performed operators with higher precedence are employed first. precedence 1 st unary +, unary - 2 nd binary * / % 3 rd binary + - Operator Associativity operators if two operators in an expression have the same precedence, associativity determines the direction in which the expression will be evaluated. *, /, % : L -> R +, - (bin): L -> R = : R -> L +, - (unary) : R -> L Precedence Examples Evaluation Order 1 + 2 * 3-4 -> 1 + 6-4 -> 7-4 -> 3 use parenthesis to force a desired order of evaluation (1 + 2) * (3 4) Associativity it Examples Bitwise i Operators Left associativity a / b * c (a / b) * c Right associativity - + - a - (+ (- a)) shift/lo gic shift op. bit op. Op. name usage type output left shift a<<n integer right shift a>>n integer Shift bits of a to left by nbit Newly created bits will be 0 Shift bits of a to right by n bit Newly created bits will be 0 bit AND a & b integer AND of a s and b s each bit bit OR a b integer OR of a s and b s each bit bit XOR a ^ b integer XOR of a s and b s each bit 1 s complement ~a integer 1 s complement of a

Truth/False Table Bitwise i Operators Examples a b a&b a b a^b 0 0 0 0 0 0 1 0 1 1 1 0 0 1 1 1 1 1 1 0 a ~a 0 1 1 0 11 = 0000 0000 0000 1011 17 = 0000 0000 0001 0001 11 << 2 0000 0000 0000 1011 << 2 = 0000 0000 0010 1100 = 44 17 >> 3 0000 0000 0001 0001 >> 3 = 0000 0000 0000 0010 = 2 output: 11 << 2 = 44 17 >> 3 = 2 expression value result a 0x1f05 0001 1111 0000 0101 b 0x31a1 1 0011 0001 1010 0001 ~a 0xe0fa 1110 0000 1111 1010 a & b 0x1101 0001 0001 0000 0001 output: 1f05 & 31a1 = 1101 1f05 31a1 = 3fa5 1f05 ^ 31a1 = 2ea4 ~1f05 = ffffe0fa a b 0x3fa5 0011 1111 1010 0101 a ^ b 0x2ea4 0010 1110 1010 0100

Relational l Operators meaning 연산자자료형결과값 Equal a == b not equal less than less than or equal to greater than a!= b a<b a <= b a > b integer or greater than a >= b or equal to 1(=true) if a is equal to b otherwise 0(=false) 1(=true) if a is not equal to b otherwise 0(=false) 1(=true) if a is less than b otherwise 0(=false) 1(=true) if a is less than or equal to b otherwise 0(=false) 1(=true) if a is greater than b otherwise 0(=false) 1(=true) if a is greater than or equal to b otherwise 0(=false) output: (10 > 11) = 0 (10 >= 11) = 0 (10 == 11) = 0 (10!= 11) = 1 (10 < 11) = 1 (10 <= 11) = 1 Logical Operators #include <stdio.h> op name expression meaing logical NOT! a If a is false, then 1(=true), otherwise 0(=false) logical AND logical OR a && b a b If both a and b are true, then 1(=true), otherwise 0(=false) If either a or b is true, then 1(=true), otherwise 0(=false) int main() { int score; printf("score?"); scanf("%d",&score); if (score >= 90 && score <=100) printf("your grade is A.\n"); if (score >= 80 && score < 90) printf("your grade is B.\n"); if (score >= 70 && score < 80) printf("your grade is C.\n"); if (score >=60 && score < 70) printf("your grade is D.\n"); if (score < 60) printf("your grade is F.\n"); } return 0;

Automatic ti Type Conversion Automatic ti Type Conversion What happens when expression has mixture of different data types. double x=1.2; float y=0 0.0; 0; int i=3; int j=0; j=x+i; /* (temporary copy of)i will be converted to double type before + operation. the value of i in memory is unchanged */ y=x+i; printf( j=%d, y=%f\n,j,y); lower types are promoted to higher types. The expression itself will have the type of its highest operand. The type hierarchy is as follows long double double float int short, char If either operand is long double, convert the other to long double Otherwise, if either operand is double, convert the other to double Otherwise, if either operand is float, convert the other to float Otherwise, convert char and short to int Automatic Type Conversion with assignment operator Example Type casting Programmers can enforce type conversion to a variable double x=5.5; int y=3; y=x; /* x will be converted to int type */ x=y; /* y will be converted to double type */ Ex1) double x=3.5; double y=2.7; double below_point; below_point = x*y - (int)(x*y) ; Ex2) double x=3.5; printf( integer number of x = %d\n,(int)x);