CPS120: Introduction to Computer Science Operations Lecture 9.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
4 Operations On Data Foundations of Computer Science ã Cengage Learning.
What is a variable?  A variable holds data in memory so the program may use that data, or store results.  Variables have a data type. int, boolean, char,
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
Expressions, Data Conversion, and Input
CPS120: Introduction to Computer Science Lecture 8.
1 Lab Session-III CSIT-120 Fall 2000 Revising Previous session Data input and output While loop Exercise Limits and Bounds Session III-B (starts on slide.
Computer Arithmetic Nizamettin AYDIN
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
Chapter 2 part #4 Operator
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Object-Oriented Programming Using C++ Third Edition Chapter 2 Evaluating C++ Expressions.
CPS120: Introduction to Computer Science Decision Making in Programs.
CHAPTER 2 PART #4 OPERATOR 2 nd semester King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited.
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.
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
C++ Programming: Basic Elements of C++.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.
CSC 107 – Programming For Science. The Week’s Goal.
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.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
PHY-102 SAPVariables and OperatorsSlide 1 Variables and Operators In this section we will learn how about variables in Java and basic operations one can.
Mathematical Calculations in Java Mrs. C. Furman.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Recap……Last Time [Variables, Data Types and Constants]
CPS120: Introduction to Computer Science Decision Making in Programs.
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.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Operators.
1 CS161 Introduction to Computer Science Topic #6.
CPS120: Introduction to Computer Science Decision Making in Programs.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
CSC Programming for Science Lecture 5: Actual Programming.
Chapter 3 Math Operations. Objectives Use the assignment and arithmetic operators. Use operators in output statements. Explain the problem with division.
CompSci 230 S Programming Techniques
CPS120 Introduction to Computer Science
Expressions.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Expressions.
INSPIRING CREATIVE AND INNOVATIVE MINDS
BIL 104E Introduction to Scientific and Engineering Computing
Chapter 7: Expressions and Assignment Statements
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Operators and Expressions
OPERATORS (2) CSC 111.
Lecture 3 Expressions Richard Gesick.
Introduction to C++ Programming
Algorithms computer as the tool process – algorithm
Chapter 3 Operators and Expressions
Data Types and Expressions
Operator King Saud University
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

CPS120: Introduction to Computer Science Operations Lecture 9

The Assignment Operator The assignment operator is the equal symbol (=) The assignment operator changes the value of the variable to its left after evaluating the expression on its right For example: sum = ;  The variable sum ends up with the value 1003 salary = 40000; poundsPressure = ; sum = original + 300; salary = salary + raise;

Multiple Assignments i = j = k = 10; // initializes all three variables to the value, 10 This statement is equivalent to: i = 10; j = 10; k = 10;

Common Arithmetic Operators + for addition - for subtraction * for multiplication / for division % for modulus (like finding the remainder of a division problem)

Compound Operators Example 1: j += 1; // is the same as j = j + 1 Example 2: total /= 2; // is the same as total = total / 2;

Increments and Decrement The incrementing (++) and decrementing (--) operators are useful at times if used carefully counter++; is equivalent to counter = counter + 1; and counter += 1; counter--; is equivalent to counter = counter - 1; and counter -= 1; Use the incrementing and decrementing operators with variables in statements such as these, that is with no other operators or code except the variable name that is being incremented or decremented.

Order of Operations Obey the order of operations Perform the following mathematical operations in this order: Parentheses, Exponentiation, Multiplication, Division, Addition and Subtraction Multiplication and division are of equal precedence, so you must work left to right within an algebraic expression The modulus operator (%) has the same precedence as * and / but is higher in precedence than + and -. Addition and subtraction work left to right within an algebraic expression as well Use parentheses if necessary to override the order of operations in order to produce the desired result

Promotion Promotion is supported in C++ to allow your expressions or statements to temporarily convert "smaller" data types to "larger" ones in order to provide an answer to a calculation Remember, promotion of a variable can occur when the expression is calculated, you are unable to store a floating point number in a space reserved for an integer, so the floating point number is truncated The wrong result can result

Typecasting Typecasting is the automatic conversion from one data type to another data type To typecast a variable, supply the name of the data type you want to use to interpret the variable, followed by the variable placed in parenthesis circumference = PI * (long double) (diameter);

Try to avoid overflow, which is a condition when an integer becomes too large for its data type You may not be able to tell from the output (without checking it carefully, at least) that this run-time error is occurring int total; total = ; total++; cout << total;  would display because is the maximum value that can be stored in a variable of the data type int The C++ constant INT_MAX can be used in a C++ program anywhere you wish the maximum possible value of an int to be used Overflow

Overflow Details Think of the legal range of the integer data type as being a number line that goes from to When a value exceeds the right (positive) end of that number line, it "overflows" and wraps around and maps to a position on the left (negative) end of the number line It is possible to overflow the double and char data types as well.

Underflow a condition when a floating-point number becomes too small for its data type such as when a large negative exponent is used. double a = 1e-323; cout << a << endl; cout << a / 10 << endl; causes 0 to display for the second value due to underflow of the double data type.

Decision Making In Computers A circuit quite simply allows one out of two choices to be made depending on its inputs When decisions are made in a computer program, they are simply the result of a computation in which the final result is either TRUE or FALSE The value zero (0) is considered to be FALSE by C++. Any positive or negative value is considered to be TRUE

Using Relational Operators Relational operators provide the tools with which programs make decisions with true and false evaluations == equal to NOTE: this is two equals symbols next to each other, not to be confused with the assignment operator, = > greater than = greater than or equal to <= less than or equal to != not equal to

Using Logical Operators When complex decisions must be coded into an algorithm, it may be necessary to "chain together" a few relational expressions (that use relational operators) This is done with logical operators (also called Boolean operators.) && is the logical AND operator || is the logical OR operator ! is the logical NOT operator

Truth Tables Use this truth table to determine the results of the logical operators. In this table, 1 represents TRUE and 0 represents FALSE. Note that the ! symbol (the logical NOT operator) changes a TRUE to a FALSE.

Order of Logical Operations Logical operators may be mixed within evaluation statements but the following order of preference must be respected: 1. NOT operator (!) 2. AND operator (&&) 3. OR operator (||) Short-circuit evaluation in C++ allows the compiler to quickly evaluate a logical expression in some cases without having to completely evaluate each part of the expression

Complete order of operations The complete order of operations including all of the arithmetic, relational, and logical operators including all of the basic arithmetic, relational, & logical operators is: *, /, % +, -, =, ==, != ! && ||

Sample Evaluations - True The following control expressions have the resulting value of TRUE assuming that the integer variables a, b, & c have the values a = 1, b = 2, & c = 3. (a b || b > 0 && b > 1) (a > 0) (a) (-a) (a = 3)

Sample Evaluations: False While the following control expressions evaluate to FALSE where a = 1, b = 2, and c = 3: (a - 1) (!a) (0 * c) (c - a + b) Remember that the ! symbol (the logical NOT operator) changes a TRUE to a FALSE.