Notes
Outline
COM1100
Fundamentals of Computer Science –Winter 2000
Lecture 5
01/18/99
Announcement
There is no office hour today.
Questions will be answered by email.
Reading material this week
Textbook
Chapter 4 : Decision
4.1 The if statement
4.2 Relational Operator
Chapter 5 : Functions (5.1 ~ 5.4)
Chapter 3 : (optional)
is not exactly like our graphics package
Package 1 – Patterns
NameUseFunctions
Package 2 – Recitation 2
Package 3 – Lab 2
Today’s lecture
Review of previous lectures
C++ programming style
Flow of control
if statement
Review of previous lectures -- 1
First C++ program
Identifier
Variable declaration, initialization and use
Assignment statements
Basic Data Type
int, short
double
char
Review of previous lectures -- 2
Arithmetic operation
In the absence of parentheses, expressions containing multiple arithmetic operators are evaluated by the priority, or precedence.
Example
int result;
  result = (8 + 2) * 6 % 7 - 10 / 5;
  cout << "The result is : " << result << endl;
Review of previous lectures -- 3
Raw I / O
cout << x << endl;
cin >> x;
Verified Read
x = RequestInt(“Please enter a number :”);
RequestString(“Enter your name: ”, myName);
Verified Input with Default
x = RequestInt(“Enter a number:", 1);
RequestString("Type in your name", “Yuhong”, myName);
Review of previous lectures -- 4
Graphic functions
SetForeColor(short R, short G, short B);
PaintRect(short x1, short y1, short x2, short y2);
PaintCircle(short x1, short y1, short radius);
PaintOval(short x1, short y1, short x2, short y2);
InvertOval(short x1, short y1, short x2, short y2);
Today’s lecture
Review of previous lectures
C++ programming style
Flow of control
if statement
C++ programming style
Conceptually, C++ source file is free-form, no set columns or required alignment.
Good C++ Programming Style
One of the most important factor to be a good programmer
Purpose: make the program readable and understandable
Good Programming Style -- 1
There are spaces after keywords and between binary operators.
int x = 5 + y;
Braces must line up.
{
}
Good Programming Style -- 2
Choose descriptive variable names
string s; // bad variable names
string myMood; // good variable names
int     x;   // bad variable names
int     YearOfBirth;    // good variable names
When declare variables for graphic coordinates
int x1; // left horizontal
int y1; // top vertical
int x2; // right horizontal
int y2; // bottom vertical
int r; // radius of a circle
Good Programming Style -- 3
No magic numbers may be used.
Instead defining constant
Constant will be discussed in later lectures
Use comments
Use indentations
Tab key
C++ Comments
In C++, two methods for writing comments.
C style comments
/* here is a remark */
/* here is a comments
       goes to another line */
C++ one line comment
// to the end of the current line
x =  x + 1; // increment x by 1
// Here is a C++ remark
Today’s lecture
Review of previous lectures
C++ programming style
Flow of control
if statement
Flow of control
Flow of control refers to the order in which a program’s statements are executed.
Any algorithm could be constructed using combinations of four standardized flow of control structures:
Sequential (normal)
Selection
Repetition
Invocation
Selection – if statement
if ( condition)
statement1;
if ( condition)
statement1;
else
statement2;
Executes statement1 if condition is true (not zero)
When else is present executes statement2 if condition is false (zero)
if statement -- 2
Parentheses around condition are required.
Condition is often the result of relational operators
if ( x > y )
     return x;
else
     return y;
if statement -- 3
Condition is sometimes a mathematical expression
int x  = 5;
int y = 3;
int sum = 14;
int result;
if ( x - y  ) // equals to if ( (x-y) !=0 )
result = sum / (x - y );
else
cout << “Error: Divided by zero ! \n”
if statement -- 4
Condition also can simply be a variable
if ( x ) // same as if ( x != 0 )
result = sum / x;
Examples
int x = -5;
if ( x )
cout << “Good Morning !” << endl;
else
cout << “Good Afternoon !” << endl;
Relational Operators
Operand  Relational_Operator Operand
Price < 10.5
Operator Meaning Example
< Less Than age < 20
> Greater than height > 6.2
<= Less than or equal to num1 <= num2
>= Greater than or equal to temp >= 98.5
== Equal to grade == 100
!= Not Equal to character != ‘a’
Today’s lecture
Review of previous lectures
C++ programming style
Flow of control
if statement
Lecture Thursday
Functions
C++ Source File Layout
More on Graphic functions