FP 201 Unit 2 - Part 2


Published on

1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • wchar_t: for special character eg: symbol/international language wchar_t myChar1 = L' Ω'; wchar_t myString1[] = L"♠♣♥♦";
  • Signed types can represent both positive and negative values unsigned types can only represent positive values (and zero). unsigned short int NumberOfSisters; signed int MyAccountBalance;
  • FP 201 Unit 2 - Part 2

    1. 1. Unit 2 :Data types
    2. 2.  Objectives C++ data types, constant and variable C++ keywords Input Output : cin, cout Hands On!
    3. 3.  At the end of this presentation, students should be able to • Identify identifier, variable, constant, data types and keywords use in C++ Programming • Write C++ program
    4. 4.  A data type determines the type of the data that will be stored, usually, in the computer memory (RAM). Type statements in C++ are used to allow the compiler to: • reserve blocks of memory to store information • give the reserved blocks of memory a symbolic name so that the data contained in this block of memory can be manipulated by referring to this name in future C++ statements.
    5. 5.  C++ provides six fundamental data types: char int float double bool wchar_t However, qualifiers that can be put in front of the numerical data types to form their derivatives: short long signed unsigned Can you tell the difference between those qualifiers?
    6. 6. Name Description Size* Range* Character or small signed: -128 to 127char 1byte integer. unsigned: 0 to 255short int signed: -32768 to 32767 Short Integer. 2bytes(short) unsigned: 0 to 65535 signed: -2147483648 to 2147483647int Integer. 4bytes unsigned: 0 to 4294967295 signed: -2147483648 tolong int 2147483647 Long integer. 4bytes(long) unsigned: 0 to 4294967295 Boolean value. It canbool take one of two values: 1byte true or false true or false.float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits) Double precision +/- 1.7e +/- 308 (~15double 8bytes floating point number. digits) Long double precision +/- 1.7e +/- 308 (~15long double 8bytes floating point number. digits)wchar_t Wide character. 2 or 4 bytes 1 wide characterFundamental data types and their size and ranges in the memory
    7. 7.  What is the suitable data type for the following? • number of student in your class • your matrix number • assignment marks for this subject • the distance to the moon (the distance to the moon is over 200,000 miles) • last months checking account balance • a counter used to count the number of lines in a text file • Malaysian’s plate number • number of people living in Malaysia • the temperature used in a chemistry formula
    8. 8. Unit 2 :Identifier, variable, constant, keywords
    9. 9.  KEYWORDS  reserved words that have standard, predefined meanings and must be used only for their intended purpose.  it cannot be used as an identifier.  keywords in C++ are as follow: asm, auto, bool, break, case, catch, char, class, const, const_cast, continue, default, delete, do, double, dynamic_cast, else, enum, explicit, export, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while
    10. 10.  refer to memory locations which can hold values. it is used for naming variables, constants, functions, structures and classes. Rules: • a valid identifier must begin with a letter or underscore (_) • can consist only of letters, digits, and underscores. • no blank space is allowed & the size must less than 31 characters • an identifier should not match with any C++ reserved keywords.
    11. 11.  The C++ language is a "case sensitive" language. That means that an identifier written in capital letters is not equivalent to another one with the same name but written in small letters. Thus, for example, the NUMBER variable is not the same as the result variable or the number variable.
    12. 12. Valid Invalidx “x”sumx2 2sumxhourly_rate hourly-ratename name@GROSS_PAY GROSS PAY
    13. 13.  Valid or not? • mass • c++ • Force • 2ndBit • pos12 • speed_of_light • yağmur • SpeedOfLight • float • isPrime • speed of light
    14. 14.  variable • a valid identifier whose value can change during the course of execution of a program general form of the declarations: data-type variable_name; example: int mass; double x, speed, dragForce; when a variable is declared, you can initialize it in two alternative but equivalent ways int mass = 22; or int mass(22);
    15. 15.  Declaration of string variable •  include an additional header file <string> general example: string name=“Mohamad”;
    16. 16. // Declaration of variables#include <iostream>using namespace std;int main () { short x = 22, y = 11, z; z = x - y; cout << "z = " << z << endl; int p = 3; int q = x * y * z – 2 * p; cout << "q = " << q << endl; return 0; }
    17. 17.  variable can have either local or global scope scope (visibility) of local variables is limited to the block enclosed in braces ({ }) where they are declared global variables are declared outside of all blocks and their scope are the entire program, i.e. the main body of the source code.
    18. 18.  Write appropriate declarations for the following: • Integer variable: index • Unsigned integer variable: customer_no • Double precision variables: gross, tax, net • Character variables: first, last
    19. 19.  values that do not change during program execution. they can be any type of integer, character or floating-point. done by using const keyword as follows: const float PI = 3.1415926, TWOPI = 2.0*PI; const int EOF = -1; const num3 = 15; by convension; • variable names use lowercase letters • constants use uppercase only
    20. 20.  Correct the following errors • long Float x; • int code = three, • const int array_size; Declare the following variable: Name Type Initial value marks double None grade char A price float 10.0 num_1 int 5 msg string Hello World result bool true
    21. 21. #include <iostream>using namespace std;int main() { const num2 = 10; const num3 = 15; cout << “Number =” << num2 + num3; return 0; }
    22. 22. Code Description Code Description a Audible Bell t Horizontal tab b Backspace Backslash character f Formfeed ’ Single quote character n Newline ” Double quote character r Carriage return 0 Null ASCII 0
    23. 23.  http://www.cplusplus.com/doc/tutorial/
    24. 24. SUMMARYIn this presentation, you learnt the following: Data type defines the type of value to be stored in the memory. Variable is the name given to the memory location where the value is stored. A constant is a value that does not change throughout program execution.
    25. 25. EXERCISE1. List the data types of C++.2. What is the difference between variables and constants?