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 PROGRAM CONTROL Pseudocode CONTROL STRUCTURES - All programs could be written in terms of only three control
structures: FLOW CHART Usual meaning of flowchart symbols:
-OVAL - one containing the word "Begin" and the other containing the
word "End"
-RECTANGLE - action symbol
-DIAMOND - decision symbol C++ KEY WORDS 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 C++ statement 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: C++ statement 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: This could also be written as: Nested if/else structure Pseudocode: C++ statement This is the same as: This latter form is popular because it avoids the deep indentation of
the code to the right. Compound statement 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 C++ statement int product = 2; 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
While grade counter is less than or equal to ten
Set the class average to the total divided by ten C++ program
int main()
// initialization phase
// processing phase
while ( gradeCounter <= 10) { // loop 10 times
// termination phase
return 0; //indicate program ended successfully
Counter variables are normally initialized to zero or one, depending on
their use.
- 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.
- Specifying the order in which statements are to be executed in a
computer program is called program control.
- 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.
- 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.
Sequence structure, [DEFAULT in C++]
Selection structure, [USE OF 'if', 'if/else', 'switch'] and
Repetition structure. [USE OF 'while', 'do/while','for']
- 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.
- These are the usual first and last symbols of a flowchart.
-indicates an action to be taken like a calculation etc.
-indicates a decision is to be made.
- 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.
If students's grade is greater than or equal to 60
Print "Passed".
if ( grade >= 60)
cout << "Passed";
If student's grade is greater than or equal to 60
Print "Passed"
else
Print "Failed".
if ( grade >= 60 )
cout << "Passed";
else
cout << "Failed";
cout << ( grade >= 60 ? "Passed" : "Failed" );
grade >= 60 ? cout << "Passed" : cout << "Failed";
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".
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";
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";
if ( grade >= 60 )
cout << "Passed.\n";
else
{
cout << "Failed.\n";
cout << "You must take this course again.\n";
}
While the power of 2 is not larger than 1000
keep on getting the values of the power of 2.
while ( product <= 1000 )
product = 2 * product;
Set total equal to zero
Set grade counter to one
Input the next grade
Add the grade into the total
Add one to the grade counter
Print the class average
// Class average program with counter controlled repetition.
# include <iostream.h>
{
int total, // sum of grades
gradeCounter, // number of grades entered
grade; // one grade
float average; // average of grades
total = 0; // Clear total
gradeCounter = 1; // prepare to loop
cout << "Enter grade: "; // prompt for input
cin >> grade; // input grade
total = total + grade; // add grade to total
gradeCounter = gradeCounter + 1; // increment counter
}
average = total / 10.0; // division
cout << "Class average is " << average << endl;
}
THANKS FOR STOPPING BY !
TAKE CARE !
AFTER TODAY'S CLASS IS OVER, LOOK AT THE ASSIGNMENT.