|
|
|
|
|
There is no office hour today. |
|
Questions will be answered by email. |
|
|
|
|
|
|
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 |
|
|
|
|
|
Review of previous lectures |
|
C++ programming style |
|
Flow of control |
|
if statement |
|
|
|
|
|
|
|
|
|
First C++ program |
|
Identifier |
|
Variable declaration, initialization and use |
|
Assignment statements |
|
Basic Data Type |
|
int, short |
|
double |
|
char |
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
|
|
|
|
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); |
|
|
|
|
|
Review of previous lectures |
|
C++ programming style |
|
Flow of control |
|
if statement |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
There are spaces after keywords and between
binary operators. |
|
int x = 5 + y; |
|
Braces must line up. |
|
{ |
|
} |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
No magic numbers may be used. |
|
Instead defining constant |
|
Constant will be discussed in later lectures |
|
Use comments |
|
Use indentations |
|
Tab key |
|
|
|
|
|
|
|
|
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 |
|
|
|
|
|
|
|
Review of previous lectures |
|
C++ programming style |
|
Flow of control |
|
if statement |
|
|
|
|
|
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 |
|
|
|
|
|
|
|
|
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) |
|
|
|
|
|
|
|
|
|
Parentheses around condition are required. |
|
|
|
Condition is often the result of relational
operators |
|
if ( x > y ) |
|
return x; |
|
else |
|
return y; |
|
|
|
|
|
|
|
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” |
|
|
|
|
|
|
|
|
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; |
|
|
|
|
|
|
|
|
|
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’ |
|
|
|
|
|
|
|
Review of previous lectures |
|
C++ programming style |
|
Flow of control |
|
if statement |
|
|
|
|
Functions |
|
C++ Source File Layout |
|
More on Graphic functions |
|
|
|
|
|