Data Types and Variables
Data Types and Variables
Saber BHAR
Table of Contents
void
1. What is a Data Type sbyte
ushort
double
2. Classical Data Types int
Integer Types
float ulong
Floating-Point Types
byte
Type Conversion
Characters and Strings uint
3. Variables short
long
2
Data Types, Variables
and Type Conversions
How Computing Works?
Computers are machines that process data
Instructions and data are stored in the computer memory
10110
4
Variables
Variables have name, data type and value
Assignment is done by the operator "="
Variable name
Data type
int count = 5;
Variable value
5
What Is a Data Type?
A data type:
Is a domain of values of similar characteristics
Alphabetical characters: a, b, c, …
Days of week: Monday, Tuesday, … 6
Data Type Characteristics
A data type has:
Name (C# keyword or .NET type)
Default value: 0 7
Integer Types
sbyte [-128 …127]: signed 8-bit [-27 … 27-1]
byte [0 … 255]: unsigned 8-bit [0 … 28-1]
short [-32 768 … 32 767]: signed 16-bit [-215 … 215-1]
ushort [0 … 65 535]: unsigned 16-bit [0 … 216-1]
int [-2 147 483 648 … 2 147 483 647]: signed 32-bit [-2 31 … 231-1]
uint [0 … 4 294 967 295]: unsigned 32-bit [0 … 2 32-1]
long [-9 223 372 036 854 775 808 … 9 223 372 036 854 775 807]:
signed 64-bit [-263 … 263-1]
ulong [0 … 18 446 744 073 709 551 615]: unsigned 64-bit [0 … 2 64-1]
8
Centuries – Example
Depending on the unit of measure we can use different data types:
byte counter = 0; 1
for (int i = 0; i < 260; i++) 2
{ …
counter++; 255
Console.WriteLine(counter); 0
} 1
10
Problem: Centuries to Minutes
Write program to enter an integer number of centuries and
convert it to years, days, hours and minutes
Centures = 1
1 centuries = 100 years = 36524 days = 876576 hours
= 52594560 minutes
Centures = 5
5 centuries = 500 years = 182621 days = 4382904
hours = 262974240 minutes
11
Solution: Centuries to Minutes
Console.Write("Centuries = ");
int centuries = int.Parse(Console.ReadLine());
int years = centuries * 100;
int days = (int) (years * 365.2422); Tropical year has
int hours = 24 * days; (int) converts 365.2422 days
int minutes = 60 * hours; double to int
Console.WriteLine("{0} centuries = {1} years = {2}
days = {3} hours = {4} minutes", centuries, years,
days, hours, minutes);
12
Integer Literals
E.g. 12345678U, 0U
The 'l' and 'L' suffixes mean a long
E.g. 9876543L, 0L
13
What are Floating-Point Types?
d ou b le
Floating-point types:
float
Represent real numbers, e.g. 1.25, -0.38
14
Floating-Point Numbers
Floating-point types are:
float (±1.5 × 10−45 to ±3.4 × 1038) double
32-bits, precision of 7 digits
double (±5.0 × 10−324 to ±1.7 × 10308) float
64-bits, precision of 15-16 digits
The default value of floating-point types:
Is 0.0F for the float type
Sample solution:
double r = double.Parse(Console.ReadLine());
Console.WriteLine("{0:f12}", Math.PI * r * r);
18
Scientific Notation
Floating-point numbers can use scientific notation, e.g.
1e+34, 1E34, 20e-3, 1e-12, -6.02e28
double d = 10000000000000000000000000000000000.0;
Console.WriteLine(d); // 1E+34
double d2 = 20e-3;
Console.WriteLine(d2); // 0.02
double d3 = double.MaxValue;
Console.WriteLine(d3); // 1.79769313486232E+308
19
Floating-Point Division
Integral division and floating-point division are different:
2
1000000000000000000 1000000000000000005
5
2
0.00000000003 333333333333.30000000003
333333333333.3
23
Solution: Exact Sum of Real Numbers
This code works but makes mistakes sometimes:
int n = int.Parse(Console.ReadLine());
double sum = 0;
for (int i = 0; i < n; i++)
sum += double.Parse(Console.ReadLine());
Console.WriteLine(sum);
24
Type Conversion
Variables hold values of certain type
Type can be changed (converted) to another type
Implicit type conversion (lossless): variable of bigger type
(e.g. double) takes smaller value (e.g. float)
float heightInMeters = 1.74f;
double maxHeight = heightInMeters; // Implicit conversion
int n = int.Parse(Console.ReadLine());
int p = int.Parse(Console.ReadLine());
int courses = (int) Math.Ceiling((double)n / p);
Console.WriteLine(courses);
26
Boolean Type
Boolean variables (bool) hold true or false:
int a = 1;
int b = 2;
bool greaterAB = (a > b);
Console.WriteLine(greaterAB); // False
bool equalA1 = (a == 1);
Console.WriteLine(equalA1); // True
27
Problem: Special Numbers
A number is special when its sum of digits is 5, 7 or 11
For all numbers 1…n print the number and if it is special
28
Solution: Special Numbers
int n = int.Parse(Console.ReadLine());
for (int num = 1; num <= n; num++)
{
int sumOfDigits = 0;
int digits = num;
while (digits > 0)
{
sumOfDigits += digits % 10;
digits = digits / 10;
}
bool special = (sumOfDigits == 5) || …; // TODO: finish this
Console.WriteLine("{0} -> {1}", num, special);
}
29
The Character Data Type
The character data type:
Represents symbolic information
32
Solution: Triples of Latin Letters
int n = int.Parse(Console.ReadLine());
for (int i1 = 0; i1 < n; i1++)
for (int i2 = 0; i2 < n; i2++)
for (int i3 = 0; i3 < n; i3++)
{
char letter1 = (char)('a' + i1);
char letter2 = // TODO: finish this
char letter3 = // TODO: finish this
Console.WriteLine("{0}{1}{2}",
letter1, letter2, letter3);
}
33
Escaping Characters
Escaping sequences are:
Represent a special character like ', " or \n (new line)
Represent system characters (like the [TAB] character \t)
34
Character Literals – Example
35
The String Data Type
The string data type:
Represents a sequence of characters
39
Naming Variables
Variable names
Always refer to the naming conventions
of a programming language – for C# use camelCase
Preferred form: [Noun] or [Adjective] + [Noun]
41
Variable Scope and Lifetime
Scope shows from where you can access a variable
Lifetime shows how long a variable stays in memory
static void Main() Accessible in
{ the Main()
var outer = "I'm inside the Main()";
for (int i = 0; i < 10; i++) Accessible in
{ the loop
var inner = "I'm inside the loop";
}
Console.WriteLine(outer);
// Console.WriteLine(inner); // Error
}
42
Variable Span
Variable span is how long before a variable is called
Always declare a variable as late as possible (e.g. shorter span)
static void Main()
{ "outer"
var outer = "I'm inside the Main()"; variable span
for (int i = 0; i < 10; i++)
{
var inner = "I'm inside the loop";
}
Console.WriteLine(outer);
// Console.WriteLine(inner); // Error
}
43
Variable Span
Variable span is how long before a variable is called
Always declare a variable as late as possible (e.g. shorter span)
static void Main()
{
for (int i = 0; i < 10; i++)
"outer"
{ variable span
var inner = "I'm inside the loop";
}
var outer = "I'm inside the Main()";
Console.WriteLine(outer);
// Console.WriteLine(inner); // Error
}
44
Problem: Refactor Special Numbers
int kolkko = int.Parse(Console.ReadLine());
int obshto = 0; int takova = 0; bool toe = false;
for (int ch = 1; ch <= kolkko; ch++)
{
takova = ch;
while (ch > 0)
{
obshto += ch % 10;
ch = ch / 10;
}
toe = (obshto == 5) || (obshto == 7) || (obshto == 11);
Console.WriteLine($"{takova} -> {toe}");
obshto = 0; ch = takova;
}
45
Summary
Classical data types:
Integer numbers: 8-bit, 16-bit, 32-bit, 64-bit
Floating-point numbers (float, double) –
good for physics, not for money!
Decimal floating-point (decimal) – 128-bit real number,
for financial calculations / large precision
Character and String: Represents symbolic and text information