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.

Slides:



Advertisements
Similar presentations
Types and Arithmetic Operators
Advertisements

Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Expressions ► An expression can be a single variable, or can include a series of variables. If an expression includes multiple variables they are combined.
Review Question What kind error is it when I try to multiply a number in a program by 1000 and store in a variable, but the variable is too small for the.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Chapter 2 part #4 Operator
2440: 211 Interactive Web Programming Expressions & Operators.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Data Types Declarations Expressions Data storage C++ Basics.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
 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.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
CSE 220 – C Programming Expressions.
Topics Designing a Program Input, Processing, and Output
Chapter 7: Expressions and Assignment Statements
ARITHMETIC IN C Operators.
Operators and Expressions
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
BASIC ELEMENTS OF A COMPUTER PROGRAM
BIL 104E Introduction to Scientific and Engineering Computing
Computing Fundamentals
ITEC113 Algorithms and Programming Techniques
Relational Operations
Data Types, Identifiers, and Expressions
Chapter 7: Expressions and Assignment Statements
Test Review Computer Science History
Arithmetic operations & assignment statement
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Type Conversion, Constants, and the String Object
Arithmetic Operator Operation Example + addition x + y
OPERATORS (1) CSC 111.
Lecture 3 Expressions Richard Gesick.
Arithmetic Expressions in C
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
3-3 Side Effects A side effect is an action that results from the evaluation of an expression. For example, in an assignment, C first evaluates the expression.
Winter 2018 CISC101 11/22/2018 CISC101 Reminders
Arithmetic Expressions & Data Conversions
C Operators, Operands, Expressions & Statements
CS150 Introduction to Computer Science 1
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.
CS150 Introduction to Computer Science 1
Expressions.
Chapter 2: Java Fundamentals
Data Types and Expressions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Chapter 2: Overview of C++
Operator King Saud University
is multiplied by a variable of type to yield a result of type
Data Types and Expressions
Arithmetic Expressions & Data Conversions
Data Types and Expressions
Introduction to Python
Presentation transcript:

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 be performed to produce a result of a specified type Arithmetic expression Performs a numeric computation producing a numerical result Logical expression Produces a Boolean result evaluating to true or false String expression Produces a result of type string Expressions may contain other expressions which are evaluated in order producing an overall result Ex: (2+5) * (7+3)

Arithmetic Expressions Basic arithmetic operations + - * / int data type includes % (modulus) Integer division - result is integer 15 / 3 = 5 15 / 2 = 7 0 / 15 = 0 15 / 0 undefined Float results includes decimal portion 15.0 / 2.0 = 7.5

Modulus for Integers Used only with integers Get a compiler error if used with a float Yields remainder - the result is integer Examples: 7 % 2 = 1 299 % 100 = 99 49 % 5 = 4 15 % 0 undefined 15 % -7 system dependent

Mixed-type Expressions Example: 4.6 / 2 evaluates to 2.3 Rule: when an integer and a floating point operand are combined by an operator, the integer gets converted to the floating point type Caveat: this rule is dependent on operator precedence rules

Mixed-type Assignments If the variable on left side of assignment is of different type than the type of the evaluated expression on the right side of =, the result of the expression must be converted to the appropriate type Conversion from a float to an int involves dropping (truncating, not rounding!) the decimal part Example: int n; float x=2.7; n= x; // n will be assigned the value 2 (trick: to round, add 0.5 first!)

Mixed-type Assignment Examples float a, b, x; int m, n; a=10; // result is 10.0 stored in a b = 3.5; m=5; n = 10; x = m / n; // result is 0 assigned to x m = b * 3; // result is 10 assigned to m

Order of Operator Precedence Highest ( ) nested expressions evaluated inside out unary +, - *, /, % binary +, - Associativity Lowest Warning: watch out for the types of operands and the type of the result from evaluating each operand!

Step-by-Step Expression Evaluation

Figure 2.10 Evaluation for z - (a +b / 2) + w * -y;

Figure 2.11 Evaluation tree for m = x + k / 2:

Mathematical Formulas in C++ a = bc not valid C++ syntax Must use * operator a = b * c; m= y − b x − a Must use ( ) and / m = (y - b) / (x - a);