Chapter 3.1 & 3.2 s Programming s Assignment Statements s Incrementing & Decrementing s Math Library Functions.

Slides:



Advertisements
Similar presentations
Arithmetic Calculations
Advertisements

Computer Programming w/ Eng. Applications
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
BBS514 Structured Programming (Yapısal Programlama)1 Functions and Structured Programming.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
1 Lecture 6 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
CS150 Introduction to Computer Science 1
Compound Operators 03/07/11. More Operators, First Section 4.4.
Chapter 3 Assignment and Interactive Input
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
1 Data types, operations, and expressions Continued l Overview l Assignment statement l Increment and Decrement operators l Short hand operators l The.
1  Ex: Declare a variable to store user’s age: int age; Prompt the user to enter his/her age: printf (“ How old are you? “); Read / scan the entered value.
Data Types, Expressions and Functions (part I)
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Operaciones y Variables
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
CHAPTER:8 OPERATORS AND EXPRESSION IN C++ Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Chapter 3 COMPLETING THE BASICS Programming Fundamentals with C++1.
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.
Completing the Basic (L06)
CSE 100 s s Input: cin s type casting Math Library Functions math.h sqrt(n) fabs(n) cos(n) log10(n) log(n) pow(b, n) etc. * * *
計算機程式語言 Lecture 03-1 國立臺灣大學生物機電系 3 3 Assignment, Formatting, and Interactive Input.
CSE1222: Lecture 3The Ohio State University1. Assignment Operations  The C++ assignment operator is: =  Examples: x = 3 * 5; y = x – 7; y = y + 4; Do.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Chapter 3: Assignment, Formatting, and Interactive Input.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Expressions and Interactivity. 3.1 The cin Object.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Chapter 3 Assignment, Formatting, and Interactive Input C++ for Engineers and Scientists Third Edition.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
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 =
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
CSCI 125 & 161 / ENGR 144 Lecture 6 Martin van Bommel.
Arithmetic Expressions
Chapter 7: Expressions and Assignment Statements
Chapter 3 Assignment and Interactive Input.
for Repetition Structures
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Chapter 2 Assignment and Interactive Input
Relational Operations
Chapter 7: Expressions and Assignment Statements
User-Defined Functions
Operators and Expressions
Expressions and Interactivity
OPERATORS (2) CSC 111.
Lecture 3 Expressions Richard Gesick.
A First Book of ANSI C Fourth Edition
Assignment Operators Topics Increment and Decrement Operators
Assignment Operators Topics Increment and Decrement Operators
CSE 100 Input: cin type casting.
Common Programming Errors Assignment Statements
Data Types and Expressions
C++ for Engineers and Scientists Second Edition
Assignment Operators Topics Increment and Decrement Operators
Chapter 4 Procedural Abstraction and Functions That Return a Value 1
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Chapter 3.1 & 3.2 s Programming s Assignment Statements s Incrementing & Decrementing s Math Library Functions

Review: Assignment Operator The assignment operator (=)causes the operand on the left to take on the value to the right side of the statement. This operator assigns from right to left. Syntax: variable = value validinvalid riker = = riker *

Assignment Example 1 #include #include Using namespace.std; void main(void) { int sum; int sum; sum = 25;//initialize sum sum = 25;//initialize sum cout << “The number stored in sum is " cout << “The number stored in sum is " << sum; << sum; sum = sum + 10; sum = sum + 10; cout << "\nThe number now stored in sum is " << sum<< ‘\n’; cout << "\nThe number now stored in sum is " << sum<< ‘\n’; } sum 25 35

Example 1: Output Output: The number stored in sum is 25 The number now stored in sum is 35 No surprises

Assignment Example 2 int sum; sum = 0; cout << "\nThe value of sum is initially set to " << sum; sum = sum + 10; cout << "\nsum is now " << sum; sum = sum + 20; cout << "\nsum is now " << sum; sum = sum - 30; cout << "\nsum is now " << sum; sum = sum - 40; cout << "\nThe final sum is " << sum;

Assignment Example 2 int sum; sum = 0;// initialize sum cout << "\nThe value of sum is initially set to " << sum; sum = sum + 10; cout << "\nsum is now " << sum; sum = sum + 20; cout << "\nsum is now " << sum; sum = sum - 30; cout << "\nsum is now " << sum; sum = sum - 40; cout << "\nThe final sum is " << sum; Sumcout ??? A Trace of Ex2

Example 2 - Output Output: The value of sum is initially set to 0 sum is now 10 sum is now 30 sum is now 0 The final sum is -40 Hopefully, no surprises here either

Assignment Operators A shorthand notation for certain assignments. They all have right-to-left associativity. MyVariable += TaxRate * Cost variable op= (expression) is equivalent to variable = variable op (expression) Surprise! MyVariable = MyVariable + TaxRate * Cost

+=add then assign - =subtract then assign *=multiply then assign /=divide then assign %=modulus, then assign X - = 3  X = X - 3 pay *= 0.35  pay = pay * 0.35 Assignment Operators

Assignment Operators += - = * = /= %= 1. i += 2i = i r * = 7r = r * 7 3. j * = (k + 3)j = j * (k + 3) 4. x /= y - 4x = x /y hour %= 12hour = hour % left - = t_outleft = left - t_out Assignment Operators *

Common Use Accumulating Subtotals variable = variable + new_value; Syntax: variable = variable + new_value; Examples year_pay = year_pay + pay; balance = balance - debit; counter = counter + 1; counter += 1; * } same

Increment/Decrement ++increment --decrement Surprise again! num++num-- ++num--num unary operators take a single operand num++, num-- ++num, --num

Increment/Decrement k = k + 1k = k + 3 k += 1k += 3 k ++ no equivalent

Increment/Decrement num = num + 1 num++ i = i + 1 i++ num = num - 1 num-- i = i - 1 i-- * * * * num += 1 num - =1 i += 1 i - = 1

Increment/Decrement k g value after execution k g 1. k = 7; 2. g = 2; 3. k = g; 4. g = g + 1; * * * * or combine 3 & 4 k = g++ Use it first, then add 1

postfix:first use it, then alter value Increment/Decrement postfix:first use it, then alter value z = 10; v = z--; cout <<v<<‘\t’<<z; v z 109 count = 10; k = count++; cout<<k<<‘\t’<<count; k count 1011 * *

output 1 cout << cnt++<<'\n'; 2 cout<<cnt<<'\n'; 3 cout<<(cnt++==guess)<<'\n'; 4 cout<<cnt<<'\n'; 5 cout<<cnt++<<'\n'; 6 cout<<cnt<< '\n'<<'\n'; Use Before Increment/Decrement * * * // print then inc 10// print then inc 11 // check then inc 1// check then inc int cnt = 10, guess = 11;

output 1 cout << ++cnt<<'\n'; 2 cout<<cnt<<'\n'; 3 cout<<(++cnt==guess)<<'\n'; 4 cout<<cnt<<'\n'; 5 cout<<++cnt<<'\n'; 6 cout<<cnt<< '\n'<<'\n'; Use After Increment/Decrement * * * // inc then print 11// inc then print 11 // inc then check 0// inc then check int cnt = 10, guess = 11;

Increment/Decrement a) cout << j++ b) cout << ++j c) cout << j += 14 d) cout << j /= 10 e) cout << j *= 10 f) cout << j - = 6 g) cout << (j = 5) + j h) cout << (j == 5) + j * * int j = 5; a) a) 5 b) b) 6 c) c) 19 d) d) 0 e) e) 50 f) f) -1 g) g) 10 h) h) 6

Math Library Functions cmath sqrt(n) fabs(n) cos(n) log10(n) log(n) pow(b, n) etc. * * * #include Function prototypes (or declarations)

Math Library Functions name of the function what it does data type of argument data type of returned value The actual function (object code) in usr/lib/libm.h g++ calculator.cc -lm Do not have to use

Math Library Functions function_name (argument); Syntax: function_name (argument); Ex.sqrt(49) pow(2.1, 3) abs(-34.5) cos(30) abs(34.5) *

Math Library Functions nested functions sqrt( pow ( fabs (-4), 3) ) = sqrt( pow ( 4.0, 3) ) = sqrt( 64.0 ) = 8.0 * * * You can use returned values from functions in any expression cout << sqrt(64.0); value = 23 * sqrt(number) + 5;

Type Casting The explicit conversion of a value from one data type to another. data_type (expression) Syntax: data_type (expression) * int (5.34 * 1.68) int (8.9712) This returns a value of 8.

Type Casting someInt = someDouble - 8.2; someInt = int(someDouble - 8.2); These are identical statements. *

Type Coercion The implicit (automatic) conversion of a value from one data type to another. someDouble = 42; is stored as 42.0 someInt = 11.9; is stored as 11 * g++ warning

Common Programming Errors not declaring all variables storing data of one type in a variable of a different type. The variable data type is kept. using a variable before assigning it a value mixing data types in an operation in integer division 4/5 = 0

More Common Programming Errors << ; forgetting << and ; not initializing variables before use ++ – applying ++ or – incorrectly

Practice Assignment An aquarium consists of four panels, all of which are metal except for the largest which is glass. All three side panels are rectangles. The bottom panel is a right triangle. There is no top panel. After getting user input on the dimension, calculate the area of the glass panel.

Assume the user enters positive real numbers expressed in inches. Comments are not required. sample output Height: [10.0] Width: [9.0] Length: [12] The glass is square inches. Practice Assignment

dummy box for extra sound “Sleeping is not a waste of time.” “Sleeping is not a waste of time.” Deepak Chopra “Except in C++ class” “Except in C++ class” Joseph DeLibero