MELJUN CORTES C++ Chap3 Control Statements

Embed Size (px)

Citation preview

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    1/68

    More C Program Control

    Statements

    C Programming Language

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    2/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 2

    Objectives

    Cs most important program controlstatements while, do~while

    Determine your programs flow of execution.

    Form the backbone of your programs.

    Well learn

    Nested loops

    More of control statements break, continueOther selection statement switch

    Unconditional jump statement goto

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    3/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 3

    1. Input Characters

    Using Library Functionsgetchar() : returns a single character typed on

    the keyboard.

    waits for a key to be pressed. (line buffers input)

    waits until you enter a carriage return.

    getche(): returns a single character immediately

    after a key is pressed.

    Interactive input

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    4/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 4

    1. Input Characters

    #include void main(void) {char ch;ch = getchar(); /* read a char */printf(" you typed: %c", ch);

    }

    getchar() function

    getche() function#include #include

    void main(void) {char ch;printf(Enter a character : );ch = getche(); /* read a char */printf(\nIts ASCII code is %d, ch);

    }

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    5/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 5

    1. Input Characters

    Example

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    6/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 6

    1. Input Characters

    Exercises

    Write a program that reads ten letters, After the

    Letters have been read, display the one that comes

    earliest in the alphabet.

    (Hint, The one with the smallest value comes first.)

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    7/68MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 7

    1. Input Characters

    Exercises

    Write a program that displays the ASCII codes for the

    characters A through Z. and a through z. How do the

    codes differ between the upper- and lowercase

    characters?

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    8/68MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 8

    if (count > max) // outer if

    if(error) printf(Error, try again.") ; // nested if

    2. Nest if statements

    When anifstatement is the target of another ifor elsenested if

    using indent of a nested if increase readability

    allow nested ifs at least 15 levels deep.

    Indent

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    9/68MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 9

    if (p)if(q) printf(a and b are true") ;

    else printf(To which statement does this else apply?);

    2. Nest if statements

    One confusing aspect of nested if

    Dangling else

    An elsealways associates with the nearestifin the same block that does not already have

    anelseassociated with it.

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    10/68MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 10

    if ( i != 0)

    if ( i > 0 )

    printf(positive!\n);

    else

    printf(necative!\n);

    T

    F

    T

    2. Nest if statements

    Dangling else - Example

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    11/68MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 11

    2. Nest if statements

    F T

    T

    Dangling else solution

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    12/68MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 12

    2. Nest if statements

    Examples

    if (expression)

    statement ;

    else if (expression)

    statement ;else if (expression)

    statement ;

    .

    .

    else

    statement ;

    if (expression) statement ;

    else

    if (expression) statement ;else

    if (expression) statement ;

    .

    .

    else statement ;

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    13/68

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    14/68MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 14

    2. Nest if statements

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    15/68MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 15

    2. Nest if statements

    Examples

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    16/68MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 16

    2. Nest if statements

    Exercises

    Write a program that computes the area of either

    a circle, rectangle, or triangle.

    Use an if-else-if ladder.

    ============ Area computation program =========Choice shape ( 1 : circle, 2 : rectangle, 3 : triangle ) : 1Input radius : 10Area of the circle is 31.40

    Choice shape ( 1 : circle, 2 : rectangle, 3 : triangle ) : 2Input width : 10Input height : 20Area of the rectangle is 200.00

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    17/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 17

    #include

    #include

    int main(void){

    int i;char ch;

    ch = 'a'; /* give ch an initial value */

    for(i=0; ch!='q'; i++) { /* if ch is q, stop loop */

    printf("pass: %d\n", i);ch = getche();

    }

    return 0;

    }

    3. Examine for loop variations

    Flexibility of for loop

    one or more of the expressions inside for may be empty.

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    18/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 18

    #include

    int main(void) {

    int i;

    printf("Enter an integer: ");

    scanf("%d", &i);

    for( ; i; i--)

    printf("%d ", i); /* i times excute */return 0;

    }

    3. Examine for loop variations

    Examples expression empty

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    19/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 19

    #include

    #include

    int main(void) {

    char ch;

    for(ch=getche(); ch!='q'; ch=getche()) ;

    printf(\nfound the q\n");

    return 0;

    }

    3. Examine for loop variations

    Examples target empty

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    20/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 20

    for( ; ; ) {

    }

    3. Examine for loop variations

    Examples infinite loop

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    21/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 21

    #include

    int main(void) {

    int i;

    for(i=0; i

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    22/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 22

    3. Examine for loop variations

    Exercises

    Write a program that computes driving time when given the

    distance and the average speed.

    Let the user specify the number of drive time computations he

    or she wants to perform.

    ============ Driving time computation program =========

    How many compute driving time : 2Enter the distance and average speed : 50 30

    Driving time : 1.67

    Enter the distance and average speed : 100 80

    Driving time : 1.25

    Formula : dist = speed * time time = dist / speed

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    23/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 23

    3. Examine for loop variations

    Exercises

    To create time-delay loops, for loops with empty targets are

    often used. Create a program that asks the user for a number

    and then iterates until zero is reached.Once the countdown is done, sound the bell, but dont display

    anything on the screen.

    ============ Time-Delay program =========

    Enter a number : 1000000

    Good bye~~!!

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    24/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 24

    3. Examine for loop variations

    Exercises

    Write a program that begins at 1 and runs to 1000.

    Have the program add the loop-control variable to itself inside

    the increment expression.This is an easy way to produce the arithmetic progression 1 2 4

    8 16, and so on.

    1 2 4 8 16 32

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    25/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 25

    4. Understand Cs whileLoop

    General form of whileloop

    while (expression)

    statement;

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    26/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 26

    4. Understand Cs whileLoop

    Compound statements of whileloop

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    27/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 27

    4. Understand Cs whileLoop

    #include

    #include

    int main(void) {

    char ch;

    ch=getche();

    while(ch!='q)

    ch=getche() ;

    printf("found the q");

    return 0;

    }

    Examples while loop

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    28/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 28

    4. Understand Cs whileLoop

    #include

    #include

    int main(void) {

    char ch;

    printf(Enter your message. \n);

    ch=getche();

    while(ch!=\r) {

    printf(%c, ch+1);

    ch=getche() ;

    }

    return 0;

    }

    Examples simple code machine

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    29/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 29

    4. Understand Cs whileLoop

    Exercises

    Exercise 1 of Section 3.3 use a whileloop

    ============ Driving time computation program =========

    How many compute driving time : 2Enter the distance and average speed : 50 30

    Driving time : 1.67

    Enter the distance and average speed : 100 80

    Driving time : 1.25

    Formula : dist = speed * time time = dist / speed

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    30/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 30

    4. Understand Cs whileLoop

    Exercises

    Write a program that converts centigrade to fahrenheit. Use a

    while loop and compute from 5 to 50 of centigrade.

    Increasing value is 5.

    Centigrade

    Fahrenheit

    ============ Fahrenheit computation program =========

    5 Centigrade is 41.00 Fahrenheit10 Centigrade is 50.00 Fahrenheit

    15 Centigrade is 59.00 Fahrenheit

    : :

    50 Centigrade is 122.00 Fahrenheit

    Formula : F = 9/5 * C + 32 ;

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    31/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 31

    5. Use the doLoop

    General form of do_whileloop

    do {

    statements

    } while (expression) ;

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    32/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 32

    5. Use the doLoop

    Compound statements of doloop

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    33/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 33

    5. Use the doLoop

    Examples reprompt the user until a valid input

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    34/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 34

    5. Use the doLoop

    #include

    #include

    int main(void) {

    char ch;

    do {

    ch=getche();

    } while(ch!='q);

    printf(Found the q. ");

    return 0;

    }

    Examples do loop

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    35/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 35

    5. Use the doLoop

    Exercises

    Write a program that converts gallons to liters. Using a do loop,

    allow the user to repeat the conversion.

    Gallon Liter

    ============ Gallons to Liters program =========

    Enter the Gallons : 100100 gallons is 378.5400 liters.

    Enter the Gallons : 1000

    1000 gallons is 3785.4000 liters.

    : :

    Formula : L = 3.7854 * G ;

    h d

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    36/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 36

    5. Use the doLoop

    Exercises

    Menu display

    ============ Mailing list menu =========

    1. Enter address2. Delete address

    3. Search the list

    4. Print the list

    5. Quit

    Enter the number of your choice (1-5) : 3

    Search the list

    Good Bye~~!!

    6 C d L

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    37/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 37

    6. Create nested Loops

    When the body of one loop contains another

    for(i=0; i

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    38/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 38

    6. Create nested Loops

    Example 1

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    39/68

    6 C d L

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    40/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 40

    6. Create nested Loops

    Exercises

    Write a program that finds all the prime numbers between

    2 and 1000.

    ============ The Prime Numbers ============

    The Prime Numbers : 2 3 5 7 11 13 17 19 ..........

    Prime number checking of a number(num)

    for(i=2; i

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    41/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 41

    6. Create nested Loops

    Exercises

    Write a program that reads ten characters from the keyboard.

    Each time a character is read, use its ASCII code value to output

    a string of periods equal in number to this code. For example,

    given the letter A, whose code is 65, your program would output

    65 periods.

    ============ Periods Printing Program ============A : ..

    D : ..: :

    : :N :

    6 C t t d L

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    42/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 42

    6. Create nested Loops

    Exercises star line

    Write a program that prints star lines when given the

    number of star.

    ============ Stars Printing Program ============

    How many lines do you want to print : 5

    6 C t t d L

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    43/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 43

    6. Create nested Loops

    Exercises star line

    Write a program that prints star lines when given the

    number of star.

    ============ Stars Printing Program ============

    How many lines do you want to print : 5

    6 C t t d L

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    44/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 44

    6. Create nested Loops

    Exercises star line

    Write a program that prints star lines when given the

    number of star.

    ============ Stars Printing Program ============

    How many lines do you want to print : 5

    6 C t t d L

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    45/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 45

    6. Create nested Loops

    Exercises star line

    Write a program that prints star lines when given the

    number of star.

    ============ Stars Printing Program ============

    How many lines do you want to print : 5

    6 C t t d L

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    46/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 46

    6. Create nested Loops

    Exercises star line

    Write a program that prints star lines when given the

    number of star.

    ============ Stars Printing Program ============

    How many lines do you want to print : 5

    6 Cr t t d L

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    47/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 47

    6. Create nested Loops

    Exercises star line

    Write a program that prints star lines when given the

    number of star.

    ============ Stars Printing Program ============

    How many lines do you want to print : 5

    6 Create nested Loops

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    48/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 48

    6. Create nested Loops

    Exercises star line

    Write a program that prints star lines when given the

    number of star.

    ============ Stars Printing Program ============

    How many lines do you want to print : 5

    7 Use break to exit a loop

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    49/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 49

    7. Use breakto exit a loop

    allows you to exit a loop

    for(i=1; i

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    50/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 50

    7. Use breakto exit a loop

    Using break in inner loops

    7 Use break to exit a loop

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    51/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 51

    7. Use breakto exit a loop

    Example 1

    7 Use break to exit a loop

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    52/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 52

    7. Use breakto exit a loop

    Example 2 exit from only the innermost loop

    #include

    int main(void) {

    int i, j ;

    for(i=0; i

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    53/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 53

    7. Use breakto exit a loop

    Exercises tip amount

    Write a program that prints a table showing the proper amount

    of tip to leave. Start the table at $1 and stop at $100, using

    increments of $1. Compute three tip percentages : 10%, 15%, and

    20%. After each line, ask the user if he or she wants to continue.

    If not, use break to stop the loop and end the program.

    ============ Tip amount Program ============

    total Tip : 10% 15% 20%

    $1 0.10 0.15 0.20 continue (y/n) ? y$2 0.20 0.30 0.40 continue (y/n) ? y

    $3 0.30 0.45 0.60 continue (y/n) ? n

    Thank you ~~!!

    8 The continue statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    54/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 54

    8. The continuestatement

    Forces the next iteration of the loop to take place,

    skipping any code in between itself and the test

    condition of loop.

    #include

    int main(void) {int x;

    for(x=0; x

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    55/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 55

    8. The continuestatement

    Using continue in loops

    8 The continue statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    56/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 56

    8. The continuestatement

    Example 1

    8 The continue statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    57/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 57

    8. The continuestatement

    Exercises Using continue

    Write a program that prints only the odd numbers between 1

    and 100. Use a for loop that looks like this :

    for(i=1; i

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    58/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 58

    9. The switchstatement

    Multiple selection statement.

    The logic of switch statement

    9 The switch statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    59/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 59

    9. The switchstatement

    The general form of the switch statement

    Flow

    9 The switch statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    60/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 60

    Demonstrate the switch statement

    9. The switchstatement

    #include

    int main (void) {

    int printFlag;

    printf(Please enter a integer:);

    scanf(%d, &printFlag);

    switch (printFlag) {

    case 1: printf(This is case 1\n);

    case 2: printf(This is case 2\n);

    default: printf(This is default\n);

    }

    } /* main */

    9 The switch statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    61/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 61

    Results of the demonstration

    9. The switchstatement

    9 The switch statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    62/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 62

    A switch with break statements

    9. The switchstatement

    9 The switch statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    63/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 63

    Demonstrate the switch statement

    9. The switchstatement

    #include

    int main(void) {

    int i;

    printf("Enter a number

    between 1 and 4: ");scanf("%d", &i);

    switch(i) {

    case 1:

    printf("one");

    break;

    case 2:

    printf("two");

    break;

    case 3:

    printf("three");

    break;

    case 4:printf("four");

    break;

    default:

    printf("unrecognized number");

    }

    return 0;

    }

    9 The switch statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    64/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 64

    Switch will work with only intor chartypes.

    It is possible to have a switch as part of the

    statement sequence of an outer switch

    9. The switchstatement

    switch(a) {

    case 1:

    switch(b) {

    case 0: printf("b is false");

    break;

    case 1: printf("b is true");

    }break;

    case 2:

    :

    9 The switch statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    65/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 65

    Example 1

    9. The switchstatement

    9. The switch statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    66/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 66

    Example 2

    9. The switchstatement

    9. The switch statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    67/68

    MELJUN CORTES MBA MPA BSCS

    School of Computer I nformation 67

    Example 3

    9. The switchstatement

    #include

    #include

    int main(void) {

    char ch;

    printf("Enter the letter: ");

    ch = getche();switch(ch) {

    case 'a':

    case 'e':

    case 'i':

    case 'o':

    case 'u':case 'y': printf(" is a vowel\n");

    break;

    default: printf(" is a consonant");

    }

    return 0;

    }

    9. The switch statement

  • 8/10/2019 MELJUN CORTES C++ Chap3 Control Statements

    68/68

    9. The switchstatement

    Exercises