lesson 2 - variables and data type

Upload: john-son

Post on 03-Apr-2018

227 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 Lesson 2 - Variables and Data Type

    1/21

  • 7/28/2019 Lesson 2 - Variables and Data Type

    2/21

    What is a variable?

    When a user enters a new value that would be involved in

    the calculation, to manage that value, you can(temporarily) store it in the computer memory.

    NAME

    Data Type

    Since the values entered in a reserved memory area changeregularly, they are called variables.

    Creating a variable (Declaring a variable)

    Type of the value (Data Type) Variable_Name;

    Data

  • 7/28/2019 Lesson 2 - Variables and Data Type

    3/21

    Fundamental data typesWhen programming, we store the variables in our

    computer's memory, but the computer has to knowwhat kind of data we want to store in them, since it isnot going to occupy the same amount of memory tostore a simple number than to store a single letter or alarge number, and they are not going to be interpretedthe same way.

  • 7/28/2019 Lesson 2 - Variables and Data Type

    4/21

    Name Description Size* Range*

    charCharacter or smallinteger.

    1bytesigned: -128 to 127unsigned: 0 to 255

    short int(short)

    Short Integer. 2bytes signed: -32768 to 32767unsigned: 0 to 65535

    int Integer. 4bytes

    signed: -2147483648 to2147483647unsigned: 0 to4294967295

    long int(long)

    Long integer. 4bytes

    signed: -2147483648 to

    2147483647unsigned: 0 to4294967295

    boolBoolean value. It cantake one of two values:true or false.

    1byte true or false

    float Floating point number. 4bytes+/- 3.4e +/- 38 (~7digits)

    doubleDouble precisionfloating point number.

    8bytes+/- 1.7e +/- 308 (~15digits)

    long doubleLong double precisionfloating point number.

    8bytes+/- 1.7e +/- 308 (~15digits)

    wchar_t Wide character. 2 or4 bytes 1 wide character

  • 7/28/2019 Lesson 2 - Variables and Data Type

    5/21

    Declaration of variables

    In order to use a variable in C++, we must first declareit specifying which data type we want it to be. Thesyntax to declare a new variable is to write the specifierof the desired data type (like int, bool, f loat...)followed by a valid variable identifier. For example:

    int a;float mynumber;

  • 7/28/2019 Lesson 2 - Variables and Data Type

    6/21

    Example 01// operating with variables#include

    usingnamespace std;

    int main (){// declaring variables:int a, b;int result;

    unsignedshortint NumberOfSisters;signedint MyAccountBalance;short Year;}

  • 7/28/2019 Lesson 2 - Variables and Data Type

    7/21

    Scope of variables

  • 7/28/2019 Lesson 2 - Variables and Data Type

    8/21

    Initializing the variable When you have just declared a variable, it may not hold a

    significant value.

    To know the value it has, you should put an initial value into that

    memory space. Putting an initial value is referred to as initializing the variable.

    Initializing a created variable:

    Type of the value(Data Type) Variable_Name;

    Variable_Name = Value to be assign ;

    type identifier (initial_value) ;

  • 7/28/2019 Lesson 2 - Variables and Data Type

    9/21

    Example 01// initialization of variables#include usingnamespace std;int main ()

    {int a=5; // initial value = 5int b(2); // initial value = 2int c;c=4;

    int result; // initial value undetermineda = a + 3;result = a - b;cout

  • 7/28/2019 Lesson 2 - Variables and Data Type

    10/21

    Introduction to strings

    Variables that can store non-numerical values that arelonger than one single character are known as strings.

  • 7/28/2019 Lesson 2 - Variables and Data Type

    11/21

    // my first string

    #include

    #include

    usingnamespace std;int main ()

    {

    string mystring = "This is a string";

    cout

  • 7/28/2019 Lesson 2 - Variables and Data Type

    12/21

    // my first string#include

    #include

    usingnamespace std;

    int main () {

    string mystring;

    mystring = "This is the initial string content";

    cout

  • 7/28/2019 Lesson 2 - Variables and Data Type

    13/21

    Literals in C++ A literal is the source code representation of a fixed value literals are

    represented directly in your code without requiring computation. A literal is any number, text or other information that directly

    represents a value. Integer Literals

    decimal formathexadecimal formatoctal format

    The floating point Literals Character Literals Unicode escape sequence Boolean Literals

  • 7/28/2019 Lesson 2 - Variables and Data Type

    14/21

    Integer Literals 75 // decimal

    0113 // octal

    0x4b // hexadecimal 75 // int

    75u // unsigned int

    75l // long

    75ul // unsigned long

  • 7/28/2019 Lesson 2 - Variables and Data Type

    15/21

    Floating Point Numbers 3.14159 // 3.14159

    6.02e23 // 6.02 x 10^23

    1.6e-19 // 1.6 x 10^-19 3.0 // 3.0

    3.14159L // long double

    6.02e23f// float

  • 7/28/2019 Lesson 2 - Variables and Data Type

    16/21

    Character and string literals

    'z'

    'p'

    "Hello world" "How do you do?"

  • 7/28/2019 Lesson 2 - Variables and Data Type

    17/21

    Unicode escape sequence

    \u0041' Capital letter A

    '\u0030' Digit 0

    '\u0022' Double quote "

    '\u003b' Punctuation ;

    '\u0020' Space

    '\u0009' Horizontal Tab

  • 7/28/2019 Lesson 2 - Variables and Data Type

    18/21

    Boolean Literals

    bool chosen = true;

  • 7/28/2019 Lesson 2 - Variables and Data Type

    19/21

    Defined constants (#define)

    #define PI 3.14159

    #define NEWLINE '\n'

  • 7/28/2019 Lesson 2 - Variables and Data Type

    20/21

    Example// defined constants: calculate circumference

    #include usingnamespace std;#define PI 3.14159#define NEWLINE '\n'int main () {double r=5.0; // radius

    double circle;circle = 2 * PI * r;cout

  • 7/28/2019 Lesson 2 - Variables and Data Type

    21/21

    Declared constants (const)

    With the const prefix you can declare constants with aspecific type in the same way as you would do with avariable:

    constint pathwidth = 100;

    constchartabulator = '\t';