|
|
|
|
|
|
Textbook |
|
Chapter 6 : Iteration |
|
6.1.1, 6.1.2, 6.1.3 |
|
6.2.2 |
|
Package 1 – Patterns |
|
EncapsulateLoop |
|
Package 2 – Recitation 4 |
|
Package 3 – Lab 4 |
|
Sample quiz in Lecture 9 notes |
|
|
|
|
|
|
Questions from recitation and lab |
|
More on escape sequence |
|
Logic operators |
|
Flow of control |
|
Repetition |
|
while statement |
|
do – while statement |
|
break statement |
|
continue statement |
|
Quiz |
|
|
|
|
|
Write a function that will paint an arc (part of
a circle). The arguments specify the center of the arc, the radius, the
starting and ending angle. Use one degree angle increments. The coordinates
of a point on a circle with the center (xcenter, ycenter), radius r, and
angle a are: |
|
x = xcenter + radius * cosdeg(a); |
|
y = ycenter + radius * sindeg(a); |
|
|
|
|
Purpose: an arc grows in clockwise direction |
|
// function signature |
|
void
PaintArc(int xcenter, int ycenter, int r, int sAngle, int eAngle); |
|
|
|
// function definition |
|
void
PaintArc(int xcenter, int ycenter, int r, int sAngle, int eAngle) { |
|
SetForeColor(255, 0, 0); // set
color |
|
for (int i = sAngle; i <= eAngle;
i++) { |
|
int x = xcenter + r * cosdeg(i -
sAngle); |
|
int y = ycenter + r * sindeg(i -
sAngle); |
|
DrawLine(xcenter, ycenter, x, y); |
|
} |
|
} |
|
|
|
|
|
|
// get info from user |
|
int xcenter = RequestInt("Enter x:
", 100); |
|
int ycenter = RequestInt("Enter y:
", 100); |
|
int r = RequestInt("Enter
radius:", 50); |
|
int startAngle = RequestInt("Enter
starting angle: ", 0); |
|
int endAngle = RequestInt("Enter ending
angle: ", 45); |
|
|
|
// function call |
|
PaintArc(xcenter, ycenter, r, startAngle,
endAngle); |
|
|
|
|
|
|
Purpose: make the arc grows in anticlockwise
direction |
|
// function definition |
|
void
PaintArc(int xcenter, int ycenter, int r, int sAngle, int eAngle) { |
|
SetForeColor(255, 0, 0); // set
color |
|
for (int i = sAngle; i <= eAngle;
i++) { |
|
int x = xcenter + r * cosdeg(i -
sAngle); |
|
int y = ycenter - r * sindeg(i -
sAngle); // change here |
|
DrawLine(xcenter, ycenter, x, y); |
|
} |
|
} |
|
|
|
|
|
|
|
The Sleep function suspends the execution of the
current program for a specified interval. |
|
Syntax : Sleep(sleep_time); |
|
sleep time is in milliseconds (0.001 sec) |
|
Sleep(1000); // suspend for 1 second |
|
Sleep(500); // suspend for 0.5 second |
|
|
|
|
|
|
|
Visual C++ does not support SysBeep(1) |
|
Instead, you can use ‘\a’ to play the system
Beep sound. |
|
cout << ‘\a’ ; |
|
Will play whatever system beep is currently set. |
|
cout << “Hello, World ! \n \a” ; |
|
|
|
|
|
When a backslash(\) is used directly in front of
a select group of characters, the backslash tells the computer to escape
from the way these characters would normally be interpreted. |
|
For this reason, the combination of a backslash
and these specific characters are called escape sequence. |
|
We have already encountered an example |
|
New line escape sequence: \n |
|
|
|
|
|
In addition to using simple relational
expressions as condition, more complex conditions can be created using the
logic operators AND, OR, and NOT. |
|
In C++, these operators are represented by the
symbols &&, ||, and !, respectively |
|
|
|
|
When the AND (&&) is used with two
simple expressions, the condition is true only if both individual
expressions are true by themselves. |
|
(age > 40) && (term < 10) |
|
int x = 5, y =8; |
|
if ( ( x > 0) && ( (y –x) > 3
) ) |
|
cout << “Good Morning!” <<
endl; |
|
else |
|
cout << “Good Afternoon!”
<< endl; |
|
//will print: Good Afternoon! |
|
|
|
|
|
|
When the OR (||) is used with two simple
expressions, the condition is satisfied if either one or both of the two
expressions is true. |
|
(age > 40) || (term < 10) |
|
int x = 5, y =8; |
|
if ( ( x > 0) || ( (y –x) > 3 ) ) |
|
cout << “Good Morning!” <<
endl; |
|
else |
|
cout << “Good Afternoon!”
<< endl; |
|
//will print: Good Morning! |
|
|
|
|
|
|
|
The NOT (!) operator is used to change an
expression to its opposite state. |
|
If the expression has any nonzero value (true),
!expression produce a zero value (false). |
|
If an expression is false to begin with (has a
zero value), !expression will be true. |
|
int x = 5, y =8; |
|
if ( ! ( (y –x) > 3 ) ) |
|
cout << “Good Morning!” <<
endl; |
|
else |
|
cout << “Good Afternoon!”
<< endl; |
|
//will print: Good Morning! |
|
|
|
|
|
|
|
Parentheses |
|
Arithmetic expression |
|
* / % |
|
+ - |
|
Relational expression |
|
< <= > >= |
|
== != |
|
Logic Expression |
|
! |
|
&& |
|
|| |
|
|
|
|
(6 * 3 == 34 / 2) || (13 < 3 * 3 + 4) &&
!(6 – 2 < 5) |
|
(18 == 17) || ( 13 <
9 + 4 ) && ! (4 < 5) |
|
0 || (13 < 13) && !1 |
|
0 || 0 && 0 |
|
0 || 0 |
|
0 |
|
|
|
|
Used to repeat a statement (or black) while an
expression is true (not zero) |
|
Syntax : |
|
while (condition) { |
|
statements; |
|
} |
|
purpose: execute the statements while the
condition remains true. |
|
|
|
|
|
|
When the while statement is entered, the test
condition is evaluated. |
|
If the test succeeds, the loop body is executed. |
|
Then the test is performed again. |
|
As long as it is successful, the body of the
while statement is executed. |
|
As soon as the test fails, the next statement
after the end of the body is executed. |
|
|
|
|
|
|
It is possible that the body of the while
statement is never executed at all: namely, if the result fails the first
time. |
|
|
|
|
|
|
Sometimes, we want to execute the loop body at
least once and perform the loop test after the body was executed. |
|
Syntax |
|
do { |
|
statements; |
|
}
while (condition) |
|
purpose: execute the statements, then test the
condition, and repeat the statement while the condition remains true. |
|
|
|
|
|
|
int i = 10; |
|
while ( i >= 1) { |
|
cout << i << “ “; |
|
i --; |
|
} |
|
int i = 1; |
|
do { |
|
cout << i << “ “; |
|
i --; |
|
} while ( i > 1); |
|
|
|