Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

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

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

Operators & Expressions

Unit 3. Operators. School of Science and Technology INTRODUCTION

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

Chapter 3 Structure of a C Program

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

Operators and Expressions:

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

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

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

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

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

Lecture 3. More About C

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

Operators in C. Staff Incharge: S.Sasirekha

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

Data Types and Variables in C language

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

Expressions and Casting

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

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

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

Department of Computer Science

Operators. Lecture 3 COP 3014 Spring January 16, 2018

Chapter 3: Operators, Expressions and Type Conversion

Programming in C++ 5. Integral data types

Basic Assignment and Arithmetic Operators

Chapter 4: Basic C Operators

Information Science 1

Arithmetic Expressions in C

Computers Programming Course 6. Iulian Năstac

Operators. Java operators are classified into three categories:

LECTURE 3 C++ Basics Part 2

Basics of Programming

MA 511: Computer Programming Lecture 3: Partha Sarathi Mandal

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

First of all, it is a variable, just like other variables you studied

Operators And Expressions

Introducing C. Origins of C. Chapter 1. Standardization of C. Origins of C

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

JAVA OPERATORS GENERAL

Programming for Engineers Iteration

UNIT 3 OPERATORS. [Marks- 12]

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

Chapter 12 Variables and Operators

Fundamental of Programming (C)

C: How to Program. Week /Mar/05

Unit-2 (Operators) ANAND KR.SRIVASTAVA

Data Types. Data Types. Integer Types. Signed Integers

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

Expressions and Precedence. Last updated 12/10/18

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

C Programming

Structured programming. Exercises 3

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

Programming for Engineers: Operators, Expressions, and Statem

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

Infix to Postfix Conversion

Constants and Variables

Fundamentals of Programming

Add Subtract Multiply Divide

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

1.3b Type Conversion

Information Science 1

Muntaser Abulafi Yacoub Sabatin Omar Qaraeen. C Data Types

Programming and Data Structures

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

C-LANGUAGE CURRICULAM

Slide 1 CS 170 Java Programming 1 Expressions Duration: 00:00:41 Advance mode: Auto

Arithmetic Operators. Portability: Printing Numbers

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

The Arithmetic Operators

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

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

CS102: Variables and Expressions

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

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

A Java program contains at least one class definition.

Declaration and Memory

PART II. Computation

Programming for Engineers Introduction to C

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

Chapter 2: Data and Expressions

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

Prepared by: Shraddha Modi

Full file at C How to Program, 6/e Multiple Choice Test Bank

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

Language Reference Manual simplicity

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

ECE 122 Engineering Problem Solving with Java

Basics of Java Programming

Lecture 4 CSE July 1992

CMSC 104 -Lecture 6 John Y. Park, adapted by C Grasso

More Programming Constructs -- Introduction

Lecture 02 Summary. C/Java Syntax 1/14/2009. Keywords Variable Declarations Data Types Operators Statements. Functions

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

Visual C# Instructor s Manual Table of Contents

CS2351 Data Structures. Lecture 7: A Brief Review of Pointers in C

Transcription:

Introduction to Computer Engineering 0702111 2. Expressions Yacoub Sabatin Muntaser Abulafi Omar Qaraeen 1 Introduction A typical programming scenario: Read input data using scanf function, Perform some calculations involving: Evaluating expressions; Storing results in certain variables by executing assignment statements, and Output result using print function; Read & Output Covered previously; Will focus on the calculation point by combining constants and variables with operators (+,-,/, *, %) to form expressions; 2 1

C language has rich and powerful set of operators: Arithmetic Operators: ( ) Brackets; ++ Increment; -- Decrements; * Multiplication; / Division; + Addition (plus); - Subtraction (minus); % Remainder (Mod); = Assignment. 3 Relational Operators: > Greater than; >= Greater than or equal to; < Less than; <= Less than or equal to; == Equal to;!= Not equal to. Logical/Boolean Operators: && AND; OR;! NOT. 4 2

The operators precedence (priority): 1. Example: a=10.0 + 2.0 * 5.0-6.0 / 2.0; What is the answer?! (Answer=17) Multiplication & Division parts will be evaluated first and then the addition and subtraction parts. Note: To avoid confusion use brackets. The following are two different calculations: a=10.0 + (2.0 * 5.0) - (6.0 / 2.0) a=(10.0 + 2.0) * (5.0-6.0) / 2.0 Level 1: (); Level 2: *, /, %; Level 3: +, -. 5 Evaluate the expression: 5 + 3 * 2 + 12 / 6? 5 + 6 + 2 13; Evaluate: 7 / 3 = 2 7 % 3 = 1 20.0 / 3 * 20 / 3 = 44.44443 20 / 3 * 20.0 / 3 = 40.0 20.0 / 3 + 20 / 3 = 12.666667 Evaluate: - 4 + 23 * ( 2 / 3) 3 * 17 189.0 / 18.0 6 3

Example: Universal Product Code (UPC) is a bar code for the product, to be able to identify it and know its price. It consists of 12 digits (1, 5, 5, and 1) the 1 st (d) identifies the type of the item, the next 5 identify the manufacturer, the next 5 identify the product, then the final digit is check digit which can tell if the previous 11 digits were read correctly or not, so to re-read them by the scanner once again if there is something wrong. The formula to calculate this digit: first_sum= d + i2 + i4 + j1 + j3 + j5 second_sum= i1 + i3 + i5 + j2 + j4 total = 3 * first_sum+ second_sum check digit=9 -( (total-1)% 10 ) The following example implements this algorithm: 7 /* Computes a Universal Product Bar Code check digit */ #include <stdio.h> main() { int d, i1, i2, i3, i4, i5, j1, j2, j3, j4, j5, first_sum, second_sum, total; printf("enter the first (single) digit: "); scanf("%1d", &d); printf("enter first group of five digits: "); scanf("%1d%1d%1d%1d%1d", &i1, &i2, &i3, &i4, &i5); 8 >> 4

printf("enter second group of five digits: "); scanf("%1d%1d%1d%1d%1d", &j1, &j2, &j3, &j4, &j5); first_sum = d + i2 + i4 + j1 + j3 + j5; second_sum = i1 + i3 + i5 + j2 + j4; total = 3 * first_sum + second_sum; } printf("check digit: %d\n", 9-( (total - 1) % 10 )); return 0; 9 Try: 3, 1, 2, 3, 4, 5, 0, 6, 7, 8, 9: Check Digit =?? Assignment Statements Once you've declared a variable you can use it, but not until it has been declared - attempts to use a variable that has not been defined will cause a compiler error! Using a variable means storing something in it You can store a value in a variable by using assignment statements of the form: Variable name (v) = value or an expression(e); Example: a=10; /*stores the value 10 in the int variable a */ 10 5

Expression, e, is evaluated and its value is assigned to v, (overwriting the previously stored value; if any) The assignment produces a result which is the value of v after assignment e can be: Constant i = 5; /* i is 5 */ variable j = i; /* j is 5 */ expression k = 10 * i + j; /* k is 55 */ Generally, v is an expression that have an lvalue (a location in the machine s memory). So an assignment i + j = 0; is illegal, because i + j has no lvalue or no place to store the Zero! 11 BE CAREFUL WITH ARITHMATIC!!! What is the answer to this simple calculation? a=10/3 The answer depends upon the data type of the division sides (see the next example). If a declared as type in the answer will be 3; If a is of type floa then the answer will be 3.000000 Two points to note from the above calculation: 1. C ignores fractions when doing integer division! 2. When doing float calculations integers will be converted into float. We will see later how C handles type conversions. 12 6

float a=10/3; 2 examples: printf("the answer is: %f",a); Would print: The answer is: 3.000000 float a=10/3.0; printf("the answer is: %f",a); Would print: The answer is: 3.333333 13 Compound Assignments i = i + 3; An assignment statement that adds 3 to the previous value stored in i Compound assignment operators: (+=, -=, *=, /=, %=) Allows to shorten statements: (i = i + 3;) to (i += 3;) += adds value of right operand to variable to left Other compound assignments: v += e Adds e to v, storing result in v; v -= e Subtracts e from v, storing result in v v *= e Multiplies v by e, storing result in v v /= e Divides v by e, storing result in v 14 v %= e Computes the remainder when v is divided by e, storing result in v; 7

Example int a, b, c, d, e; a = 14; b = a + 1; //Now, b=15 b *= b; //b=225 c = b / 10-2; //c=20 d = a + 15 - c; //Now, d=9 c += d; //c=29 e = c + d; //e=38 e /= 2; planets. 15 //e=19 printf( %d%d%d%d%1.0f DNA is a Main-belt Asteroid discovered by C. Juels & P. Holvorcem at Fountain Hills.\n,a-9,b/45,c-24,d-4,e/4.0); Asteroid: The belt is the region of the Solar System located roughly between the orbits of the planets Mars and Jupiter. It is occupied by numerous irregularly shaped bodies called asteroids or minor What s the output?? What are the final values of X, Y, & Z: X = Y = Z = 10;/* The = operator is right associative, so this assignment is equivalent to: X=(Y=(Z=10)); */ X += 15; Y -= 15; Z %= 3; X =?, Y =?, Z =? Watch out an unexpected results in chained assignments and type conversion: int i; float f; f=i=33.3; i is assigned the value 33 (because: int), then f is assigned 33.0 (not 33.3, as you might think)! 16 8

17 Increment & Decrement Another way to increment variables is using: ++ (increment) (add 1 to operand) and -- (decrement) (subtract 1 from operand) ++ and -- can be used as: prefix operators (++i and --I) Variable is changed before the expression is evaluated. postfix operators (i++ and i--) Variable is not changed until after it is used in the expression. Simple Assignment Compound Increment i = i + 1 i += 1 ++i j = j - 1 j - = 1 --j That is: A prefix ++i, the variable is incremented (or decremented) and then used: int i = 10; i is incremented to 11, & int j = ++i; then j is set to 11 (the value of i) A postfix i++, the variable is used and then incremented. (increments i, but returns the prior, non-incremented value) int i = 10; j is set to 10 (the value of i) and int j = i++; then i is incremented to 11. 18 9

The expressions: k = 2 * ++i Add one to i, Store the result back in i, Multiply i by 2, and Store that result in k. k = 2 * i++ Take i's old value and multiply it by 2, Increment i, and Store the result of the multiplication in k. The -- operator works in the same way. 19 Example 1: i = 1; printf ( i is %d \n, ++i); /* prints i is 2 */ printf ( i is %d \n, i); /* prints i is 2 */ Example 2: i = 1; printf ( i is %d \n, i++); /* prints i is 1 */ printf ( i is %d \n, i); /* prints i is 2 */ Example 3: a= 3; printf("a=%d, a+1=%d \n", a, ++a); /* incremented before evaluation, then passed */ /* will print out a=4, a+1=4 */ 20 10

21 If the increment operator comes after the named variable, then the value of the statement is calculated after the increment occurs: So for the statement: a= 3; printf("a=%d, a+1=%d \n", a, a++);»would display a=3 a+1=3 Now, a will be set to 4, Try printf("a=%d", a);»the result will be: a=4 22 11

If Expression contains more than One operator and NO parentheses are used to clarify expression Then can USE operator Precedence & Associatively to clarify any ambiguity! Example: Operator Precedence & Associativity Does x+y*k equals x+(y*k) or (x+y)*k? 23 Recalling Precedence: Highest - + (unary) * / % Lowest + - (binary) Unary operation: An operation with only one operand: Operand One of the inputs (such as 3, 6) of an operator (such as +); Ex.: 3 + 6 = 9 i.e. an operation with a single input. 24 12

In C language, the following operators are unary: Increment / Decrement : ++x, x++, --x, x-- (Doesn t work on expressions; needs l-value) Address: &x Indirection: *x Positive / Negative: +x, -x One's complement: ~x Logical negation:!x (ex. printf( %d,!!3); would print 1) Sizeof operator: sizeof x, sizeof(type-name) Cast: (type-name) cast-expression 25 Ex. printf( %f,(float)(10/3)); 3.000000 Ex. printf( %f,(float)10/3); 3.333333 If expression contains operators of the same precedence, then we have to use Associativity Left Associativity: Operator groups from L to R Ex: 26 Associativity Binary Arithmetic Operators (+ - * / %): i j + k (i j) + k I * j / k (I * j) / k Right Associativity: Operator groups from R to L Ex: Unary Arithmatic Operators (- +) -+i -(+i) 13

27 Notes In compound assignment operators: i+=j; equivalent to i=i+j; i=+j; compiles BUT equivalent to i=(+j) which merely copies the value of j into i! Compound assignment operators have the same properties as the = operator (associativity) i+=j+=k; means i+=(j+=k); Equality & Assignment: x = 5; /* sets value of x to 5 */ if (x == 5) /* returns true/false */ x += 3; /* x is now is 8 */ if (x!= 5) { Printf ( x is not 5 \n ); } x *= 2; /* x is now 16 */ 28 14

1. What is the value of x in each of the following: 1.x = 3 + 4 % 6 / 2 + 5? 2.x = y = 4; x *=3 + y; 2. What is the output in each case: 1.printf( %d, 3 * 17 189/18); 2.printf( %f,193/19/pow(3.0, 2.0)); 3.printf( %d, -4 + 23 *( 2 / 3)); 4.printf( %f, 3 * 17 189.0 /18.0); 5.printf( %f,193/19.0/pow(3.0, 2.0)); 29 Examples 6.printf( %d, 5 % 2 + 14 / 3-8); 15