meljun cortes c++_variables_keywords_data_types

Upload: meljun-cortes-mbampa

Post on 02-Jun-2018

214 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    1/32

    C++

    Variables, Identifiers,

    Keywords, Data Types

    MELJUN CORTES MBA MPA BSCS

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    2/32

    MELJUN CORTES, MBA,MPA,BSCS

    VARIABLES Is a location in the computers memory where

    a value can be stored for use by a program. Is a symbol that represents a storage location

    in the computers memory. A place to store a

    piece of information. It must be declared with a name and a data

    type before they can be used in a program.

    Example:

    integer1, sum, interger2, etc.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    3/32

    MELJUN CORTES, MBA,MPA,BSCS

    NAMING CONVENTIONS

    The names of variables in C++ are typically referred toas Identif iers. The following are rules for creating

    identifiers. Names are made up of letters and digits.

    The first character must be a letter or an underscore(_). However, an underscore is not recommended to

    be used as the first character in a name since someof C++s internal identifiers begin with underscores.

    C++ is case-sensitive, i.e., the lower-case letter a isnot the same as the uppercase letter A.

    At least the first 31 characters of a name aresignificant.

    There can be no spaces in identifiers. Ex. last_name

    Keyword must NOT be used as identifiers because

    they are part of C++ language.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    4/32

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    5/32

    MELJUN CORTES, MBA,MPA,BSCS

    KEYWORDS These names are reserved and cannot be used for other

    purposessuch as in naming user-defined variable names.The MS Visual C++ language has a total of 59 keywordsshown below.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    6/32

    MELJUN CORTES, MBA,MPA,BSCS

    ESCAPE CODES

    Are special characters that cannot be expressedotherwise in the source code of the program (all ofthem are preceded by an inverted slash (\))

    \n newline \f page feed

    \r carriage return \a alert (beep)

    \t tabulation \ single quote ()

    \v vertical tab \ double quote ()\b backspace \? question

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    7/32MELJUN CORTES, MBA,MPA,BSCS

    CONSTANTS

    Constants are entities whose value does notchange.

    A constant can either be numeric constant ora literal constant. In C/C++, a numeric

    constant can be an integer or floating pointnumber, while a literal constant can be asingle character or a string, i.e. a constant

    with more than one character. A singlecharacter is written such that it is enclosed ina pair of single quotes. A string is writtenenclosed in a pair of double quotes.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    8/32MELJUN CORTES, MBA,MPA,BSCS

    The Table 2.3 shows some examples of numeric and literalconstants. It is important to note that even if 'A' and "A"contains the same value, they are of different types. Theformer is a character while the latter is a string.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    9/32MELJUN CORTES, MBA,MPA,BSCS

    Basic Data Types in C++

    A data type specifies the kind of valuescan be assumed by a variable of that type,the range of values that can be assumed

    by a variable of that type, and the amountof memory (in bytes) needed by a variableto store a value of that type.

    There are five basic data types in C++,namely: char, int, float, doubleand bool

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    10/32MELJUN CORTES, MBA,MPA,BSCS

    charthe character data type. The chardata type is used to

    represent/store/manipulate character datavalues. The range of values that can beassumed by a char value is from 0 to 255.

    Example: a, B, c, name, Course, 7, *, $, etc.

    intthe integer data type. The int data typeis used to represent/store/manipulate signedwhole numbers. The range of values that

    can be assumed by an int value is from-2147483648 to 2147483647.

    Example: 5, -10, 15, etc.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    11/32MELJUN CORTES, MBA,MPA,BSCS

    floatthe single precision floating point datatype. The float data type is used to store single

    precision signed real numbers.Example: 6.20, 12.50, -21.489, 0.583, etc.

    doublethe double precision floating point

    data type. The double data type is used to storedouble precision signed real numbers.

    Example: 5.10, 10.50, -15.356, 0.254, etc.

    boolthe Boolean data type. The bool datatype is used to represent Boolean values trueorfalse.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    12/32MELJUN CORTES, MBA,MPA,BSCS

    The amount of memory spacerequired to store an int,a floatand doubleis platform-dependent (depends onthe machine and the software). For compilers such asthe Microsoft Visual C++ compiler, a charand boolrequires 1 byte of memory each, an in tand f loatrequires 4 bytes of memory each, while a doub lerequires 8 bytes of memory.

    The sizeofis a keyword in C++. It is an operator that yields the size

    in number of bytes of the specified data type.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    13/32MELJUN CORTES, MBA,MPA,BSCS

    Declaration of Variables

    In order use a variable in C++, we must firstdeclare it specifying which of the data typeswe want it to be. The syntax to declare a

    new variable is to write the data type specifierthat we want followed by a valid variableidentifier.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    14/32MELJUN CORTES, MBA,MPA,BSCS

    ;

    A semicolon signifies the end of a declaration. A missing

    semicolon will cause the compiler to generate a syntaxerror. Variables should be named following the C++naming conventions. For example:

    It is possible to declare several variables

    of the same type on the same line. Insuch a case, a comma should be insertedbetween two variables. A missing commawill generate a syntax error. For example:charch1, ch2;

    boolb1, b2;intx, y, z;

    float degree_celsius,

    degree_fahrenheit, degree_kelvin;

    doublenumerator, denominator;

    char ch;

    boolb;inti;

    floatf;

    doubled;

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    15/32

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    16/32

    MELJUN CORTES, MBA,MPA,BSCS

    Additionally to this way of initializing variables

    (known as c-like), C++ has added a new wayto initialize a variable: by enclosing the initialvalue between parenthesis ():

    type ident i f ier(in it ial_value) ;For example:

    int a (0);

    Both ways are valid and equivalent in C++.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    17/32

    MELJUN CORTES, MBA,MPA,BSCS

    Scope of Variables

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    18/32

    MELJUN CORTES, MBA,MPA,BSCS

    Scope of Variables

    Global variablescan be referred toanywhere in the code, within any function,whenever it is after its declaration.

    Local variablesis limited to the code levelin which they are declared. If they aredeclared at the beginning of a function (like inmain) their scope is the whole mainfunction.

    In the example above, this means that ifanother function existed in addition to main(),the local variables declared in maincould notbe used in the other function and vice versa.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    19/32

    MELJUN CORTES, MBA,MPA,BSCS

    In C++, the scope of a local variable is given

    by the block in which it is declared (a block isa group of instructions grouped togetherwithin curly brackets {}signs). If it is declaredwithin a function it will be a variable with

    function scope, if it is declared in a loop itsscope will be only the loop, etc...

    In addition to localand globalscopes there

    exists external scope, that causes a variableto be visible not only in the same source filebut in all other files that will be linked together

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    20/32

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    21/32

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    22/32

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    23/32

    MELJUN CORTES, MBA,MPA,BSCS

    Input / Output Functions

    conso le

    it is the basic interface of computers,normally it is the set composed of thekeyboard and the screen. The keyboard is

    generally the standard inputdevice and thescreen the standard outputdevice.

    In the iostreamC++ library, standard input

    and outputoperations for a program aresupported by two data streams: cinfor inputand coutfor output.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    24/32

    MELJUN CORTES, MBA,MPA,BSCS

    Input / Output Functions

    cout(the standard output stream )

    it is normally directed to the screen.

    it is used to output information to thescreen.

    cin(the standard input stream)

    it is normally assigned to the keyboard.

    it is used to input information from thekeyboard.

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    25/32

    MELJUN CORTES, MBA,MPA,BSCS

    The cou tstreamis used in conjunction with theoverloaded operator

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    26/32

    MELJUN CORTES, MBA,MPA,BSCS

    The

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    27/32

    MELJUN CORTES, MBA,MPA,BSCS

    For examp le, these two sentences are very different:

    cout

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    28/32

    MELJUN CORTES, MBA,MPA,BSCS

    cout

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    29/32

    MELJUN CORTES, MBA,MPA,BSCS

    Handling the standard input in C++ is done byapplying the overloaded operator of extract ion(>>) on the cinstream. This must be followedby the variable that will store the data that is

    going to be read.For example:

    int age;

    cin >> age;

    declares the variable age as an int and thenwaits for an input from cin (keyborad) in orderto store it in this integer variable.

    Input / Output Functions

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    30/32

    MELJUN CORTES, MBA,MPA,BSCS

    Always consider the typeof the variable thatare using as a container with cinextraction.If the variable is an integer then get an

    integer, if variable is a character than get acharacter.

    Input / Output Functions

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    31/32

    MELJUN CORTES, MBA,MPA,BSCS

  • 8/10/2019 MELJUN CORTES C++_Variables_Keywords_Data_Types

    32/32

    MELJUN CORTES MBA MPA BSCS