Presentation is loading. Please wait.

Presentation is loading. Please wait.

សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management

Similar presentations


Presentation on theme: "សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management"— Presentation transcript:

1 សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management
Variable and data type Lectured by : SRENG VICHET Tel : Prepared by : CHEA VICHET EAV DARAPISAL CHONG KIMKEANG SRENG VICHET ចំនាយពេល ៣ម៉ោង

2 National University of Management
Objectives Identifier Variable and data type Declared Variable initialization Variable Scope of Variable Introduction to string National University of Management

3 National University of Management
Objective (con’t) string concatenate Constant variable Literal variable Symbolic Constant typedef Enumerated Constants National University of Management

4 National University of Management
Let’s think! If I want you remember number 5 then number 2 more, so your brain remember number 5 and number 2. Now I suggest you to add 1 in the first number. You ready answer 6(5+1), then take number one minus number two. The result is 6 -2 =4. first_num = 5; second_num = 2; first_num+=1; int result = first_num –second_num; National University of Management

5 National University of Management
Identifier Name of variable or function Must begin with letter. No space. Must not be matched any keywords. first_number = 5; // valid Second number = 2 // invalid identifer Case sensitive Result = 7; result = 7; National University of Management

6 National University of Management
Variable & Data Type Variable: Identifier that is used to store any different data type. mySkill =“information technology”; myAge = 28; myHeight =1.70; Data Type It is the type of data that variable will be stored: National University of Management

7 National University of Management
Fundamental Data Type Character variables hold a single byte representing 1 of the 256 characters and symbols in the standard ASCII character set. for (int i=1; i<128; i++) { cout << char(i)<<endl; } cout << "The size Max of short int is :"<<SHRT_MAX<<endl; cout << "The size Min of short int is :"<<SHRT_MIN<<endl; cout << "The size Max of int is :"<<INT_MAX<<endl; cout << "The size Min of int is :"<<INT_MIN<<endl; cout << "The size Max of long int is :"<<LONG_MAX<<endl; cout << "The size Min of long int is :"<<LONG_MIN<<endl; cout << "The size Max of Float is :"<<FLT_MAX<<endl; cout << "The size Min of Float is :"<<FLT_MIN<<endl; cout << "The size Max of Double is :"<<DBL_MAX<<endl; cout << "The size Min of Double is :"<<DBL_MIN<<endl; cout << "The size Max of Long Double is :"<<LDBL_MAX<<endl; cout << "The size Min of Long Double is :"<<LDBL_MIN<<endl; National University of Management

8 National University of Management
Declared Variable Syntax: <data type> <identifier>; int numberOfEmployee; float salary; char letter; string name; Multiple variable with the same type: int a,b,c; float salary, income; ASCII =255 National University of Management

9 Initialization of variable
When declaring a regular variables, its value is by default undermined. In order to initialize the variable, we have two ways to to do this in C++: Syntax: type identifier = initial_value; //c-like int a = 0; type identifier(initial_value); // constructor initialization int b(0); National University of Management

10 National University of Management
singed and unsigned char, short, long and int can be signed and unsigned too. singed : your variables are stored the positive(+) and negative(-) values; unsigned : your variable are stored the positive(+) and value of 0; unsigned short int myNumberOfSisiters; signed int myAccountBalance; National University of Management

11 National University of Management
Example #include <iostream> int main() { unsigned short ageP=30; int ageN=-30; std::cout << ageP<<std::endl; std::cout << ageN<<std::endl; std::cin.get(); return 0; } unsigned short age =-10; // logical error National University of Management

12 National University of Management
Scope of Variable local variable may be accessed within the block of code in which a variable may be accessed. It can be used in function or a block of code({}). global variable may be accessed within every scope of the program. It can be used everywhere even though in function. National University of Management

13 National University of Management
#include <iostream> using namespace std; int a; // global variable int b; // global variable int main () { int result; // local variable a = 5; // you use b = 2; // global result = a + b ; // variable here cout << "a + b = " ; cout << result ; cin.get(); return 0; } National University of Management

14 National University of Management
#include <iostream> using namespace std; int a, b; //global variable int main() { a = 5; b = 10; cout << "a = " << a << endl; cout << "b = " << b << endl; int a, b; //local variable to its block a = 2; b = 3; } cin.get(); return 0; a = 5 b = 10 a = 2 b = 3 National University of Management

15 Introduction to string
Variables that can store non-numberical values that are longer than one single character are known as string. In order to used string type we need to include a additional header file in our source code: <string>. #include <string> ….. string num=“National University of Management”; National University of Management

16 National University of Management
#include <iostream> #include <string> using namespace std;   int main () { string mystring = “This is a string.” cout << mystring << endl; string yourstring(“This is another string.”); cout << yourstring ; return 0; } This is a string. This is another string. National University of Management

17 National University of Management
string concatenate string firstName = "Sabay"; string middleName = "Sok"; string lastName = "Sen"; string fullName = lastName + " "+ middleName + " "+ firstName; cout << fullName; cout << endl; Concatenate : ការតភ្ជាប់អក្សរ a series of events, ideas or things that are not connected ចប់ National University of Management

18 National University of Management
Constant Variable A constant, like a variable, is a memory location where a value can be stored. Unlike variables, constants never change in value(read-only). C++ has two type of constants: Literal constants. Symbolic constants. National University of Management

19 National University of Management
Literal Constant Is a value typed directly into your program wherever it is needed. Integer 75 // int 75u // unsigned int 75l // long 75ul // unsigned long Literal Constant ជាតួអក្សរ លេខ ឬព្យញ្ជនៈ ដែលមានតំលៃ និងប្រភេទរបស់ ខ្លួនវាជាក់លាក់ long width = 5; This statement assigns the integer variable width the value 5. The 5 in the statement is a literal constant. You can’t assign a value to 5, and its value can’t be changed. The values true and false, which are stored in bool variables, also are literal constants. National University of Management

20 Literal Constant (con’t)
Floating Point // 6.02e23 // 6.02 x 1023 L // long double 6.02e23f // float Character and String 'z' // 'z' character “Hello World” // “Hello World” String Boolean Boolean has only two value : true (1) និង false (0) for (int i=1; i<128; i++) { cout << char(i)<<endl; } National University of Management

21 National University of Management
Symbolic Constant Is a constant represented by a name, just like a variable. The const keyword precedes the type, name, and initialization. const int bouns = 300; const float pi = ; National University of Management

22 typedef (synonym of type definition)
typedef int integer; //integer is synonym int typedef double salary; //salary is synonym double typedef salary mysalary; //incorrect Why we used typedef: In order to hide data type. Easy to know, understand and use data type. typedef int integer; typedef double salary; integer myAge=13; salary mySalary=120; cout << "Your age is :"<<myAge<<endl; cout << "Your salary is :"<<mySalary<<endl; National University of Management

23 National University of Management
Enumerated Constants It create a set of constants with a single statement. They are defined with the keyword enum followed by a series of comma-separated names surrounded by braces: enum COLOR {RED, BLUE,GREEN,}; The values of enumerated constants begin with 0 for the first in the set and count upwards by 1. National University of Management

24 National University of Management
Example int Mon=1; int Tue=2; int Wed=3; int Thu=4; int Fri=5; int Sat=6; int Sun=7; enum DAYOFWEEK { Mon=1, Tue, Wed, Thu, Fri, Sat, Sun, }; DAYOFWEEK dayOfWeek= Sun; cout << dayOfWeek; //7 enum DAYOFWEEK { monday=1, tuesday, wenesday, thursday, friday, saturday, sunday }; DAYOFWEEK today=sunday; cout << "Today is :"<<today<<endl;//7 National University of Management

25 National University of Management
Quiz Please list all data types that you know, at least 5 data types. What is distinguish between singed and unsigned? What is the difference between an integer variable and a floating variable. Which of the following variable names good, bad, invalid? Age !Ex R98 _Invalid ----- Meeting Notes (9/27/12 09:46) ----- 13 ch02 data type scope variable 17 using variable constant variable 27 ch03 operator arithmetic operator National University of Management

26 National University of Management
Thank You! National University of Management


Download ppt "សាកលវិទ្យាល័យជាតិគ្រប់គ្រង National University of Management"

Similar presentations


Ads by Google