1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material.

Slides:



Advertisements
Similar presentations
Operators and Arithmetic Operations. Operators An operator is a symbol that instructs the code to perform some operations or actions on one or more operands.
Advertisements

Computer Programming w/ Eng. Applications
CMSC 104, Version 9/011 Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class Project Incremental.
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
Types and Arithmetic Operators
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Operators and Expressions
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
Intro to Programming Problem 1: Computers have to be told exactly what do to. Problem 2: Computers only understand “Machine language”. Problem 3: Machine.
 Input and Output Functions Input and Output Functions  OperatorsOperators Arithmetic Operators Assignment Operators Relational Operators Logical Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
LESSON 6 – Arithmetic Operators
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CPS120: Introduction to Computer Science Operations Lecture 9.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
CMSC 104, Version 8/061L10ArithmeticOps.ppt Arithmetic Operators Topics Arithmetic Operators Operator Precedence Evaluating Arithmetic Expressions In-class.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Expressions and Assignment Statements
CSE 220 – C Programming Expressions.
Chapter 7: Expressions and Assignment Statements
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
INSPIRING CREATIVE AND INNOVATIVE MINDS
BIL 104E Introduction to Scientific and Engineering Computing
Computing Fundamentals
Relational Operations
Chapter 7: Expressions and Assignment Statements
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Computer Programming: C language
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Arithmetic Operators in C
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
A First Book of ANSI C Fourth Edition
Arithmetic Operators in C
C Operators, Operands, Expressions & Statements
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Associativity and Prescedence
Lecture3.
Doing Arithmetic Today’s lecture is in chapter 2 Assignment statement:
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 3 Operators and Expressions
Data Types and Expressions
Chapter 4: Expression and Operator
Operator King Saud University
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material

2 What’s an Expression? A mathematical formula that computes a value –formed by operators (+, -, *, /, %, …) celsius = (fahrenheit-32)/1.8; area = 3.14*radius*radius; circumference = 2*3.14*radius; volume = length*width*height; sum = sum + 5; …

3 Arithmetic Operators Basic operators for performing arithmetic are the same in many programming languages: Modulus (remainder)% Division/ Multiplication* Subtraction- Addition+ Binary operators Decrement-- Increment++ Unary operators

4 Arithmetic Operators: Example 14sum = sum % 3%Modulus 24sum = sum / 2/Divide 84sum = sum * 2*Multiply 24sum = sum – 2-Subtraction 64sum = sum + 2+Addition Value of sum after Value of Sum before EquationOperatorOperation 34--sum--Decrement 54++sum++Increment Value of sum after Value of sum before EquationOperatorOperation

5 Notes on / Operator If both operands are integers, / performs integer division int a = 5; int b = 4; int result = a/b; /* > 1 */ printf(“result: %d\n”, result); /* will print 1 */

6 Mixed Operands If one of the operands is a float/double, the other operand is implicitly converted to float/double before the expression is evaluated –If the l-value is an integer, the result is typecast to int before assignment –If the l-value is a float/double, the result is typecast to float/double before assignment int result; float fresult; result = 5/4.0; /* > 1 */ fresult = 5/4.0; /* > 1.25 */ printf(“result: %d, fresult: %.2f\n”, result, fresult);

7 More on Unary Operators result = ++a; /* increment first, then assign */ Equivalent to: a = a+1; result = a; result = --a; /* decrement first, then assign */ Equivalent to: a = a-1; result = a;

8 More on Unary Operators result = a++; /* assign, then increment */ Equivalent to: result = a; a = a+1; result = a--; /* assign, then decrement */ Equivalent to: result = a; a = a-1; Unary operators can be used with both int & float/double operands. But typically applied to ints

9 Compound Assignment Operators Compound assignment operators: += -= *= /= %= … For example: sum += data; /* sum = sum + data */ a -= 6; /* a = a – 6; */ product *= data; /* product = product*data */ a %= 2; /* a = a%2 */ a /= b+c; /* a = a/(b+c) */ …

10 Associativity & Precedence When an expression contains more than one operator of the same type, associativity rules apply result = a+b+c;  result = (a+b)+c; /* left associativity */ a = b = c;  a = (b=c); /* right associativity */ When an expression contains different operators together, precedence rules apply result = a+b*c;  result = a+(b*c); /* * has higher precedence than + */

11 Associativity & Precedence Rules right=, +=, -=,.. 4 left+, -3 left*, /, %2 right-, ++, --1 AssociativityOperatorPrecedence It is difficult to remember all associativity and precedence rules So it is best to parenthesize the expression yourself to avoid ambiguity

12 Expression Examples(1) int a = 10, b = 22; int result = a+b*3; printf("a: %d, b: %d, : %d\n", a, b, result); int a = 10, b = 22; int result = (a+b)*3; printf("a: %d, b: %d, : %d\n", a, b, result); a: 10, b: 22, : 76 a: 10, b: 22, : 96

13 Expression Examples(2) int a = 7, b = 9, c = 4; int result = (a+10)%b/c; printf("a: %d, b: %d, c: %d, : %d\n", a, b, c, result); int a = 7, b = 9, c = 4; int result = (a+10)%(b/c); printf("a: %d, b: %d, c: %d, : %d\n", a, b, c, result); a: 7, b: 9, c: 4, : 2 a: 7, b: 9, c: 4, : 1

14 Expression Examples(3) int a = 7, b = 8; printf("a(before): %d, ", a); a*=b+1; printf("a(after): %d, b: %d\n", a, b); a = b = c = 1; printf("a(before): %d, b(before): %d, ", a, b); a+=b+=c; printf("a(after): %d, b(after): %d, c: %d\n", a, b, c); a(before): 7, a(after): 63, b: 8 a(before): 1, b(before): 1, a(after): 3, b(after): 2, c: 1

15 Expression Examples(4) a = 10; b = 5; printf("a(before): %d, b(before): %d, ", a, b); result = (a b); printf("a(after): %d, b(after): %d, result: %d\n", a, b,result); a = 10; b = 5; printf("a(before): %d, b(before): %d, ", a, b); result = (++a + ++b); printf("a(after): %d, b(after): %d, result: %d\n", a, b,result); a(before): 10, b(before): 5, a(after): 11, b(after): 6, result: 16 a(before): 10, b(before): 5, a(after): 11, b(after): 6, result: 17

16 Example C Program (1) #include /* Convert fahrenheit to celsius */ main() { float fahrenheit, celsius; printf(“Enter a temp in fahrenheit: “); scanf(“%f”, &fahrenheit); celsius = (fahrenheit-32)/1.8; printf(“%f degrees fahrenheit equals %f degrees celsius\n”, fahrenheit, celsius); } Prompt the user and get the fahrenheit temperature to convert celsius = (fahrenheit-32)/1.8 Print the fahrenheit and celsius degrees Start End

17 Example C Program(2) #include /* Compute sum, product, avg of 2 ints */ main() { int number1, number2; int sum, product, average; printf(“Enter 2 integers: “); scanf(“%d%d”, &number1, &number2); sum = number1+number2; product = number1*number2; average = sum/2; printf(“sum: %d, product: %d, avg:%d\n”, sum, product, average); } /* end-main */ Prompt the user and get number1 and number2 sum = number1 + number2 Print sum, product and average product = number1 * number2 average =sum/2 Start End

18 Example C Program(3) #include /* Computes the circumference and area of a circle */ main() { float radius, circumference, area; printf(“Enter the radius: “); scanf(“%f”, &radius); circumference = 2* *radius; area = *radius*radius; printf(“Circumference: %f, area: %f\n”, circumference, area); } /* end-main */ Prompt the user to enter the radius circumference = 2* *radius Print circumference and area of the circle area = *radius*radius; Start End Get radius of the circle

19 The Need for Symbolic Constants In the previous C program, we twice used the number PI = inside the program Using such magic numbers inside of C programs usually leads to bugs –We may type one of the PI numbers wrong! –If we want to add more precision to PI, we need to change all occurrences of the PI numbers, which may also lead to bugs Question: Is there a solution? –Yes: Symbolic constants

20 Defining Symbolic Constants Defined using the preprocessor directive #define Example: #define TRUE 1 #define FALSE 0 #define PI Using symbolic constants: –circumference = 2*PI*radius; –area = PI*radius*radius;

21 Example C Program(3) #include #define PI /* Computes the circumference and area of a circle */ main() { float radius, circumference, area; printf(“Enter the radius: “); scanf(“%f”, &radius); circumference = 2*PI*radius; area = PI*radius*radius; printf(“Circumference: %f, area: %f\n”, circumference, area); } /* end-main */

22 Resolving Symbolic Constants Symbolic constants are resolved by the preprocessor before compilation begins –Preprocessors works like find/replace –Replaces every occurrence of PI with its definition –Preprocessor also removes comments from the code so that the compiler does not have to deal with them prog.c COMPILER ASSEMBLER LINKER LOADER + OS prog.s prog.o printf.o scanf.o prog.exe program running PREPROCESSOR prog.i

23 A Common #define Bug Find the error here: #define SIZE 5; int main() { printf(“Size is %d\n”, SIZE); } int main() { printf(“Size is %d\n”, 5;); } After find/replace by preprocessor: