BASIC ELEMENTS OF COMPUTER PROGRAMS Part 2
BASIC ELEMENTS OF COMPUTER PROGRAMS Part 2
• int age;
• double total_Price;
• char gender;
• bool status;
• string name;
When writing the declaration statement, the correct data type for the variable defined in
our problem statements must be determined. Giving suitable variable names relevant to the
problem statement is also good practice. These requirements will make the resulting program
easy to understand.
4.2 CONSTANT VARIABLE DECLARATION
Constant variables must also be declared before they can be used. As mentioned earlier, all
variables must be declared before they are used in a program. As we know, a constant variable
is a variable that has a value that never changes. So, we have to clarify the value once we declare
it, as the computer will use it as we have declared.
To declare the constant variable, we will use the literal value, which means that we are using the
constant value. A literal value is any fixed or constant value used in a program. To write the
declaration statement for a constant variable, we will write starting with const and continue with
the data type and variable name and assign the literal or actual value of the variable to it.
The syntax of a declaration statement for a constant variable is:
const data type variable name = literal or actual value;
Examples:
5. ARITHMETIC EXPRESSIONS
5.1 ARITHMETIC OPERATORS
Arithmetic expressions are the mathematical formula used to solve the problem statement.
Usually, in solving a problem, we will do some calculations or evaluations to find the solution. In
C++ programming, a few operators are defined in the arithmetic expressions.
There are five (5) basic operators used in C++ expressions. They are addition (+), subtraction (-),
multiplication (*), division (/), and modulus (%).
To write an arithmetic expression or mathematical formula in C++ programming, we will write
the expression with the operator and continue with the assignment operator (=). A semicolon
should be placed at the end of each arithmetic expression.
Example:
ANS = X + Y;
The following example shows an arithmetic expression to add variable x to variable y and
store the answer in the variable ANS.
Process Symbol/Operators Example
Addition + 40.5 + 2.2 = 42.7 or 28 + 5 = 33
Subtraction - 60 – 30 = 30 or 50.2 – 20 = 30.2
Multiplication * 7 * 3 = 21 or 10.5 * 3 = 31.5
Division / 6 / 3 = 2 or 10.0 / 3 =0.3333
Modulus (remainder) % 29 % 9 = 2 or 8 % 2 = 0
Never use modulus with a floating point values
A summary of explanations and examples for C++ expressions
OR
double price;
price = 37.99;
// a variable named price is declared as a double data type, and now the memory cell named price contains
the value 37.99
Besides simple assignment (=), we also have a situation of (==) whereby this assignment is used
to do a comparison between the right and left sides of assignments (==). For example, in the selection
type of program, we need to do a comparison of the answer that the user keys in with the defined value,
especially values using char and string data type as shown below:
If (ans==’y’)
True statements;
Else
False statements;
More examples:
The compound assignment is another term that shows we need to perform the calculation based
on the expression given.
Examples:
1. 2 + 2 * 5
Solution:
2 + (2 * 5)
= 2 + 10 // evaluate *
= 12 // evaluate +
2. 2 * 3 / 2 % 2
Solution: // evaluate operators from left to right
= 6 /2 // evaluate *
= 3 % 2 // evaluate %
=1
5.4 INCREMENT AND DECREMENT OPERATORS (POSTFIX AND PREFIX)
The increment is the process of adding value; the decrement subtracts value in a statement.
Usually, incremental and decremental expressions will be used in looping. Postfix means we will
perform the expression after the evaluation. Prefix means we will perform the evaluation
immediately.
The table below shows how increments and decrements for postfixes and prefixes are calculated.
Given a = 4 Given a = 4
++a --a
It means: a = a + 1 It means: a = a - 1
=4+1 =4-1
=5 =3
Function Operation
sqrt(x) Returns the square root of the argument x
pow(x,y) Returns x raised to the power of y
pow10(y) Returns 10 raised to the power of y
sin( ) Returns the sine of argument (radians)
hypot(a,b) Returns the hypotenuse of right triangle with sides a and b
log( ) Returns the natural log of the argument
log10( ) Returns the base 10 log of the argument
abs( ) Returns the absolute value of the argument
List of predefined functions in the maths library
Example:
// Program to calculate and show the application of the maths library function
// Header files
#include<iostream>
#include<math.h>
// Main function
int main( )
{ //program starts
cout<<”Number=”<<number1<<endl;
return 0;
} //program ends
----------------------------------------------
Output:
Number = 5
cin>>num1;
cin>>code;
The examples above show how to read a numeric data type.
For char [ ] that has more than 1 value and string data type, we need to include the string header
to allow the process to be executed. The reading process will have getline keyword whereby the
the keyword will inform the computer to accept the string value. The reading process for char [ ]
and string data type can be performed using the general syntax as shown in the following
example:
cin.getline(variable, length); // for char with size
getline(cin,name); // for string data type
Example 1:
//Program to read char data type
//header files
#include<iostream>
using namespace std;
//main function
int main( )
cin>>ws;
cin.getline(student_name, 10);
return 0;
} // program ends
Example 2:
//main function
int main( )
string student_name;
cin>>ws;
getline(cin,student_name);
return 0;
} // program ends
For display instructions and outputs in C++, we will use the cout statement. For display
instructions, we will write the instruction in double quote after the cout<< and end the statement with
the semicolon (;). The syntax of the display instruction is shown below:
For display output in C++, we will use the cout statement, place the variable name, and end the statement
with a semicolon (;). The syntax of the display output is as follows:
cout<< output variable name;
If we want to combine an instruction and display output in one statement. It can be written like this:
Example:
//main function
int main( )
int age;
age = 25;
// display string
// display value
return 0;
} // program ends