0% found this document useful (0 votes)
36 views

CSC425 Topic 2 Components of Programming Language - Part2

Components of Programming Language - Part2

Uploaded by

Syahmi Fadzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

CSC425 Topic 2 Components of Programming Language - Part2

Components of Programming Language - Part2

Uploaded by

Syahmi Fadzi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Components of a Programming

Language

Topic 2 - continued

1
Arithmetic Expressions, Operators
and Assignment Concept

2
Contents
 Assignment Statement
 Operators (Shortcut Operator, Unary Operator)
 Arithmetic Operators
 Order of Precedence
 Mathematical Library Functions
 Formatting Program Output
 String Manipulation

3
Assignment Statement
 Assignment statement is the most basic C++ statement
 Store the value of an expression in a variable
 Use Assignment Operator (=)
 Syntax:

variable = <variable|constant|expression>

 Expression can be :
i. a constant
ii. another variable
iii. an arithmetic expression
iv. a function

4
Assignment Statement (cont.)
 Examples of Assignment Statements:
length = oldLength;
width = 50;
area = length * width;
moreData = true; //a boolean variable

5
Assignment Statement (cont.)
 Exercise 1:
Write a program that takes input of length in feet. The
program should then convert and display the length in
centimeter. Assume that the given lengths in feet is integer.

 Exercise 2:
Write a program that read the radius of a circle. The program
should able to calculate the area, then display the area of the
circle to the user.

6
Assignment Statement (cont.)
 Exercise 1:
Write a program that takes input of length in feet. The
program should then convert and display the length in
centimeter. Assume that the given lengths in feet is integer.
Answer: lengthInCM = lengthInFeet * 30.48;
 Exercise 2:
Write a program that read the radius of a circle. The program
should able to calculate the area, then display the area of the
circle to the user.
Answer: areaCircle = 3.14 * r * r;

7
Assignment Statement (cont.)
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;

8
Assignment Statement (cont.)

 Exercises:

Expression Equivalent to
price = price * rate;
count = count + 3
amount /= 2;
price *= rate + 1

9
Assignment Statement (cont.)

 Answers :

Expression Equivalent to
price = price * rate; price *= rate;
count = count + 3 count += 3
amount /= 2; amount = amount / 2;
price *= rate + 1 price = price * (rate + 1)
but not
price = price * rate + 1

10
Assignment Statement (cont.)
 For a variable to increase or decrease by 1, C++ provides
two unary operators :
(++) : increment operator
(--) : decrement operator

Expression Equivalent to
i = i + 1  ++i (prefix increment operator)
If x = 5; and y = ++x;
After the second statement both x and y are 6
 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

11
Assignment Statement (cont.)
 Exercises:

Expression Value X Value Y


X = 6; Y = ++X
Y = 10; X = Y++;
X = 5; Y = --X;
Y = 10; X = Y--;

12
Assignment Statement (cont.)
 Answers:

Expression Value X Value Y


X = 6; Y = ++X; 7 7
Y = 10; X = Y++; 10 11
X = 5; Y = --X; 4 4
Y = 10; X = Y--; 10 9

13
Arithmetic Operators
 C++ Operators
+ addition
- subtraction
* multiplication
/ division
% remainder (mod operator)
 +, -, *, and / can be used with integral and floating-
point data types
 Unary operator - has only one operand
 Binary Operator - has two operands
Order of Precedence
 All operations inside of () are evaluated first
 *, /, and % are at the same level of precedence and are
evaluated next
 + and – have the same level of precedence and are
evaluated last
 When operators are on the same level
 Performed from left to right
Arithmetic Operators
 Exercises: Rewrite the following statements in C++
format.
 a. 27
 15  8  15
5
 b. 13  2 16  3 18

3
 c. 12  3  15  5  3
4
Arithmetic Operators
 Exercises: Rewrite the following statements in C++
format.
Answer:
 a. 27
 15  8  15 (27 / 5) + (15 * 8) – 15
5
 b. 13  2 16  3 18 13 + (2 * 16 / 3) – 18

3
 c. 12  3  15  5  3 12 + (3 / 4) – 3 + (15 / 5 * 3)
4
Mathematical Library Functions
 Header file cmath (math.h)
#include <cmath>
 used in the following form :
function_name(argument)
 These functions are the pre-defined functions

Example :
sqrt(16) //square root of x
fabs(2.3 * 4.6) //absolute value |x|
tan(x) //tangent of x
floor(x) //largest integer <= x
pow(2, 3) //2 to the power of 3
19
Formatting Program Output
 Besides displaying correct results, it is extremely
important for a program to present its output attractively.
 To include manipulators within an output display, must
include the header file using the preprocessor command
as follows :
#include <iomanip>
 Most commonly used manipulators in a cout statement :

20
Formatting Program Output
Manipulator Action
endl Manipulator moves output to the beginning of the next
line
setfill(‘x’) Sets the fill character from its default of a blank space to
the character x
setprecision(n) Outputs decimal numbers with up to n decimal places
showpoint forces output to show the decimal point and trailing
zeros
scientific Use exponential notation on output
left Left justify output
fixed Outputs floating-point numbers in a fixed decimal format
setw Outputs the value of an expression in specific columns

21
Formatting Program Output
Manipulator Examples
endl cout << “Hello world!” << endl;
setfill(‘x’) cout << setfill(‘x’) << setw (10) << 8;
setw
setprecision(n) cout << setprecision(5) << 3.14159;
showpoint cout << showpoint << 30;
noshowpoint
scientific cout << scientific << 3.1416;
left cout << setw(15) << right << 3.1416;
right
fixed cout << fixed << setprecision(5) << 3.14159;

22
String Manipulations
 String input
- header file :
#include <cstring>

- can be declared and initialized as :


string name;
name = “Abdul”;

or to read a string from the keyboard


cout << “What is your name ?”;
cin >> name;

23
String Manipulations
 String function :
string-name.length():
- to know the numbers of character in a string

string-name.substr(m,n):
- to extract substring from a full string

 String concatenation : using symbol ‘+’


- joining two or more strings together to
become one long string.
const string hello = "Hello";
const string message = hello +
",world" +
"!";
24
Thank you

25

You might also like