chapter 2 components of a programming language i ntroduction t o c omputer p rogramming (csc425)
TRANSCRIPT
![Page 1: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/1.jpg)
CHAPTER 2CHAPTER 2COMPONENTS OF A COMPONENTS OF A
PROGRAMMING PROGRAMMING LANGUAGELANGUAGE
INTRODUCTION TO INTRODUCTION TO COMPUTER COMPUTER
PROGRAMMINGPROGRAMMING(CSC425)(CSC425)
![Page 2: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/2.jpg)
CONTENTS
Arithmetic Expressions Operators
Addition, subtraction,multiplication, division, modulus
Binary and unary operator Negation operator
2CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
![Page 3: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/3.jpg)
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.
ARITHMETIC EXPRESSION
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
![Page 4: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/4.jpg)
4
Is the most basic C++ statement Use to store the value of an expression in a variable Use Assignment Operator (=) Syntax:variable = <variable|constant|expression>
Expression can be :i. a constantii. another variableiii. an arithmetic expressioniv. a function
Assignment statement
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
![Page 5: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/5.jpg)
5
Examples:
length = oldLength;width = 50;area = length * width;moredata = true; //a boolean variablex = total ( );
Assignment statement
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
![Page 6: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/6.jpg)
ARITHMETIC 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
![Page 7: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/7.jpg)
ARITHMETIC 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
![Page 8: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/8.jpg)
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
![Page 9: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/9.jpg)
9
ARITHMETIC EXPRESSION Example :
Expression Equivalent to
price = price * rate; price *= rate;
count = count + 3 count += 3
price *= rate + 1 price = price * (rate + 1)
but not
price = price * rate + 1
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
![Page 10: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/10.jpg)
10
Assignment Operation For a variable to increase or decrease by 1, C++ provides two
unary operators :
(++) : increment operator
(--) : decrement operatoro Increment & Decrement Operators:
Expression Equivalent to
i = i + 1 i++ (postfix increment operator)
If x = 5; and y = x++;
After the second statement y is 5 and x is 6
i = i - 1 i--
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
![Page 11: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/11.jpg)
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
![Page 12: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/12.jpg)
12
Mathematical Library Functions
Header file #include <math.h> used in the following form :
function_name(argument)
Example :sqrt(x) //square root of xpow(a,2) //a2
fabs(2.3 * 4.6) //absolute value |x|tan(x) //tangent of xfloor(x) //largest integer <= x
More reference at:http://www.cplusplus.com/reference/cmath/floor/
CSC425 : INTRODUCTION TO COMPUTER PROGRAMMING
![Page 13: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/13.jpg)
Arithmetic Operations Examples
#include<iostream.h>
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:
![Page 14: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/14.jpg)
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
![Page 15: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/15.jpg)
When more than one operator will be executed to determine the result, C++ executes the arithmetic operators in the following order:
Operator (s) Operation Precedence
( ) parentheses Evaluated first, inside-out if nested or left to right if same level
*,/,% Multiply, Divide, Modulus
Evaluated second, left to right
+, - Add, Subtract Evaluated last, left-to-right
Precedence & Associativity
![Page 16: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/16.jpg)
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
![Page 17: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/17.jpg)
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
![Page 18: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/18.jpg)
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
![Page 19: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/19.jpg)
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
![Page 20: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/20.jpg)
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
![Page 21: CHAPTER 2 COMPONENTS OF A PROGRAMMING LANGUAGE I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)](https://reader036.vdocument.in/reader036/viewer/2022062314/56649f1a5503460f94c2f77f/html5/thumbnails/21.jpg)
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