Data Type 2
Data Type 2
DATA TYPES
Data types are sets (ranges) of values that have similar characteristics.
Characteristics : Data types are characterized by: -
- Name - for example, int x;
- Size - (how much memory they use) – for example, 4 bytes;
- Default value - for example 0.
Type Represents Range Default
(2)
Char let1 = ‘ A ‘ , let2= ‘ l ‘, let3 = ‘ i ‘ ;
Console.WriteLine(“The name is : ” + let1 + let2 + let3);
(3)
String str1 = “Azhen”, str2=“Ahmed”;
Console.WriteLine( str1 + “ “ + str2 );
3
Write: Is a print command print data in a same line.
WriteLine: Is a print command print data and take the pointer to the new
line.
ReadLine: Is an input command used to input data.
Ex4:
Console.Write("Write your name : ");
String a = Console.ReadLine();
Console.WriteLine("The Input is : " + a );
-----------------------------------------------------------------------------
• The example above is to read, input string type and print string type.
• All the input data inside the “ ReadLine (); ” are string type.
• We should convert the data type if we want to read or input a number
type.
4
Convert Among the data types
EX5:
Console.Write("Enter the first number : ");
String strnumber1 = Console.ReadLine();
Console.Write("Enter the Second number : ");
String strnumber2 = Console.ReadLine();
int num1 = int.Parse(strnumber1);
int num2 = int.Parse(strnumber2);
int r= num1 + num2;
Console.WriteLine("The sum is : " + r);
==============================================
Int.Parse (); used when we want to convert string to the integer.
The Console is a window of the operating system through which users can
interact with system programs of the operating system or with other console
applications.
5
EX6:
// Convert from integer to string.
int number1 = 97;
int number2 = 3;
string r;
r = Convert.ToString(number1) + number2;
Console.WriteLine(r);
==============================
The result above will be 973 because it’s a string type not integer type.
Convert.ToSrting (); used when we want to convert to string type.
==============================
6
Operators: Every programming language uses operators, through which we
can perform different actions on the data.
7
Logical Operators – Example
bool T = true;
bool F = false;
Console.WriteLine(T && F); // False
Console.WriteLine(T || F); // True
Console.WriteLine(!F); // True
Console.WriteLine(F || true); // True
8
Comparison Operators: