CS (151) - INTRODUCTION TO PROGRAMMING C++

Teacher: Dr. Shrestha



Fall Semester, 2000

Class hour: 5:00 - 10:00 Friday

Materials for November 3, 2000
*****************************

Chapter (2)
***********

Control Structures


Save days' work in your diskette !

    ALGORITHMS
    - A procedure for solving a problem in terms of the actions to be executed, and the order in which these actions are to be executed, is called an algorithm.

    PROGRAM CONTROL
    - Specifying the order in which statements are to be executed in a computer program is called program control.

    Pseudocode
    - Pseudocode is an artificial and informal language that helps programmers develop algorithms. It helps the programmer "think out" a program before attempting to write it in a programming language.

    CONTROL STRUCTURES
    - Usually statements in a program are executed one after another in the order in which they are written. This is called sequential execution. However, some C++ statements could be written that enable the programmer to specify that the next statement to be executed may be other than the next one in sequence. This is called transfer of control- reminds the notorious "goto" statements of the past.

    - All programs could be written in terms of only three control structures:
    Sequence structure, [DEFAULT in C++]
    Selection structure, [USE OF 'if', 'if/else', 'switch'] and
    Repetition structure. [USE OF 'while', 'do/while','for']

    FLOW CHART
    - A flow chart is a graphical representation of an algorithm or of a portion of an algorithm. Flowcharts are drawn using certain special-purpose symbols such as rectangles, diamonds, ovals, and small circles connected by arrows called flowlines.

    Usual meaning of flowchart symbols:

    -OVAL - one containing the word "Begin" and the other containing the word "End"
    - These are the usual first and last symbols of a flowchart.

    -RECTANGLE - action symbol
    -indicates an action to be taken like a calculation etc.

    -DIAMOND - decision symbol
    -indicates a decision is to be made.

    C++ KEY WORDS
    - keywords are words reserved by the language to implement various features and are those that should not be used as identifiers such as for variable names.

    The following are C++ keywords:

    asm, auto, break, bool, case, char, catch, class, const, const_cast, continue, delete, default, do, double, dynamic_cast, else, enum, explicit, extern, false, float, for, friend, goto, if, inline, int, long, mutable, namespace, new, operator, private, protected, public, register, reinterpret_cast, return, short, signed, sizeof, static, static_cast, struct, switch, template, this, throw, true, try, typedef, typeid, typename, union, unsigned, using, virtual, void, volatile, wchar_t, while.

    THE if SELECTION PROCEDURE

    - A selection structure is used to choose among alternative courses of action.

    Example:

    Pseudocode
    If students's grade is greater than or equal to 60
    Print "Passed".

    C++ statement

    if ( grade >= 60)
    cout << "Passed";

    if LOGIC - condition true - statement executed, condition false - statement ignored and control is passed to the next statement.

    In C++ draft standard, the keywords "true" and "false" are used to represent the values of type "bool" - based on the word boolean algebra.

    if STRUCTURE - single-entry/single-exit structure.

    THE if/else SELECTION STRUCTURE

    - A different action is to be performed when the condition is "true" than when the condition is "false".

    Example:

    Pseudocode:
    If student's grade is greater than or equal to 60
    Print "Passed"
    else
    Print "Failed".

    C++ statement

    if ( grade >= 60 )
    cout << "Passed";
    else
    cout << "Failed";

    C++ provides the conditional operator (?:) that is closely related to the if/else structure.

    The conditional operator is C++'s only ternary operator - it takes three operands.

    Example:
    cout << ( grade >= 60 ? "Passed" : "Failed" );

    This could also be written as:
    grade >= 60 ? cout << "Passed" : cout << "Failed";

    Nested if/else structure

    Pseudocode:

    If student's grade is greater than or equal to 90
    Print "A"
    else
    If student's grade is greater than or equal to 80
    Print "B"
    else
    If student's grade is greater than or equal to 70
    Print "C"
    else
    If student's grade is greater than or equal to 60
    Print "D"
    else
    Print "F".

    C++ statement

    if ( grade >= 90 )
    cout << "A";
    else
    if ( grade >= 80 )
    cout << "B";
    else
    if ( grade >= 70 )
    cout << "C";
    else
    if ( grade >= 60 )
    cout << "D";
    else
    cout << "F";

    This is the same as:

    if ( grade >= 90 )
    cout << "A";
    else if ( grade >= 80 )
    cout << "B";
    else if ( grade >= 70 )
    cout << "C";
    else if ( grade >= 60 )
    cout << "D";
    else
    cout << "F";

    This latter form is popular because it avoids the deep indentation of the code to the right.

    Compound statement

    if ( grade >= 60 )
    cout << "Passed.\n";
    else
    {
    cout << "Failed.\n";
    cout << "You must take this course again.\n";
    }

    The braces surrounding the two statements are important, without them, the statement

    cout << "You must take this course again.\n";

    would be outside the body of the "else" part of the "if", and would execute regardless of whether the grade is less than 60.

    THE while REPETITION STRUCTURE

    - Allows the programmer to specify that an action is to be repeated while some condition remains true.

    Example:

    Pseudocode
    While the power of 2 is not larger than 1000
    keep on getting the values of the power of 2.

    C++ statement

    int product = 2;
    while ( product <= 1000 )
    product = 2 * product;

    The initial value of "product" here which is 2 and which is obtained by raising the power of 2 by 1 is called INITIALIZATION.

    A PROBLEM

    A class of ten students took a quiz. The grades (integers in the range of 0 to 100) for this quiz are availabale to you. Determine the class average on the quiz.

    Pseudocode
    Set total equal to zero
    Set grade counter to one

    While grade counter is less than or equal to ten
    Input the next grade
    Add the grade into the total
    Add one to the grade counter

    Set the class average to the total divided by ten
    Print the class average

    C++ program

    // Class average program with counter controlled repetition.
    # include <iostream.h>

    int main()
    {
    int total, // sum of grades
    gradeCounter, // number of grades entered
    grade; // one grade
    float average; // average of grades

    // initialization phase
    total = 0; // Clear total
    gradeCounter = 1; // prepare to loop

    // processing phase while ( gradeCounter <= 10) { // loop 10 times
    cout << "Enter grade: "; // prompt for input
    cin >> grade; // input grade
    total = total + grade; // add grade to total
    gradeCounter = gradeCounter + 1; // increment counter
    }

    // termination phase
    average = total / 10.0; // division
    cout << "Class average is " << average << endl;

    return 0; //indicate program ended successfully
    }

    Counter variables are normally initialized to zero or one, depending on their use.


THANKS FOR STOPPING BY !

THINGS CHANGE - COME BACK AGAIN !

TAKE CARE !

AFTER TODAY'S CLASS IS OVER, LOOK AT THE ASSIGNMENT.