MIZAN-TEPI UNIVERSITY
TEPI CAMPUS
College of Computing and Informatics
Department of Information Systems
Assignment of Computer Programming
Individual Assignment
Name: Abduwasi Ahmed
ID No: 0116/14
Submission date:
16/8/2014
Submitted to: Petros
A.
.
Table of Contents
INTRODUCTION......................................................................................................................................2
Variables.....................................................................................................................................................3
Creating More Than One Variable at a Time......................................................................................3
Variable initialization...........................................................................................................................4
Basic Data Types………………………………………………………………………………………………………………………………4
C++ data types and their ranges...........................................................................................................4
Type int...................................................................................................................................................5
Type double, float..................................................................................................................................6
Type String.............................................................................................................................................6
Operatos…………………………………………………………………………………………………………………………………………..7
Assignment Operators (=)....................................................................................................................8
Logical operators ( !, &&, || )...............................................................................................................10
Conditional operator ( ? ).....................................................................................................................11
Precedence of operators.......................................................................................................................13
REFERENCES............................................................................................................................................15
1|Page
INTRODUCTION
In this part of assignment we will discuss about variables, data types and
different types of operators we use usually in c++ programs.
To understand the concepts of computer program its important to know what
means by variables, what means data types and what means operators.
Variables means nothing but simply it means a symbolic name for memory
location in which data can be stored and subsequently recalled.
Another thing that is basic in programming is data types. We can say
programming is nothing without data types, it is fundamental concepts in
programming.
There are different symbols in the computer that tells the compiler to
perform a specific mathematical or logical manipulations. Those symbols
are called Operators.
We will see those three important basic concepts in this assignment one by
one.
2|Page
Variables
A variable is a symbolic name for a memory location in which data
can be stored and subsequently recalled.
Variables are used for holding data values so that they can be utilized
in various computations in a program.
All variables have two important attributes:
A type, which is, established when the variable is defined (e.g.,
integer, float, character)
A value, which can be changed by assigning a new value to the
variable
Variable Declaration
Declaring a variable means defining (creating) a variable
You create or define a variable by stating its type, followed by one or more
spaces, followed by the variable name and a semicolon.
The variable name can be virtually any combination of letters, but cannot
contain spaces and the first character must be a letter or an underscore
Example:-
int myAge;
Variables must be declared before used!
Case sensitive
Creating More Than One Variable at a Time
You can create more than one variable of the same type in one statement by
writing the type and then the variable names, separated by commas.
For example:
int myAge, myWeight; // two int variables
long area, width, length; // three longs
myAge and myWeight are each declared as integer variables.
The second line declares three individual long variables named area, width, and
length.
However keep in mind that you cannot mix types in one definition statement
3|Page
Variable initialization
You assign a value to a variable by using the assignment operator (=)
Thus, you would assign 5 to Width by writing
int Width;
Width = 5;
You can combine these steps and initialize Width when you define it by writing
int width = 5;
Just as you can define more than one variable at a time, you can initialize more
than one variable at creation. For example:
// create two int variables and initialize them
int width = 5, length = 7;
Basic Data Types
When you define a variable in C++, you must tell the compiler what kind of
variable it is: an integer, a character, and so forth
Several data types are built into C++.
Basic (fundamental) data types in C++ can be conveniently divided into numeric
and character types
Numeric variables can further be divided into integer variables and floating-point
variables
Integer variables will hold only integers whereas floating number variables can
accommodate real numbers.
4|Page
C++ data types and their ranges
Type Size Values/ranges
unsigned short int 2 bytes 0 to 65,535
short int(signed short int) 2 bytes -32,768 to 32,767
unsigned long int 4 bytes 0 to 4,294,967,295
long int(signed long int) 4 bytes -2,147,483,648 to 2,147,483,647
int 2 bytes -32,768 to 32,767
unsigned int 2 bytes 0 to 65,535
signed int 2 bytes -32,768 to 32,767
Char 1 byte 256 character values
Float 4 bytes 3.4e-38 to 3.4e38
Double 8 bytes 1.7e-308 to 1.7e308
long double 10 bytes 1.2e-4932 to 1.2e4932
Type int
represent integers or whole numbers
Some rules to follow
Plus signs do not need to be written before the number
Minus signs must be written when using negative #’s
Decimal points cannot be used
Commas cannot be used
5|Page
Leading zeros should be avoided
Signed - negative or positive
Unsigned - positive
Short
Long
Type double, float
Used to represent real numbers
Many programmers use type float avoid leading zeros, trailing zeros are ignored
long double
Type char
Used to represent character data
A single character which includes a space
1 byte, enough to hold 256 values
Must be enclosed in single quotes eg. ‘d’
Escape sequences treated as single char
Type String
Used to represent textual information
String constants must be enclosed in double quotation marks eg. “Hello world!”
empty string “”
new line char or string “\n”
“the word \”hello\”” (puts quotes around “hello” )
The following program reads the four different data type inputs and outputs listed
on the above. The following program reads the four different data type inputs and
outputs listed on the above
6|Page
Operators
An operator is a symbol that tells the compiler to perform specific mathematical or logical
manipulations.
Operators are symbols that perform operations on variables and values
C++ is rich in built-in operators and provide the following types of operators
Assignment Operators
Arithmetic Operators
Relational and equality operators
Logical operators
Conditional operator
Bitwise Operators
7|Page
Assignment Operators (=)
The assignment operator assigns a value to a variable.
a = 5;
This statement assigns the integer value 5 to the variable a
C++ provides five basic arithmetic operators
Operator Name Example
+ Addition 12 + 4.9 // gives 16.9
- Subtraction 3.98 - 4 // gives -0.02
* Multiplication 2 * 3.4 // gives 6.8
/ Division 9 / 2.0 // gives 4.5
% Remainder 13 % 3 //gives 1
Modulo is the operation that gives the remainder of a division of two
values. For example, if we write:
8|Page
Increment and decrement (++, --)
c++;
the increase operator (++) and the decrease operator (--) increase or c+=1;
reduce by one the value stored in a variable c=c+1;
They are equivalent to +=1 and to -=1, respectively
This operator can be used both as a prefix and as a suffix
That means that it can be written either before the variable identifier (++a) or after
it (a++)
a++ or ++a both have exactly the same meaning
a prefix (++a) the value is increased before
a suffix (a++) the value stored in a is increased after being evaluated
In Example 1, B is increased before its value is copied to A. While in Example 2,
the value of B is copied to A and then B is increased.
Relational and equality operators ( ==, !=, >, <, >=, <= )
In order to evaluate a comparison between two expressions we can use the
relational and equality operators
The result of a relational operation is a Boolean
9|Page
Logical operators ( !, &&, || )
The Operator ! is the C++ operator to perform the Boolean operation NOT
producing false if its operand is true and true if its operand is false.
The operator && corresponds with Boolean logical operation AND
This operation results true if both its two operands are true, and false otherwise
The operator || corresponds with Boolean logical operation OR
This operation results true if either one of its two operands is true,
thus being false only when both operands are false themselves
Eg; !(5==5)……false
!(6==8)….true
a B !a A&&b a || b
True True False True True
True False False False True
False True True False True
false False True False False
Conditional operator ( ? )
The conditional operator evaluates an expression returning a value if that
10 | P a g e
expression is true and a different one if the expression is evaluated as false
Its format is:
condition ? result1 : result2
If condition is true the expression will return result1, if it is not it will return result2
Example:-
7 == 5 ? 4:3 //returns 3, since 7 is not equal to 5.
7 == 5+2 ? 4:3 //returns 4, since 7 is equal to 5+2.
// conditional operator
#include <iostream>
using namespace std;
int main () output
{ 7
int a,b,c;
a=2;
b=7;
c = (a>b) ? a : b;
cout << c;
return 0;
}
Bitwise Operators
Bitwise AND (&) takes two numbers as operands and does AND on every bit of
two numbers.
The result of AND is 1 only if both bits are 1.
Bitwise OR ( | ) takes two numbers as operands and does OR on every bit of two
numbers.
The result of OR is 1 if any of the two bits is 1.
Bitwise XOR (^ ) takes two numbers as operands and does XOR on every bit of
two numbers.
The result of XOR is 1 if the two bits are different.
Left shift (<<) takes two numbers, left shifts the bits of the first operand, the
11 | P a g e
second operand decides the number of places to shift.
Right shift (>>) takes two numbers, right shifts the bits of the first operand, the
second operand decides the number of places to shift.
Bitwise NOT (~ ) takes one number and inverts all bits of it
#include <iostream>
int main()
{
unsigned int a = 5, b = 9; // a = 5(00000101), b = 9(00001001)
cout<< "a&b = “ << a & b;
cout<< "a|b = " << a | b;
cout<< "a^b = " << a ^ b;
cout<< "~a = "<< a = ~a;
cout<< "b<<1 = " << b << 1;
cout<< "b>>1 = " << b >> 1;
return 0;
}
12 | P a g e
Precedence of operators
When writing complex expressions with several operands,
we may have some doubts about which operand is evaluated first and
which later
Example:-
a = 5+7 % 2
Precedence of operators
Category Operator Associativity
Postfix () [] -> . ++ - - Left to right
Unary ! ~ ++ - - (type)* & sizeof Right to left
Multiplicative * / % Left to right
Additive + - Left to right
Shift << >> Left to right
Relational < <= > >= Left to right
Equality == != Left to right
Bitwise AND & Left to right
Bitwise XOR ^ Left to right
Bitwise OR | Left to right
Logical AND && Left to right
Logical OR || Left to right
Conditional ?: Right to left
Assignment = += -= *= /= %=>>= <<= &= Right to left
^= |=
13 | P a g e
REFERENCES
MODULES OF COMPUTER PROGRAMMING II, ADDIS ABABA UNIVERSITY
WALTER SAVITCH,PROBLEM SOLVING WITH C++(6th EDITION),USA,ADDISON
WESLEY, 2006
C++ LECTURES IN ALL ONE, ADAMA SCIENCE AND TECHNOLOGY UNIVERSITY
14 | P a g e