Mizan-Tepi University Tepi Campus: Individual Assignment
Mizan-Tepi University Tepi Campus: Individual Assignment
TEPI CAMPUS
College of Computing and Informatics
Individual Assignment
Name: Abduwasi Ahmed
ID No: 0116/14
Submission date:
16/8/2014
.
Table of Contents
INTRODUCTION......................................................................................................................................2
Variables.....................................................................................................................................................3
Creating More Than One Variable at a Time......................................................................................3
Variable initialization...........................................................................................................................4
Basic Data Types………………………………………………………………………………………………………………………………4
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.
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
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;
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 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 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
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++)
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
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
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