Data Types
Programming Data Types
Data Types
– Refer to the kinds of data that variable can assume, hold or take
on in a programming language and for which operations are
automatically provided.
Data Type Keyword
Boolean (true or false) bool
Character char
Integer (whole numbers) int
Floating point float
Double floating point double
Texts string
Programming Data Types
Boolean
– A boolean data type represents two states: true and false.
– it can also accept values 0 and 1; wherein 0 can represent false,
and 1 as true.
Example:
bool result = true;
correct declaration
bool result2 = false;
bool x = 123.456; wrong declaration
Programming Data Types
Integer
– integer can only hold whole numbers; numbers with decimal
points will be converted into whole numbers.
Example:
int num = 10;
– if you declared-> int x = 3.2 the program will ignore everything
that is typed after the decimal point.
Programming Data Types
Types of Integers:
– int can only contain values ranging from -2,147,483,648 to
2,147,483,647… but it has different types:
data type range
short -32768 to 32767
long -2,147,483,648 to
2,147,483,647
long long -18446744073709551616
to
18446744073709551616
Programming Data Types
Character
– A character data type represents a single character.
– it must have its value enclosed in single-quotes.
Example:
char letter = ‘a’; correct declaration
char x = 0;
char y = qwerty; wrong declaration
char z = “asdf”;
char a = ‘abcde’;
Programming Data Types
String
– A string data type represents a series of characters.
– it must have its value enclosed in double-quotes.
Example:
string name = “mark”; correct declaration
string x = ‘0’;
wrong declaration
string y = 1;
Programming Data Types
Floating point
– A floating point data type represents number with decimal point.
– has two types: float and double.
Example:
double number = 3.5;
data type range
float +/- 3.4e +/- 38 (~7 digits)
double +/- 1.7e +/- 308 (~15 digits)
Programming cin and cout
The Standard Output Stream (cout)
– The cout object is said to be "connected to" the standard
output device, which usually is the display screen.
– The cout is used in conjunction with the stream insertion
operator, which is written as << which are two less than
signs
Example:
cout<<“Hello, World!”<<endl;
Programming cin and cout
The Standard Input Stream (cin)
– The cin object is said to be “attached to" the standard
input device, which usually is the keyboard.
– The cin is used in conjunction with the stream extraction
operator, which is written as >> which are two greater
than signs
Example:
int x;
cin>>x;
Programming cin and cout sample program
Sample Program:
Output:
Programming cin and cout sample program
Sample Program:
Output:
Flowchart
Computer Science Fundamentals - Lectures
Flowchart and Algorithm
• Flowchart
are a visual outlining tool. They can be used to
represent an algorithm.
• Algorithm
The sequence of steps necessary to solve any
problem.
Symbols used in Flowcharting
Symbols used in Flowcharting
Sample Program:
Sample Flowchart:
Flowchart
#include<iostream.h>
start TERMINAL
using namespace std; SYMBOL
int main(){
no1, no 2
INPUT
int no1, no2;
int sum;
sum = no1+no2 PROCESS
cout<<“Please Type in First Integer:”;
cin>>no1;
cout<<“Please Type in Second Integer:”; no1, no2,
sum OUTPUT
cin>>no2;
sum = no1+no2;
cout<<“The sum of” <<no1 <<“and”<<no2
end TERMINAL
<<“is:”<<sum<<endl;
SYMBOL
}
Operators
Operators
Arithmetic Operators
Operators Use Description
+ op1 + op2 Adds op1 and op2
- op1 - op2 Multiplies op1 and op2
* op1 * op2 Divides op1 and op2
/ op1 / op2 Subtracts op1 from op2
% op1 % op2 Computes the remainder
of dividing op1 by op2
Sample Program
#include<iostream.h>
using namespace std;
int main(){
int num1;
int num2;
cout<<“Enter First Number:”<<endl;
cin>>num1;
cout<<“Enter Second Number:”<<endl;
cin>>num2;
cout<<“The sum is:”<<“ “<<num1+num2;
}
Output
Operators
Increment and Decrement Operators
Operator Use
Description
++op++ increments op by 1, evaluates to the
value of op
before it was incremented.
++++op increments op by 1, evaluates to
the value of op
after it was incremented.
Operators
Increment and Decrement Operators
Operator Use
Description
--op-- decrements op by 1, evaluates to
the value of op
before it was decremented.
----op decrements op by 1, evaluates to the
value of op
after it was decremented.
Sample Program
#include<iostream.h>
using namespace std;
int main(){
int a = 23;
int b = 24;
int c = 22;
cout<<"Number 1 ="<<++a<<endl;
cout<<"Number 2 ="<<b++<<endl;
cout<<"Number 2 = "<<b<<endl;
cout<<"Number 3 ="<<--c<<endl;
}
Output
Number 1 = 24
Number 2 = 24
Number 2 = 25
Number 3 = 21