4. EXPRESSIONS. Display the value of pi, 3.14159265 to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
JavaScript, Third Edition
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Operators and Expressions
C Programming Lecture 5. Precedence and Associativity of Operators b Rules of associativity and precedence of operators determine precisely how expressions.
Chapter 4: Basic C Operators
Operaciones y Variables
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
2440: 211 Interactive Web Programming Expressions & Operators.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Numeric Types, Expressions, and Output ROBERT REAVES.
CSE1301 Computer Programming Lecture 5: C Primitives 2 Corrections.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
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.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is a legal identifier? what.
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?
CSC Programming I Lecture 5 August 30, 2002.
Chapter 2: Using Data.
C/C++ Operators Binary Operators: Operators Between Two Operands: Operator + MeaningExample Definition. Additionx = 6 + 2;Add the values on either side.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material.
CPS120: Introduction to Computer Science Operations Lecture 9.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
COMP Primitive and Class Types Yi Hong May 14, 2015.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
Principles of Programming Chapter 4: Basic C Operators  In this chapter, you will learn about:  Arithmetic operators  Unary operators  Binary operators.
Variables Symbol representing a place to store information
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Chapter 4: Expressions Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 4 Expressions.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
Copyright © Curt Hill The Assignment Operator and Statement The Most Common Statement you will use.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
CSE 220 – C Programming Expressions.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
Relational Operations
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.
Assignment and Arithmetic expressions
Arithmetic Operator Operation Example + addition x + y
Structure of a C Program
Lecture 3 Expressions Richard Gesick.
Basic Notions Review what is a variable? value? address? memory location? what is an identifier? variable name? keyword? what is legal identifier? what.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
C Operators, Operands, Expressions & Statements
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
Associativity and Prescedence
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 4: Expression and Operator
Assignment Operators Topics Increment and Decrement Operators
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
OPERATORS in C Programming
Data Types and Expressions
OPERATORS in C Programming
Data Types and Expressions
Presentation transcript:

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 a single quotation mark for feet and double quotation mark for inches and prints the person’s height in centimeters to two decimal places

/* Computes a Universal Product Code check digit */ #include int main(void) { 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); 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; }

Arithmetic Operators The arithmetic operators are: + unary plus - unary minus +addition - subtraction * multiplication / division % remainder (modulus)

Arithmetic Operators The value of i % j is the remainder when i is divided by j. For example, the value of 10 % 3 is 1; the value of 12 % 4 is 0. The % operator requires integer operands; all other operators allow either integer or floating-point operands. If both operands are integers, / truncates the result toward 0. There is no exponentiation operator. Must use “pow” function defined in

Operator Precedence When an expression contains more than one operator, the meaning of the expression may not be immediately clear: Does i + j * k mean (i + j) * k or i + (j * k) ? In C, this potential ambiguity is resolved by operator precedence. Precedence of the arithmetic operators: Highest: + - (unary) * / % Lowest: + - (binary)

Operator Precedence Examples : i + j * k means i + (j * k) -i * -j means (-i) * (-j) +i + j / k means (+i) + (j / k)

Associativity Operator precedence rules alone aren’t enough when an expression contains two or more operators at the same level of precedence. The associativity of the operators now comes into play. The binary arithmetic operators are all left associative (they group from left to right); the unary operators are right associative. Examples of associativity: i - j - k means (i - j) - k i * j / k means (i * j) / k i - j * i + k means (i - (j * i)) + k - - i means -(-i)

Simple Assignment One way to change the value of a variable is by assignment. Simple assignment has the form v = e, where v is an lvalue and e is an expression. Examples: i = 5; /* i is now 5 */ j = i; /* j is now 5 */ k = 10 * i + j; /* k is now 55 */ If v and e don’t have the same type, then the value of e is converted to the type of v before assignment: int i; float f; i = 72.99; /* i is now 72 */ f = 136; /* f is now */

Simple Assignment (Continued) Assignment is a true operator, just like + and *. The value of v = e is the value of v after the assignment. Assignments can be chained together: i = j = k = 0; Since the = operator is right associative, this statement is equivalent to i = (j = (k = 0)); The assignment operator may appear in any expression: i = 1; k = 1 + (j = i); printf("%d %d %d\n", i, j, k); /* prints "1 1 2" */ Hiding an assignment inside a larger expression usually isn’t a good idea.

Lvalues The increment and decrement operators require an lvalue (pronounced “Lvalue”) as their operand. An expression has an lvalue if it represents a storage location in computer memory. Variables are lvalues, since a variable is just a name for a storage location. Constants and most expressions containing operators are not lvalues, including the following: 12 i + j -i

Compound Assignment In addition to the simple assignment operator =, C provides ten compound assignment operators, including these five: += -= *= /= %= The expression v += e is equivalent to v = v + e, except that v is evaluated only once in the former expression. The other operators have similar meanings.

Increment and Decrement Operators The ++ and -- operators increment and decrement variables. Both operators have the same precedence as negation. Either operator can be prefix or postfix: ++i i++ --i i-- When used as a prefix operator, ++ increments the variable before its value is fetched: i = 1; printf("i is %d\n", ++i); /* prints "i is 2" */

Increment and Decrement Operators When used as a postfix operator, ++ increments the variable after its value is fetched: i = 1; printf("i is %d\n", i++); /* prints "i is 1" */ printf("i is %d\n", i); /* prints "i is 2" */ The -- operator has similar properties: i = 1; printf("i is %d\n", --i); /* prints "i is 0" */ i = 1; printf("i is %d\n", i--); /* prints "i is 1" */ printf("i is %d\n", i); /* prints "i is 0" */

Compound Assignment Examples: i = 5; j = 2; i += j; /* i is now 7 */ i = 5; j = 2; i -= j; /* i is now 3 */ i = 5; j = 2; i *= j; /* i is now 10 */ i = 5; j = 2; i /= j; /* i is now 2 */ i = 5; j = 2; i %= j; /* i is now 1 */

Compound Assignment The compound assignment operators have the same properties as the = operator; in particular, they’re right associative and can be used in arbitrary expressions.

Expression Statements Any expression can be used as a statement: ++i; The value of the expression is discarded; the statement has no effect unless the expression modifies one of its operands: i = 1;/* stores 1 into i, then evaluates i and discard the result */ i--; /* fetches i, discards the result, then decrements i */ i + j; /* adds i and j, then discards the result */

Expression Statements Expression statements are typically used to perform assignments or to increment or decrement variables. Warning: A minor typing mistake can turn a meaningful statement into a meaningless expression statement. For example, the statement i = j; could easily become i + j;

Partial List of C OPerators PrecedenceOperatorAssociativity 1++ (postfix) -- (postfix) Left 2++ (prefix) -- (prefix) + - Right 3*/%*/% Left = *= /= %= += -+right

Expression Evaluation a = b += c++ - d + --e / -f Order of subexpression evaluation a = 5; c = (b = a + 2) - (a = 1);

#include int main() { printf("01(a). %d\n", 3 -2 / 4); printf("01(b). %f\n", / 4); printf("\n02. %d\n", -27 / / 3); printf("\n03. %d\n", 16 % * 6); printf("\n04(a). %d\n", -12 * 3 % 2 * -23 / * 2); float x = 3; printf("\n05(a). %f\n", x-- * 2 + 5); printf("05(b). %f\n", x); printf("05(c). %f\n", --x * 2 + 5); printf("05(d). %d\n", 3 % 5 / (5 % 3)); return 0; }