Download presentation
Presentation is loading. Please wait.
Published byJoshua Boone Modified over 9 years ago
1
CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
2
C ONTENTS Arithmetic Expressions Operators Addition, subtraction,multiplication, division, modulus Binary and unary operator Negation operator 2 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
3
3 Variables and constants of integral and floating point types can be combined into expressions using arithmetic operators. In C++, we have to represent the algebraic expression by using the valid syntax. A RITHMETIC EXPRESSION CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
4
4 Is the most basic C++ statement Use to store the value of an expression in a variable Use Assignment Operator (=) Syntax: variable = Expression can be : i.a constant ii.another variable iii.an arithmetic expression iv.a function Assignment statement CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
5
5 Examples: length = oldLength; width = 50; area = length * width; moredata = true;//a boolean variable x = total ( ); Assignment statement CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
6
A RITHMETIC OPERATOR Generally, there are three types of operator: Unary Binary Ternary These term reflect the number of operands an operator requires. Unary operators only require a single operand. Ex: -7 Represent the value negative 7 The literal 7 is preceded by the minus sign, is called the negation operator. Since it only requires one operand, it is a unary operator 6
7
A RITHMETIC OPERATOR CONT … Binary operator work with two operands Ex: total = cost + tax; Average = total / 3; Ternary operator work with three operands Ex: total = num1 + num2 + num3; 7
8
8 Assignment Operation Variations of Assignment Operation : We can have assignment statement like this : sum = sum + 10; This statement can be written using the following shortcut assignment operators : += -= /=%=*= Hence, the equivalent “ sum = sum + 10 ”, would be : sum += 10; CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
9
9 A RITHMETIC EXPRESSION Example : ExpressionEquivalent to price = price * rate;price *= rate; count = count + 3count += 3 price *= rate + 1price = price * (rate + 1) but not price = price * rate + 1 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
10
10 Assignment Operation For a variable to increase or decrease by 1, C++ provides two unary operators : (++) : increment operator (--): decrement operator o Increment & Decrement Operators: ExpressionEquivalent to i = i + 1i++ (postfix increment operator) If x = 5; and y = x++; After the second statement y is 5 and x is 6 i = i - 1i-- CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
11
Arithmetic Operators Defined in C++Example Addition+ 20.9 + 3 = 23.9, or 20 + 3=23 Subtraction- 50 – 10 =40, or 50.4-10= 40.4 Multiplication* 10*3=30, or 10.5*3= 31.5 Division/ 10/3 = 3, or 10.0/3= 0.3333 Modulus (remainder) % 10%3=1 Never use the modulus with floating- point values. Operators
12
12 Mathematical Library Functions Header file #include used in the following form : function_name(argument) Example : sqrt(x) //square root of x pow(a,2)//a 2 fabs(2.3 * 4.6)//absolute value |x| tan(x)//tangent of x floor(x)//largest integer <= x More reference at: http://www.cplusplus.com/reference/cmath/floor/ CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
13
Arithmetic Operations Examples #include void main (){ int number1, number2; int sum, different, product, quotient, remainder ; number1 = 8; number2 = 4; sum = number1 + number2; different = number1 - number2; product = number1 * number2; quotient = number1 / number2; remainder = number1 % number2; cout<<"sum : " <<sum<<endl; cout<<"different: " <<different<<endl; cout<<"product : " <<product<<endl; cout<<"quotient : " <<quotient<<endl; cout<<"remainder : " <<remainder<<endl; } 13 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING Output:
14
Integer Division IF two integers are divided, the result will be an integer number. Any fraction will be truncated. int / int int Example: int a = 5, b = 2; float c; c = a / b; //the new value of c is 2 cout << a / b; //2 will be displayed 14 Arithmetic Operations CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
15
When more than one operator will be executed to determine the result, C++ executes the arithmetic operators in the following order: Operator (s)OperationPrecedence ( )parentheses Evaluated first, inside-out if nested or left to right if same level *,/,% Multiply, Divide, Modulus Evaluated second, left to right +, -Add, SubtractEvaluated last, left-to-right Precedence & Associativity
16
Example: a) 3 * 7 – 6 + 2 * 5 / 4 + 6 means ( 3 * 7 ) – 6 + ( ( 2 * 5 ) / 4 ) + 6 = 21 – 6 + ( 10 / 4 ) + 6( evaluate * ) = 21 – 6 + 2 + 6( evaluate / ) = 21 – 6 + 8( evaluate + ) = 21 + 2( evaluate +) = 23( evaluate += result) Precedence & Associativity
17
b)3 + 4 * 5 means 3 + ( 4 * 5 ) = 3 + 20( evaluate * ) = 23( evaluate + ) c)2 * 3 / 2(evaluate operators from left to right) = 6 / 2(evaluate * ) = 3(evaluate / ) Precedence & Associativity
18
Write a program that does the following : 1. Find the sum, subtract and average of two integers 18 Exercise 1 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
19
Try to write a program : Convert Length Write a program that takes as input given lengths expressed in meters and millimeters. The program should then convert and output the length in centimeters. Assume that the given lengths in meters and milimeters are integers. 19 Exercise 2 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
20
20 Translate the following flow chart into source code: Begin End Read total Read count Display average average = total / count Exercise 3 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
21
21 Given the following declaration: int a, b, c, v1, v2; a = 8; c = b = 3; c = c – 1; v1 = a / b; v2 = a / c; What is the final content of a, b, c, v1 and v2? Exercise 4 CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.