CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
Types and Arithmetic Operators
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.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 3 Expressions.
Constants and Variables  Memory cells used in program  Called constants and variables  Identifiers for constants and variables should be declared before.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Expressions and Operators Program Style.
Variables, Data Types, & Arithmetic Expressions CSC 1401: Introduction to Programming with Java Lecture 3 – Part 2 Wanda M. Kunkle.
CIS 234: Order of Operations, Shortcut & Other Operators Dr. Ralph D. Westfall February, 2004.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
1 Introduction to Computers and Programming Class 3 Introduction to C Professor Avi Rosenfeld.
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.
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 =
Operaciones y Variables
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Exercises A-Declare a variable of double type with initial value=0.0; B- Declare a constant of String type with initial value=“Good” C- Declare a variable.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
LESSON 6 – Arithmetic Operators
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
C Programming Lecture 6 : Operators Lecture notes : courtesy of Ohio Supercomputing Center, and Prof. Woo and Prof. Chang.
CPS120: Introduction to Computer Science Operations Lecture 9.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Mathematical Calculations in Java Mrs. G. Chapman.
Arithmetic in Pascal A Short Glance We will learn the followings in this chapter Arithmetic operators Order of precedence Assignment statements Arithmetic.
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
Executable Statements 1. Def. Executable statements are instructions to the computer to perform specific tasks. 2. Two examples: method calls and assignment.
Chapter 7 Expressions and Assignment Statements. Outline Introduction Arithmetic Expressions Overloaded Operators Type Conversions Assignment Statements.
Data Types Declarations Expressions Data storage C++ Basics.
Mathematical Calculations in Java Mrs. C. Furman.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Doing math In java.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
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.
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.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
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.
Chapter 7: Expressions and Assignment Statements
Lecture 3 Java Operators.
Operators and Expressions
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
BIL 104E Introduction to Scientific and Engineering Computing
ITEC113 Algorithms and Programming Techniques
Chapter 7: Expressions and Assignment Statements
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.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Operators and Expressions
Arithmetic Operator Operation Example + addition x + y
Conversions of the type of the value of an expression
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
Chapter 4: Expression and Operator
Presentation transcript:

CP104 Introduction to Programming Overview of C Lecture 4__ 1 Assignment Statements An assignment statement is to store a value in a variable variable name = a value or an expression; –Declaration must be done before assigning a value to a variable !!! Examples int a; a = 3;  int a = 3;  int a = 3.56; double f = ;  double f = 3.561e3; char c = ’a’;  char c = 97; int a, b, c = 5; int a=b=c=5;  int a=5, b=5, c=5; x = y + z + 2.0; x = x + 1;

CP104 Introduction to Programming Overview of C Lecture 4__ 2 Examples int x = 5; x = x + 3; What the value in x after executing the above two statements? Data type conversion int z; double y = 10.11; z = y; /* or x = (int) y; */

CP104 Introduction to Programming Overview of C Lecture 4__ 3 Testing example #include main() { int a = 3.5, b; double f = e3; char c = 97; b = a + 3.0; printf("%d\n", a); printf("%f\n", f); printf("%c\n", c); printf("%f\n", b); fflush(stdin); getchar(); } What are the output of the above program ?

CP104 Introduction to Programming Overview of C Lecture 4__ 4 Arithmetic Operations +, -, *, / % For +, -, *, /, if both operands have the same type, the result has the same type. If one operand is float, the result will be float Addition “+” : e.g , Subtraction “–” : e.g. 7 – 2, 5 – 7, Multiplication “ * ” : e.g. 3 * 5 = ?, 3.0*5 = ? Division “ / ” : e.g. 4 / 2 = ? 7/2 = ? 7/2.0 = ? 7/2+0.5 = ? Modulus “%” (remainder of a division) –both operands must be integers or type int e.g. 7 % 3 = 1 15 % 10= ? 10 % 15= ? 4 % 3= ? 7 % 3.0 = ?

CP104 Introduction to Programming Overview of C Lecture 4__ 5 More examples / 2=? 3.0 * 5 / 2=? 4 / 5 / 2=? int m = 15, n = 10; double x; x = m / n; x = ?

CP104 Introduction to Programming Overview of C Lecture 4__ 6 Arithmetic Expressions An expression consists of variables, values, and special symbols +, -, *, /, % (reminder), (, ) in a mathematical style –Example x*y*c + (x+c*d) Precedence of Arithmetic Operators –( ) -Parentheses done first, inside-out –Unary - e.g. -x –*, /, % –+, - –Operators at the same precedence level are computed LEFT to RIGHT (except unary operation)

CP104 Introduction to Programming Overview of C Lecture 4__ 7 Arithmetic Expressions (con’d) The data type of an expression –The data type of an expression is determined by its operands If all variable and data have the same data type, then the expression has the same data type –Mixed-type is one with different data types. If there is a double data type, then the expression is a double data type Mixed-type assignment –Type cast converts an expression to a different data type by writing the desired type in parenthesis Evaluation can be down by an evaluation tree

CP104 Introduction to Programming Overview of C Lecture 4__ 8 Arithmetic Expressions (con’d) Examples: int x=5, y=3, z=4; double d=0.5; What are the types and values of x+y*z, x*y + x/z*d, x/z*(int)d ?

CP104 Introduction to Programming Overview of C Lecture 4__ 9 Evaluation Tree for area = PI * radius * radius;

CP104 Introduction to Programming Overview of C Lecture 4__ 10 Evaluation Tree and Evaluation for v = (p2 - p1) / (t2 - t1);

CP104 Introduction to Programming Overview of C Lecture 4__ 11 Evaluation Tree and Evaluation for z - (a + b / 2) + w * -y

CP104 Introduction to Programming Overview of C Lecture 4__ 12 Simplified Assignment Expressions (see demo) a +=3;  a = a+3; x *= y+3  x = x*(y+3); m = i++;  m = i; i = i+1;  m = i; i += 1; m = ++i;  i=i+1; m = i;  i+=1; m = i; n = i--;  n = i; i = i-1;  n = i; i -= 1; n = --i;  i-=1; n = i;