Chapter 4 Expression & Operators

Similar documents
Operators and Expression. Dr Muhamad Zaini Yunos JKBR, FKMP

Operators & Expressions

Chapter 4: Basic C Operators

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

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

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

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

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.

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Unit 3. Operators. School of Science and Technology INTRODUCTION

Constants and Variables

Operators in java Operator operands.

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

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:

Chapter 3 Structure of a C Program

Operators And Expressions

JAVA OPERATORS GENERAL

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

Basics of Programming

Programming in C++ 5. Integral data types

Fundamentals of Programming CS-110. Lecture 3

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

Operators in C. Staff Incharge: S.Sasirekha

ANSI C Programming Simple Programs

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

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

C Program Structures

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

Programming for Engineers Iteration

Expression and Operator

Beginning C Programming for Engineers

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

Department of Computer Science

Fundamentals of Programming

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

SECTION II: LANGUAGE BASICS

QUIZ: What value is stored in a after this

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

Lecture 3. More About C

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

The Arithmetic Operators

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

C Programming

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

Structured Programming. Dr. Mohamed Khedr Lecture 4

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

WARM UP LESSONS BARE BASICS

Prof. Navrati Saxena TA: Rochak Sachan

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

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

Computer System and programming in C

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

Computer Science & Engineering 150A Problem Solving Using Computers

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

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

Data Types and Variables in C language

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

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

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

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

Variables and Operators 2/20/01 Lecture #

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

Week 2 Introduction to Computer and Algorithm (Part 2)

UNIT 3 OPERATORS. [Marks- 12]

INTRODUCTION TO C A PRE-REQUISITE

Chapter 3: Operators, Expressions and Type Conversion

Expressions. Eric McCreath

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

Lecture 02 C FUNDAMENTALS

Fundamental of Programming (C)

Prepared by: Shraddha Modi

Computers Programming Course 6. Iulian Năstac

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

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

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

C Programs: Simple Statements and Expressions

Operators. Java operators are classified into three categories:

Engineering 12 - Spring, 1999

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

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

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Course Outline Introduction to C-Programming

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

1.3b Type Conversion

UNIT- 3 Introduction to C++

Engineering Computing I

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

Introduction to C. Systems Programming Concepts

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

Arithmetic Operators. Portability: Printing Numbers

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

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

Operations. Making Things Happen

Programming for Engineers: Operators, Expressions, and Statem

Introduction to Programming

2/5/2018. Expressions are Used to Perform Calculations. ECE 220: Computer Systems & Programming. Our Class Focuses on Four Types of Operator in C

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

Transcription:

Chapter 4 Expression & Operators 1

Aim To give understanding on: Expression and operator concept math.h and stdlib.h built-in function Objective Students should be able to: understand concepts and fundamentals in expression/ operator. write expression in C programming language 2

Introduction to Expression Given the following statement : 2x + 3 z = y expression 3

Introduction to Expression Process involves in money withdrawal scenario balance withdrawed money = current balance Expression in C : bakiterkini =wangkeluar bakisemasa; 4

What is Expression? Expression Introduction to Expression Combination or more than one variable or Constant (operand) which separated by operator example x + 3 - z Operator Operand Consists of arithmetic relational logical 5

Arithmetic Expression Arithmetic Expression Known as Mathematic Expression using Arithmetic Operator Represents by Unary operator Binary operator Represents by Operator Meaning - negative + positive -- decrement ++ increment Operator Meaning * multiply / divide + add - subtract % modulus 6

Unary Operator Arithmetic Expression Unary Operator example Operates for one operand Computer memory cell a = -20; a -20 b = +15; b +15 Increment Decrement prefix ++c; --c; postfix c++; c--; 7

Unary Operator Example 1: int A = 5; ++A; printf( %d, A); output? A--; printf( %d,a); output? A++; Arithmetic Expression printf( %d,a); output? 8

Unary Operator Example 2: Arithmetic Expression int A ; A=6; printf( %d,3 + --A); output? printf( %d, A); output? A=6; printf( %d, 3 + A--); output? printf( %d, A); output? 9

Unary Operator Example 3: Arithmetic Expression Given the value of num1 = 8.Determine the value of num2 after the execution for each of the following statements: num2 = num1++ - 2; num2 = num1; num2 = ++num1 3; num2 = num1-- +1; 10

11

Binary Operator Arithmetic Expression Binary Operator Located between constants or variables or both combination example A + z operator operand 12

Binary Operator Arithmetic Expression Multiplication Use symbol * example A * z Mathematic 2x + y operator operand Arithmetic Expression 2 * x + y 13

Binary Operator Arithmetic Expression Divide Use symbol / example A / z operator operand Mathematic 2 : y Arithmetic Expression 2 / y 14

Binary Operator Arithmetic Expression Modulus Use symbol % example A % z operator operand Return a balance when 2 numbers is divided Can only be used with an integer variable 15

Binary Operator Example: int A, B; float C; A= 2; B= 5; Arithmetic Expression C= 2.4; B% A; C % A; Valid! Answer is 1 Invalid! C is float 16

Arithmetic Expression Assignment Statement Used to store value/result of process to a variable Use operator symbol = Assignment statement Double assignment statement Compound assignment statement 17

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

Arithmetic Expression Compound Assignment Statement Use more than one operator (=) Example : int a = b= c = d = e = 250; int b =2, number =0, total = 0,average =3; number = b++ = 10; int age = workhour = 0; 19

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

Compound Assignment Statement Example : Arithmetic Expression 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 21

Arithmetic Expression Arithmetic Operator Precedence Rules Compiler will follows the following precedence to execute the arithmetic expression based on priority. Operator Arrangement/Priority (if appears more than once in a statement) ( ) Left to right ++, -- Right to left *, /, % Left to right +, - Left to right 22

Arithmetic Operator Arithmetic Operator Precedence Rules Example: 1. 5 + 2 * 6 4 / 2 2. 3 * 4 / 2 + ( 3 1) 5 + 12-4 / 2 3 * 4 / 2 + 2 5 + 12-2 12 / 2 + 2 17-2 6 + 2 15 8 23

Arithmetic Operator Precedence Rules Example: Arithmetic Expression 3. Prefix unary arithmetic expression int kira = 5, nilai pertama = 10; nilai_kedua = 5 * --kira + nilai_pertama; printf( %d %d, kira, nilai_kedua); Output: 4 30 24

Arithmetic Operator Precedence Rules Example: Arithmetic Expression 3. Prefix unary arithmetic expression int kira = 5, nilai pertama = 10; nilai_kedua = 5 * kira-- + nilai_pertama; printf( %d %d, kira, nilai_kedua); Output: 4 35 25

Arithmetic Expression Mathematic Library Function Diwakili oleh perpustakaan piawai matematk iaitu math.h Dipanggil bersama #include Antara fungsi perpustakaan matematik yang penting: Fungsi Tujuan sqrt(x) Kembalikan nilai puncagandadua x di mana x >= 0 pow(x) Kembalikan x ganda y cos(x) Kembalikan cos x di mana x di dalam radian sin(x) Kembalikan sin x di mana x di dalam radian tan(x) Kembalikan tan x di mana x di dalam radian 26

Example: Arithmetic Expression Mathematic Library Function #include<stdio.h> #include <math.h> void main() { int x = 16, y ; y = sqrt(x); printf( %d,y); Output : 4 } 27

Exercise: Arithmetic Expression 1. Convert the following mathematic expression to a valid arithmetic expression : a) b = 3 + b b) x = (a b)(a c 2 ) a + 4 c) d = (3e d) - ( 4 3c 3 ) d) r = 2s + 3(s 9) x 9 4y s 2. Given a= 3, b = 5, c=1. What is the output of the following expression? a. ( 6 * c 6 / a) - b b. (5 * c) +( a* b / b) c. ++a d. c + a * c / (3 * c) 28

Exercise: Arithmetic Expression Assume i,j and k are integer variables with i = 5 and j=3. Determine what is the value for each of the following statement: a) k = j++; d) k = ++j; b) k = i * j--; e) k = i * --j; c) k = j + i * j++; f) k = 27 / j++ - 16 % i; 29

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) 30

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

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) 32

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) 33

Relation Expression Example 3: int a=10, b = 3, c = 7; P/s: Relational operator has less priority than other operators. Start evaluating from left to right. (a+b >= 3*c)==( a!= 2*c+b) (10+3 >= 3*7)==(a!= 2*c+b) (13 >= 21)==(10!= 14+3) (13 >= 21)==(10!= 17) 0 == 1 0 (false) 34

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 ); } 35

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) 36

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

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 38

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 39

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

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 41

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 42

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- ); 43

Logical Expression Exercise: 1. Given with i=2, j = 5 and k = 15. Evaluate each of the following expression: a) i > j k g) k == j + I * j b) i!= k h) k <=k /j c) (i >= 1) && (j == 5) i) (j < i) (k > j) d)!( i > j) j) (i >0) && (j <k) (k <i) e) i < j < k k) i * k < k / j f) (i < j) && (j < k) i) i j > k 2. Complete the following statements with suitable logical expression. int angka1,angka2; if (angka1 is less than or equal to angka2) printf( %d is less than or equal to %d,angka1,angka2) 44