1 variables - mathematics

Upload: rodie-rose-bonavente

Post on 04-Jun-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/13/2019 1 Variables - Mathematics

    1/42

    VARIABLES, DATA TYPESAND OPERATORS

  • 8/13/2019 1 Variables - Mathematics

    2/42

    Sample Problem

    Create a program that accepts two numbers

    and then display the sum. Design your own

    interface. Use sampleVaras the project

    name.

  • 8/13/2019 1 Variables - Mathematics

    3/42

    Basic Data Types

    Integer

    Negative to positive whole number

    -32,768 to 32,767 4 bytes

    Float / Double Float

    Real numeric value

    -1038to 1038 / -1076to 1076

    4 bytes/ 8 bytes

    Boolean

    Holds value for true or false

    George Bool

    Character (char) Single charactera letter, digit or a punctuation character

    256 different characters

    1 byteString

    Combination of characters, can be digits, letters or special symbols

  • 8/13/2019 1 Variables - Mathematics

    4/42

    Data Types

    char

    - the character data type.

    - The char data type is used torepresent/store/manipulate character datavalues.

    - A char data type value requires one byte ofmemory space.

    - The range of values that can be assumed by a

    char value is from 0 to 255.- The number-to-character coding that is used is

    ASCII.

  • 8/13/2019 1 Variables - Mathematics

    5/42

    Data Types

    integer

    - the integer data type.

    - The int data type is used to

    represent/store/manipulate signed whole

    numeric values.

  • 8/13/2019 1 Variables - Mathematics

    6/42

    Data Types

    float

    - the single precision floating point data type.

    - The float data type is used to store single

    precision signed real numbers (i.e., those

    numbers with fractional components).

  • 8/13/2019 1 Variables - Mathematics

    7/42

    Data Types

    double

    - the double precision floating point data type.

    - The double data type is used to store double

    precision signed real numbers.

  • 8/13/2019 1 Variables - Mathematics

    8/42

    Notes on Data Types

    Mark G. De Vera

    - The amount of memory space required to store

    an int, a float and double is platform-

    dependent. (depends on the machine and the

    software).- Note that a char data is actually numeric (from 0

    to 255), and is treated as a subset of int values.

    Any operation on integer values can also be

    performed on characters.

  • 8/13/2019 1 Variables - Mathematics

    9/42

    Variable Declaration

    Mark G. De Vera

    A variable declaration is an actionby which

    a variable is introducedto a

    program/function.

    All variables in a program must be declared. Ifyou forgot to do so, the compiler will report a

    syntax error.

  • 8/13/2019 1 Variables - Mathematics

    10/42

    Variable Declaration

    Purpose

    to associate type

    for storage

  • 8/13/2019 1 Variables - Mathematics

    11/42

    Rules in Naming Variables

    Mark G. De Vera

    Should always start with a letter

    Should be descriptive in nature

    Should not be a reserved word in VB

    Should not contain spaces and especial

    characters except underscore (_)

    Can be a combination of letters, digits and

    underscore

  • 8/13/2019 1 Variables - Mathematics

    12/42

    Declaring A Variable

    Syntax:

    Dim as

    Dim as =

    Examples

    Dim lastname as String

    Dim num as IntegerDim price, total_amt as Double

    Dim x as Integer = 5

  • 8/13/2019 1 Variables - Mathematics

    13/42

    Can I use any name for the var iables?

    Mark G. De Vera

    - YES, as long as you follow the naming

    conventions, and do not use the reserved

    words in VB.

    - It is recommended, however, as a goodprogramming practice for you to use a name

    that is descriptive or suggestive.

  • 8/13/2019 1 Variables - Mathematics

    14/42

    What are operators?

    Mark G. De Vera

    Operators are symbols representingoperations that can be performed on constantsand variables.

    Different sets of OPERATORS:Assignment

    Arithmetic

    Logical

    Relational

  • 8/13/2019 1 Variables - Mathematics

    15/42

    Assignment Operators

    Mark G. De Vera

    How do you per form an assignment operat ion in

    VB?

    Symbol used is an equal sign (=)

    It is used to assign/store values to a variable.

    Dim a as Integer

    a=25; /*25 is assigned to a*/choice=a; /*a is assigned to choice*/

  • 8/13/2019 1 Variables - Mathematics

    16/42

    Arithmetic Operations

    Mark G. De Vera

    Basic Ar i thmet ic Operat ions

    + Addition Yields the sum

    - Subtraction Yields the difference

    * Multiplication Yields the product

    / Division Yields the quotient

    mod Modulus Division Yields the remainder

  • 8/13/2019 1 Variables - Mathematics

    17/42

    Relational Operations

    Mark G. De Vera

    == Equal to

    != Not equal

    > Greater than

    < Less than

    >= Greater than or equal

  • 8/13/2019 1 Variables - Mathematics

    18/42

    Logical Operations

    Mark G. De Vera

    ! Logical NOT

    && Logical AND

    || Logical OR

    !0 1

    !1 0

    0&&0 0

    0&&1 01&&0 0

    1&&1 1

    0||0 0

    0||1 11||0 1

    1||1 1

  • 8/13/2019 1 Variables - Mathematics

    19/42

    Control Structures

    Mark G. De Vera

    specify the sequence of execution of a group

    of statements.

    Three different type of control structures

    Sequential

    Conditional

    Loop

  • 8/13/2019 1 Variables - Mathematics

    20/42

    Sequential Control Structure

    Mark G. De Vera

    A sequential control structure is organized such

    that statements are executed in sequence, i.e.,

    one after the other in the order of their

    appearance in the source code.Example:

    a = 1; /* first statement */

    b = 2; /* second statement */c = a + b; /* third statement */

  • 8/13/2019 1 Variables - Mathematics

    21/42

    Conditional Control Structure

    Mark G. De Vera

    The conditional control structure allows the

    program to make choices depending on a

    condition.

    Two types of conditional structures if statement (including if-else and nested if)

    switch case statement

  • 8/13/2019 1 Variables - Mathematics

    22/42

    If Statement

    Mark G. De Vera

    if ()

    The value of is first evaluated, if it

    is non-zero, then the condition is specified as

    true. If it is evaluated as zero, then the condition

    is specified as false. If the condition is true, thenthe is executed.

  • 8/13/2019 1 Variables - Mathematics

    23/42

    If Statement Example

    Mark G. De Vera

    void main(void)

    {

    int n;

    printf(Input an integer value n: );

    scanf(%d, &n);

    if (n >= 0)

    printf(n = %d is POSITIVE\n, n);

    }

  • 8/13/2019 1 Variables - Mathematics

    24/42

    If - Else Statement

    Mark G. De Vera

    if ()

    [else ]

    The value of is first evaluated, if it is non-zero, then the condition is specified as 1 (for true)and is executed. Otherwise if it isevaluated as zero, then the condition is specified as 0

    (for false), and the else part, i.e., isexecuted.

  • 8/13/2019 1 Variables - Mathematics

    25/42

    If-Else Example

    Mark G. De Vera

    #include

    void main(void)

    {

    int n;

    printf(Input an integer value n: );

    scanf(%d, &n);

    if (n >= 0)

    printf(n = %d is POSITIVE\n, n);else

    printf(n = %d is NEGATIVE\n, n);

    }

  • 8/13/2019 1 Variables - Mathematics

    26/42

    Nested If Example

    Mark G. De Vera

    if (day >= 1 && day = 600 && start_time

  • 8/13/2019 1 Variables - Mathematics

    27/42

    Switch-Case Statement

    Mark G. De Vera

    #include

    using namespace std;

    int main(void)

    {

    int day;

    coutday;switch(day)

    {

    case 1 : cout

  • 8/13/2019 1 Variables - Mathematics

    28/42

    Loop Control Structure

    Mark G. De Vera

    Output

    1

    23

    4

    5

    #include

    void main(void)

    {

    printf(%d/n, 1);

    printf(%d/n, 2);

    printf(%d/n, 3);

    printf(%d/n, 4);

    printf(%d/n, 5);

    }

  • 8/13/2019 1 Variables - Mathematics

    29/42

    Loop Control Structure

    Mark G. De Vera

    Is the previously presented source code

    acceptable for you in printing the output?

  • 8/13/2019 1 Variables - Mathematics

    30/42

    Loop Control Structure

    Mark G. De Vera

    Three Loop Control Structures

    While loop

    For loop

    Do while loop

  • 8/13/2019 1 Variables - Mathematics

    31/42

    While Loop

    Mark G. De Vera

    while ()

    #include

    void main(void)

    {

    int i;

    i = 1; /* initialization */

    while (i

  • 8/13/2019 1 Variables - Mathematics

    32/42

    For Loop

    Mark G. De Vera

    for ([initialization]; [condition]; [change of state])

    #include void main(void)

    {

    int i;

    for (i = 1; i

  • 8/13/2019 1 Variables - Mathematics

    33/42

    Do While Loop

    Mark G. De Vera

    do

    while ()

    #include void main(void)

    {

    int i;

    i = 0; /* initialization */

    do /* body of the loop */

    {

    printf(%d\n, i);

    i++; /* change of state */

    } while (i

  • 8/13/2019 1 Variables - Mathematics

    34/42

    Functions

    Mark G. De Vera

    a program by itselfit may have input(s),

    output(s) and will perform some kind of

    processing steps.

    Two types of function

    Pre-defined

    User-defined

  • 8/13/2019 1 Variables - Mathematics

    35/42

    Pre-defined Functions

    Mark G. De Vera

    those functions that have been written by for

    us (by some other programmers);

    Example

    printf(), scanf(), exit(), strcmp()

    library functions

  • 8/13/2019 1 Variables - Mathematics

    36/42

    User-defined Functions

    Mark G. De Vera

    those function that we are going to

    write/implement by ourselves;

    Example

    implementation of the main() function

  • 8/13/2019 1 Variables - Mathematics

    37/42

    Defining a Function in C

    Mark G. De Vera

    ()

    {

    []

    []

    []

    }

  • 8/13/2019 1 Variables - Mathematics

    38/42

    Defining a Function in C

    Mark G. De Vera

    Example:

    #include

    void main(void){

    int x;

    x = 1;

    printf(%d\n, n);}

    void is the of the value

    returned by the functionvoid

    denotes that fact that the function

    does not return anything

    main is the

    void is the void

    denotes the fact that it does not have

    any parameter

    int x is the and x = 1;

    printf(%d\n, n); are the

    []

  • 8/13/2019 1 Variables - Mathematics

    39/42

    Example w/ two functions

    Mark G. De Vera

    #include

    void hello(void)

    {

    printf(Hello\n);}

    void main(void)

    {

    hello(); /* call (invoke) the function*/

    }

  • 8/13/2019 1 Variables - Mathematics

    40/42

    Examples

    Mark G. De Vera

    void main(void)

    {

    int i;

    for (i = 0; i < 500; i++)

    hello();

    /* call inside the body of the loop*/

    }

  • 8/13/2019 1 Variables - Mathematics

    41/42

  • 8/13/2019 1 Variables - Mathematics

    42/42

    Function with Data Type#include

    int Sum(int x, int y)

    {

    int z;

    z = x + y;

    return z; /*dont forget to return a value*/

    }void main(void)

    {

    int a, b, c;

    printf(Sum = %d\n, Sum(5, 10)); /* use constants*/

    c = Sum(100, 300);/*store the return value into a variable*/

    printf(c = %d\n, c);

    a = 25;

    b = 75;

    printf(Sum = %d\n, Sum(a, b)); /* use variables*/