Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Similar documents
Chapter 4 Expression & Operators

Chapter 4: Basic C Operators

6-1 (Function). (Function) !*+!"#!, Function Description Example. natural logarithm of x (base e) rounds x to smallest integer not less than x

ANSI C Programming Simple Programs

Operators and Expressions:

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

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

XQ: An XML Query Language Language Reference Manual

Computer System and programming in C

Structured Programming. Dr. Mohamed Khedr Lecture 4

C Program Structures

Introduction to C++ Introduction and History. Characteristics of C++

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

3.1. Chapter 3: The cin Object. Expressions and Interactivity

Data Types and Variables in C language

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

Fundamentals of Programming

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

CT 229 Java Syntax Continued

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

Computer Science & Engineering 150A Problem Solving Using Computers

Basic Assignment and Arithmetic Operators

CSI31 Lecture 5. Topics: 3.1 Numeric Data Types 3.2 Using the Math Library 3.3 Accumulating Results: Factorial

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

Computer Science & Engineering 150A Problem Solving Using Computers. Chapter 3. Existing Information. Notes. Notes. Notes. Lecture 03 - Functions

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

Functions. Systems Programming Concepts

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.

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

INTRODUCTION TO C A PRE-REQUISITE

C Programs: Simple Statements and Expressions

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

ME 172. Lecture 2. Data Types and Modifier 3/7/2011. variables scanf() printf() Basic data types are. Modifiers. char int float double

1.3b Type Conversion

Data Type Fall 2014 Jinkyu Jeong

Beginning C Programming for Engineers

Fundamental of Programming (C)

9 Using Equation Networks

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

Programming for Engineers Iteration

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

Python Lists: Example 1: >>> items=["apple", "orange",100,25.5] >>> items[0] 'apple' >>> 3*items[:2]

Operators in java Operator operands.

Chapter 3. Computer Science & Engineering 155E Computer Science I: Systems Engineering Focus. Existing Information.

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 2. Outline. Simple C++ Programs

Structured programming. Exercises 3

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

C++ Programming Lecture 11 Functions Part I

Computers Programming Course 6. Iulian Năstac

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

UNIT- 3 Introduction to C++

Basics of Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

3. Types of Algorithmic and Program Instructions

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:

Operators Functions Order of Operations Mixed Mode Arithmetic VOID Data. Syntax and type conventions Using the Script window interface

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

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

Introduction to C Language

Reserved Words and Identifiers

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

C Functions. CS 2060 Week 4. Prof. Jonathan Ventura

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Computers in Engineering. Moving From Fortran to C Michael A. Hawker

Structures, Operators

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

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

Java Notes. 10th ICSE. Saravanan Ganesh

C Functions. 5.2 Program Modules in C

Engineering Problem Solving with C++, Etter/Ingber

Features of C. Portable Procedural / Modular Structured Language Statically typed Middle level language

Chapter 3 Structure of a C Program

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

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

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

Operators in C. Staff Incharge: S.Sasirekha

Goals for This Lecture:

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

ENGI Introduction to Computer Programming M A Y 2 8, R E Z A S H A H I D I

Chapter 3: Operators, Expressions and Type Conversion

Course Outline Introduction to C-Programming

UNIT 3 OPERATORS. [Marks- 12]

C Program. Output. Hi everyone. #include <stdio.h> main () { printf ( Hi everyone\n ); }

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

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

Variable and Data Type 2

Programming and Data Structures

Structured Programming. Dr. Mohamed Khedr Lecture 9

Operators And Expressions

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

Declaration and Memory

Lecture 14. Daily Puzzle. Math in C. Rearrange the letters of eleven plus two to make this mathematical statement true. Eleven plus two =?

Types and Expressions. Chapter 3

Numerical Analysis First Term Dr. Selcuk CANKURT

Lecture 3. More About C

Prepared by: Shraddha Modi

Transcription:

Operators and Expression Dr Muhamad Zaini Yunos JKBR, FKMP

Arithmetic operators Unary operators Relational operators Logical operators Assignment operators Conditional operators Comma operators Operators

Expression Arithmetic expression Logical expression Character/string expression

Arithmetic Operators Arithmetic operators C operators Addition + Substraction - Multiplication * Division / Modulo division %

Arithmetic Expression Integer Expression Floating point expression Mixed mode expression

Integer Expression Mathematical expression obtained by combining integer variables and/or integer constant with help of arithmetic operators Rules A signed or unsigned integer variable or an integer constant is an integer expression An integer expression connected by arithmetic operator to an unsigned integer constant is an integer expression An integer expression enclosed in parentheses is an integer expression 2 integer expression connected by an arithmetic operator is an integer expression

Floating point expression Mathematical expression obtained by combining floating point variables and/or floating point constant with help of arithmetic operators Rules- same with integer

Mixed mode expression and implicit conversion Expression formed by integer, floating point or character constant A is integer and B floating point.. A/B?? Before division A must be converted to floating point then A is divided by B. Finally result will be stored as floating point number. During conversion lower type operand are converted to higher type operand and the result is the higher type

Data type Long double Double Float Unsigned long int Long int Unsigned int int Char, short Order Higher Lower

Type casting Format: (data_type) variable name Expression Action A= (int) 8.75 The value of A is finally 8 B= (float)x +y x is converted to float then added to y C=(double)(x+y) x +y converted to double D=(char) 65 65 converted to character (A) ASCII value E= (float)1/5 1 is converted to float 1.0 divided by 5. the value of E becomes 0.2 I=(int) D D converted to integer. The ASCII value of D is stored to I (68)

Hierarchy of arithmetic operators Operator Arrangement/Priority (if appears more than once in a statement) ( ) Left to right ++, -- Right to left *, /, % Left to right +, - Left to right

1. 5 + 2 * 6 4 / 2 5 + 12-4 / 2 5 + 12-2 17-2 15

Library Functions Mathematic function C Equivalent Mode of output Header file Power (a b ) pow (a,b) double math.h Exponential exp(x) double math.h logarithm log(x) double math.h squareroot sqrt(x) double math.h Sin sin( x) double math.h Cos cos(x) double math.h Tangent tan(x) double math.h Absolute x abs(x) int stdlib.h Absolute x fabs(x) double math.h Round up to next integer value floor(x) double math.h

Unary Operators Operator Meaning - negative + positive -- decrement ++ increment

Give the size of operand in bytes Size of operator

Relation Expression Relational expression use Relational operator Combination of more than one statement Can consists of variable vs variable produce variable vs constant constant vs constant 0 (if false) 1(if true) 19

Relation Expression Relational Operator Operator Description = = Equal to > Greater than < Less than >= Greater than or equal <= Less than or equal!= Not equal 20

Relation Expression Example 1: P/s: a, b and c are variables, Replace with the given values int a=6, b =1, c = -2; 1) a+ b == c 2) a!= b 6 + 1== -2 6!= 1 7 == -2 Answer: 0(False) Answer : 1 (True) 22

Relation Expression Example 2 : int a=6, b =1, c = -2; 3) b < a 4) b + c <= a 1 < -2 1 + -2 < 6-1 < 6 Answer: 0 (False) Answer : 1 (True) 23

Relation Expression An example program which uses relational expression #include <stdio.h> void main() { int age; printf( \nplease enter your age >> ); scanf( %d,&age); if (age > 21) Relational expression printf( \nyou are qualified to vote ); } 24

Logical Expression Logical expression use Logical Operator Combination of one or more expressions Can consists of Relational expr. vs logical expr. produces Relational expr. vs variable Relational expr. vs constant 0 (if false) 1(if true) 25

Logical Expression Logical Operator Operator && Description AND OR! NOT Logical operator && dan is used between 2 or more relational expression 26

Logical Expression Logical operator truth table for AND AND (&&) Logical Operator Result False AND False False Value 0 1 False AND True False 0 0 0 True AND False False 1 0 1 True AND True True 27

Logical Expression Logical operator truth table for OR OR ( ) Logical Operator Result False OR False False Value 0 1 False OR True True 0 0 1 True OR False Trrue 1 1 1 True OR True True 28

Logical Expression Logical operator truth table for NOT NOT(!) Value Result!0 1!1 0 Logical Operator Not false Not true Result True False 29

Logical Expression Example 1: Evaluate the following logical expression: a) (2 < 5 ) && ( 5 < 10) b) (7 % 2 < 2) ( 2 * 3 == 6) 1 && 1 (1 < 2) (6 == 6) 1 1 1 1 30

Logical Expression Example 2: Evaluate the following logical expression: Given a = 3, b = 4; c)!((5 * b <= 23 - a )) d)! ((b +3!= 8) &&( 3 * a < 2))!((5 * 4 <= 23 3))!(( 7!= 8 ) && ( 9 < 2 ))!(20 <= 20)! ( 1 && 0 )!(1)! ( 0) 0 1 31

Logical Expression An example program which using Logical Expression: #include <stdio.h> void main() { int mark; printf( \nenter your mark >> ); scanf( %d,&mark); if (mark >= 85 && mark <= 100) printf( \ngred A ); } else if( mark >=80 && mark <=84) printf( \ngred A- ); 32

Assignment Statement Format /sintax : variable = value; variable = constant; or variable = variable; variable = expression; Example : 1.average= ( 6 + 5) * 4; average 44 2.grossSalary = 1500; nettsalary = grosssalary + 200; grosssalary 1500 nettsalary 1700 3.price= 50.00; pay = price; ` price pay 50.00 50.00 33

Compound Assignment Statement Use more than one operator (=) Example : int a = b= c = d = e = 250; b =2, number =0; number = b = 10; int age = workhour = 0; 34

Arithmetic Expression Compound Assignment Statement Function To combine two different operator together. To simplify arithmetic operator Original function of operator does not affected Allowed combination are as follow: operator: +=, -=, *=, /=, %= 35

Compound Assignment Statement Example : Assignment Expression Meaning Operator += total + = 300 total = total+ 300 -= total - = count+ 300 total = total - (count + 300) *= total *=300 total = total * 300 /= total /= count 10 total = total / ( count 10) %= total % = 7 total = total % 7 36

Conditional operator The general synthax is Expression1? Expression2: expression 3; Value of expression1 is evaluate first. If value true, expression2 is performed otherwise expression3 is performed.

Comma operator A set of expression within parentheses and separated by commas can be assigned to variable The expression evaluated left to right and the value of last expression is the final value of variable