MELJUN CORTES C++_Chap2

Embed Size (px)

Citation preview

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

    1/55

    Introducing C

    s Program

    Control Statements

    C Programming Language

    Chap. 2

    MELJUN CORTES MBA MPA BSCS

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

    2/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 2

    Objectives

    Cs most important program controlstatements if, for

    Determine your programs flow of execution.

    Form the backbone of your programs.

    Well learn

    Blocks of code

    The relational and logical operatorsMore printf()

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

    3/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 3

    Ex. 1

    Read two numbers to A and B. Increase 1 number to A and decrease 1 number to B.

    START

    A = A + 1

    B = B - 1

    END

    A, B

    READ A, B

    #include

    void main() {

    int a, b;

    printf(Input two numbers : );

    scanf(%d%d, &a, &b);

    }

    a = a + 1;b = b - 1;

    printf(As value : %d\n, a);

    printf(Bs value : %d\n, b);

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

    4/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 4

    Ex. 2

    Read two numbers to A and B.Swap A for B.

    A B

    A B

    temp

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

    5/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 5

    1. Become familiar with the if

    If statements Cs selection statements

    Conditional statements

    Logical data

    true

    false

    C doesnt have boolean datatype.

    Use int, char datatype for logical data

    if(expression) statement ;

    Execute when expression is true

    The result is true or false

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

    6/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 6

    if (10>9) printf(true);

    1. Become familiar with the if

    Relational operator > : greater than

    expression

    statement

    true

    false

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

    7/55MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 7

    if (9 < 5) printf(this will not print);

    1. Become familiar with the if

    Relational operator < : less than

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

    8/55MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 8

    1. Become familiar with the if

    Example 1

    #include

    int main(void) {

    int answer;

    printf(What is 10 + 14 ? );

    scanf(%d, &answer);

    if(answer == 10+14)printf(Right!);

    return 0;

    }

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

    9/55MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 9

    1. Become familiar with the if

    Example 2 (feetmeters conversion program)

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

    10/55MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 10

    1. Become familiar with the if

    Exercises

    Write a program that asks the user for an integer

    and then tells the user if that number is even or odd.(Hint, use Cs modulus operator %.)

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

    11/55MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 11

    Two-way Selection statement

    2. Add the else

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

    12/55MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 12

    if

    else logic flow

    2. Add the else

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

    13/55MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 13

    Example 1

    2. Add the else

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

    14/55MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 14

    Example 2

    2. Add the else

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

    15/55MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 15

    Exercises

    2. Add the else

    Write a program that requests two numbers and then

    displays either their sum or product, depending on

    what the user selects.

    input two number : 28 5

    select operator ( 1 : sum, 2 : product ) : 1

    28 + 5 = 33

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

    16/55MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 16

    Exercises

    2. Add the else

    Write a program that requests two numbers and then

    displays bigger number.

    input two number : 28 5

    28 > 5

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

    17/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 17

    Code block : { the statements }

    3. Create blocks of code

    One logical

    unit

    One logical

    unit

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

    18/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 18

    If the user enters any positive number, thisfragment prints the message

    3. Create blocks of code

    scanf(%d, &num) ;

    if(num > 0) {

    printf(This is ) ;

    printf(an example of ) ;

    printf(a code block.) ;

    }

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

    19/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 19

    Example 1

    3. Create blocks of code

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

    20/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 20

    Example 2

    3. Create blocks of code

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

    21/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 21

    Exercises

    3. Create blocks of code

    Write a program that either adds or subtracts two

    integers. First, prompt the user to choose an operation;

    then prompt for the two numbers and display the result.

    select operator ( 1 : add, 2 : subtract ) : 1

    input two number : 28 528 + 5 = 33

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

    22/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 22

    Cs loop statements for loop / while loop / do.. while loop

    forloop allows one or more statements to be

    repeated.

    General form of forloop

    Initialization

    Conditional-test

    increment

    4. Use the forloop

    for (initialization; conditional test; increment)

    statements;

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

    23/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 23

    Initialization Use to give an initial value to loop-control variable

    Execute only once before the loop begins

    Conditional-test

    Is performed at the start of each iteration. Test the loop-control variable against a target value

    True the loop repeat

    False the loop stop

    Increment Is executed at the bottom of the loop

    Increase or decrease the loop-control value by a certain

    amount

    4. Use the forloop

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

    24/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 24

    4. Use the forloop

    for statement

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

    25/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 25

    4. Use the forloop

    Simple exampleThis program uses a for loop to print the

    numbers 1 through 10 on the screen

    #include

    int main(void){

    int num;

    for(num=1; num

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

    26/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 26

    4. Use the forloop

    Example (loop will not execute)

    #include

    int main(void){

    int num;

    //this loop will not execute

    for(num=11; num

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

    27/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 27

    4. Use the forloop

    Example (A for loop can run negatively)

    #include

    int main(void){

    int num;

    //this loop will not execute

    for(num=20; num>0; num=num-1)

    printf(%d , num) ;

    printf(terminating);

    return 0;

    }20 19 18 17 3 2 1 terminating

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

    28/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 28

    4. Use the forloop

    Using block to repeat several statements This program computes the product and sum of the

    numbers from 1 to 5.

    #include

    int main(void){

    int num, sum, prod;

    sum = 0;

    prod = 1;

    for(num=1; num

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

    29/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 29

    4. Use the forloop

    Example (increment or decrement by more than one) This program counts to 100 by fives.

    #include

    int main(void){

    int i;

    for(i=0; i

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

    30/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 30

    4. Use the forloop

    Example 1. (addition-drill)

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

    31/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 31

    4. Use the forloop

    Example 2. (determination of prime number)

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

    32/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 32

    4. Use the forloop

    Exercises 1.

    Create a program that prints the numbers from 1 to 100.

    1 2 3 8 9 10: : : :: : : :: : : :

    91 92 93 98 99 100

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

    33/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 33

    4. Use the forloop

    Exercises 2.

    Write a program that prints the numbers between 17 to

    100 that can be evenly divided by 17.

    34 51 68 85

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

    34/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 34

    4. Use the forloop

    Exercises 3.

    Write a program similar to the prime-number tester,

    except that it displays all the factors of a number entered

    By the user.

    For example, if the user entered 8, it would respond with

    2 and 4.

    Enter a number : 8

    8s factor => 2 4

    5 Substitute Cs increment and

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

    35/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 35

    5. Substitute C s increment anddecrement Operators

    Increment operator (++) num = num +1; num++;

    Using for loop

    Decrement operator (--)

    count = count -1; count--;

    for(num=1; num

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

    36/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 36

    5. Substitute C s increment anddecrement Operators

    The Position of increment/decrement operator(++, --) Although the effect on the variable is the same, the position

    of the operator does affect when the operation is performed.

    #include

    int main(void){

    int i, j;

    i = 10;

    j = i++;

    // this will print 11 10

    printf(i and j : %d %d\n, i, j) ;

    return 0;

    }

    5 Substitute Cs increment and

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

    37/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 37

    5. Substitute C s increment anddecrement Operators

    After the variable ( num++ )

    If max = 1

    Result

    max = 2, count = 10

    First : the current value of variable is used for expression.

    After : increment expression execute.

    Arrangement

    count = 10 * max++;

    count = 10 * max++;count = 10 * max;

    max = max + 1;

    5 Substitute Cs increment and

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

    38/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 38

    5. Substitute C s increment anddecrement Operators

    Before the variable( ++num )

    First : increment expression execute.

    After : the incremented value of variable is used for

    expression.

    #includeint main(void){

    int i, j;

    i = 10;

    j = ++i;

    // this will print 11 11

    printf(i and j : %d %d\n, i, j) ;

    return 0;

    }

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

    39/55

    5 Substitute Cs increment and

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

    40/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 40

    5. Substitute C s increment anddecrement Operators

    Example 1.

    5. Substitute Cs increment and

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

    41/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 41

    5. Substitute C s increment anddecrement Operators

    Example 2.

    5. Substitute Cs increment and

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

    42/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 42

    5. Substitute C s increment anddecrement Operators

    Exercises 1.

    Change all appropriate assignment statements in this program

    to increment or decrement statements.

    #include

    int main(void) {int a, b;

    a=1;

    a = a + 1;

    b = a;

    b = b 1;

    printf(%d %d, a, b);

    return 0;

    }

    5. Substitute Cs increment and

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

    43/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 43

    5. Substitute C s increment anddecrement Operators

    Exercises 2.

    Enter a number. Compute summation until the number

    from 1.

    enter a number : 10

    sum of 1 ~ 10 : 55

    5. Substitute Cs increment and

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

    44/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 44

    5. Substitute C s increment anddecrement Operators

    Exercises 3.

    Enter a number. Compute factorial of the number.

    enter a number : 33! = 6

    enter a number : 5

    5! = 120

    enter a number : 10

    10! = 3628800

    6 Expand printf()s capabilities

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

    45/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 45

    6. Expand printf() s capabilities

    Backslash codes

    Represent characters that cannot be entered from the keyboard,

    and non-printing characters

    Escape character

    6 Expand printf()s capabilities

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

    46/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 46

    6. Expand printf() s capabilities

    '\n' (newline character)

    It translates it into a carriage return/linefeed combination.

    backslash codes are character constants.

    char ch;

    ch = '\t'; /* assign ch the tab character */

    6 Expand printf()s capabilities

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

    47/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 47

    number square cube

    ===============================1 1 1

    2 4 8

    3 9 27

    : : :10 100 1000

    6. Expand printf() s capabilities

    ExerciseUsing forloop (1~10)

    7. Program with Cs relational

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

    48/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 48

    gand Logical operators

    Relational operators Compare two values and return a true or false result based

    upon that comparison.

    Logical operators

    Connect together true/false results.

    7. Program with Cs relational

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

    49/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 49

    gand Logical operators

    Truth table (AND, OR, NOT)

    7. Program with Cs relational

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

    50/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 50

    gand Logical operators

    Precedence of operators The relational and logical operators are both lower in

    precedence than the arithmetic operators.

    10 + count > a + 12 (10 + count) > (a + 12)

    var > max || ! (max==100) && 0

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

    51/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 51

    gand Logical operators

    Example 1.

    if(count != 0) if(count)

    The expression !count is true only if count is zero.

    if(count == 0) if(!count)

    7. Program with Cs relational

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

    52/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 52

    gand Logical operators

    Example 2. (the outcome of a relational or logical operation)

    7. Program with Cs relational

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

    53/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 53

    gand Logical operators

    Example 3. (exclusive-OR)

    XOR truth Table

    A function of XOR

    7. Program with Cs relational

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

    54/55

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 54

    and Logical operators

    Example 3. (exclusive-OR) Demonstrate the xor()

    Skills Check

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

    55/55

    Skills Check

    Page 66 ~ 67