Completing the Basic (L06)

Slides:



Advertisements
Similar presentations
Operator Overloading. C++ 2 Outline  General technique  Overloading of the assignment operator  Overloading the increment and decrement operators.
Advertisements

Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
True or false A variable of type char can hold the value 301. ( F )
Lecture 071 CS 192 Lecture 7 Winter 2003 December 15-16, 2003 Dr. Shafay Shamail.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
CS150 Introduction to Computer Science 1
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
Compound Operators 03/07/11. More Operators, First Section 4.4.
Chapter 3 Assignment and Interactive Input
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
For Loops Programming. COMP102 Prog Fundamentals I: for Loops/Slide 2 The for Statement condition action true false initialization update.
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.
Basic Elements of C++ Chapter 2.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Operaciones y Variables
For Repetition Structures (L13) * General Form of the for Statement * Components of a Typical for Header * Pre/Postincrement of the Counter * Stream Manipulator.
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.
計算機程式語言 Lecture 03-1 國立臺灣大學生物機電系 3 3 Assignment, Formatting, and Interactive Input.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
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.
C++ Programming: Basic Elements of C++.
Nested Control Structures * Conditional Operator (?:) * Preincrement and Postincrement * Predecrement and Postdecrement * Counter-Controlled Repetition.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Lecture #6 OPERATORS AND ITS TYPES By Shahid Naseem (Lecturer)
A FIRST BOOK OF C++ CHAPTER 3 ASSIGNMENT AND INTERACTIVE INPUT.
Chapter 3: Assignment, Formatting, and Interactive Input.
LOOP & Type Conversion. do – while Loop In the while loop, the test expression is evaluated at the beginning of the loop. If the test condition is false.
C++ for Engineers and Scientists Second Edition Chapter 3 Assignment, Formatting, and Interactive Input.
CHAPTER 1.3 THE OPERATORS Dr. Shady Yehia Elmashad.
Chapter 3.1 & 3.2 s Programming s Assignment Statements s Incrementing & Decrementing s Math Library Functions.
Chapter 6 Mathematical Operations. 6.1 Mathematical Expressions In mathematics this expression is valid 0 = -4y + 5 It is invalid in programming Left.
Selection Control Structures (L08) * Selection Criteria * if Statements * The if-else Statement Selection Control Structure Dr. Ming Zhang.
© 2006 Pearson Education 1 Obj: to use assignment operators HW: Prelab Exercises 2 Quiz 3.1 – 3.4 in two class days  Do Now: 1.Read p.144 – 145 (3.4 “more.
Review 1 Computers and Programming I Dr. Ming Zhang Tel: (757) Fax: (757) Office: Gosnold 217b Subject Overview.
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.
Top-Down Stepwise Refinement (L11) * Top-Down Stepwise Refinement * Cast Operator * Promotion (Implicit Conversion) * Unary Operator * Multiplicative Operator.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
Literals A literal (sometimes called a constant) is a symbol which evaluates to itself, i.e., it is what it appears to be. Examples: 5 int literal
Chapter Topics The Basics of a C++ Program Data Types
Chapter 3 Assignment and Interactive Input.
Basic Elements of C++.
Chapter 2 Assignment and Interactive Input
Arithmetic operations & assignment statement
Arithmetic & other operations
Basic Elements of C++ Chapter 2.
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Lecture 3 Expressions Richard Gesick.
With Assignment Operator
A First Book of ANSI C Fourth Edition
Counting Loops.
Common Programming Errors Assignment Statements
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
C++ for Engineers and Scientists Second Edition
HNDIT11034 More Operators.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Completing the Basic (L06) *Assignment Operations * Formatting Numbers for Program Output Completing the Basic Dr. Ming Zhang

Assignment Operations * General Form of Assignment Statement variable = operand; * Constant Operand for Assignment length = 25; (is read, length is assigned the value 25) * Overwritten with New Value length = 6.28; ( The 25 that was in length is overwritten with new value of 6.28, because a variable can only store one value at a time) Completing the Basic Dr. Ming Zhang

Expression as the Assignment Operand An expression is any combination of constants and variables that can be evaluated to yield a result. * Expression as the Operand of Assignment Statement The expression in an assignment statement can be used to perform calculations using the arithmetic operators. diff = 16 - 6; product = 5 * 6; new_value=diff+product+20; /*new_value=60*/ Completing the Basic Dr. Ming Zhang

Exercise 1: the Value of a Circle Area (C++) # Using C++, write a program to calculate # the value of a circle area. #include <iostream> using std::cout; int main( ) { … } Output: The value of the circle area is 314.160000 Completing the Basic Dr. Ming Zhang

Assignment Variations The variable on the left of the equal sign can also be used the right of the equal sign in the assignment statement sum = 20; sum = sum +10; /* sum = 30 */ * Assignment expressions like sum = sum + 25, which use the same variable on both sides of the assignment operator, can be written using the following assignment operators: += -= *= %= Completing the Basic Dr. Ming Zhang

Assignment Operators sum = 20 sum += 10; ( sum = sum + 10; sum = 30) rate = 5; sum *= rate; ( sum = sum*rate; sum = 10) sum *= sum+2; (sum = sum*(sum+2); sum = 120) sum *= rate+2; (sum = sum*(rate+2); sum = 840) Completing the Basic Dr. Ming Zhang

Accumulating #include <iostream> int main(void) { int sum = 0; cout <<“The value of sum is set to ”<<sum<<endl; sum = sum +10; cout << “sum is now ” << sum; sum = sum +20; return 0;} Output: The value of sum is set to 0 sum is now 10; sum is now 30; /* sum is NOT 20*/ Completing the Basic Dr. Ming Zhang

Counting * Counting Statement Form variable = variable + fixed_number; * Examples of Counting Statement i = i + 1; n = n + 1; count = count + 1; j = j +2; m = m + 3 kk = kk + 4; Completing the Basic Dr. Ming Zhang

Increment and Decrement Operators * Increment Operators i = i + 1; ++i; n = n + 1; ++n; count = count + 1; ++ count; * Decrement Operators i = i - 1; --i; n = n - 1; --n; count = count - 1; -- count; Completing the Basic Dr. Ming Zhang

Example of Increment Operators #include <iostream> int main(void) { int count = 0; cout <<“The initial value of count is ” << count; ++count; cout <<“count is now ” << count ; ++count; ++count; cout <<“count is now ” << count; return 0;} Completing the Basic Dr. Ming Zhang

Exercise2 : Increment Operators (C++) # Using increament operators and C++, write a # program to print out on the screen: # The initial value of count is 0. # count is now 1. # count is now 3. #include <iostream> using std::cout; int main( ) { … } Output: Completing the Basic Dr. Ming Zhang

Data Type Conversion * Case Operator for Data Type Conversion (data_type) (expression); * Example double a=2.3, b=5.0, product1; int product2; product1 = a * b; /* product1 =11.5 */ product2 = (int) (a*b); /* product2 = 11 */ product1 = (int) a * b; /*(int)a==2. product1 = 10.0*/ Completing the Basic Dr. Ming Zhang

Example of Data Type Conversion #inlude <iostream> using std::cout; int main( ) { double a=3.3, b=6.0, product1; int product2; product1 = a * b; /* product1 =19.8 */ product2 = (int) (a*b); /* product2 = 19 */ product1 = (int) a * b; /*(int)a==3. product1 = 18.0*/ cout << “product1=“ << product1 << endl; cout << “product2=“ << product2 << endl; return(0); } Completing the Basic Dr. Ming Zhang

Exercise 3 : Data Type Conversion # Read follwing program and find out the output #inlude <iostream> using std::cout; int main( ) { double a=5.3, b=4.0, product1; int product2; product1 = a * b; product2 = (int) (a*b); product1 = (int) a * b; cout << “product1=“ << product1 << endl; cout << “product2=“ << product2 << endl; return(0); } Completing the Basic Dr. Ming Zhang