4.11
4.12
4.13
Introduction
Algorithms
Pseudocode
Control Structures
if Selection Structure
if/else Selection Structure
while Repetition Structure
Formulating Algorithms: Case Study 1
(Counter-Controlled Repetition)
Formulating Algorithms with Top-Down, Stepwise Refinement:
Case Study 2 (Sentinel-Controlled Repetition)
Formulating Algorithms with Top-Down, Stepwise Refinement:
Case Study 3 (Nested Control Structures)
Assignment Operators
Increment and Decrement Operators
Introduction to Windows Application Programming
4.1 Introduction
Before writing a program
4.2 Algorithms
Procedure
The actions a program will perform
The order these actions will be performed
Also called an algorithm
Program control
4.3 Pseudocode
Pseudocode
Selection structure
The if and if/else statements
The goto statement
No longer used unless absolutely needed
Causes many readability problems
Repetition structure
The while and do/while loops (chapter 5)
The for and foreach loops (chapter 5)
Stacking
Placing one after another
Nesting
Inserting of one structure into another
Fig. 4.1
add 1 to counter
counter = counter + 1;
abstract
byte
class
delegate
event
fixed
goto
interface
namespace
out
public
sealed
static
throw
ulong
value
Fig. 4.2
as
case
const
do
explicit
float
if
internal
new
override
readonly
set
string
true
unchecked
virtual
C# keywords.
base
catch
continue
double
extern
for
implicit
is
null
params
ref
short
struct
try
unsafe
void
bool
char
decimal
else
false
foreach
in
lock
object
private
return
sizeof
switch
typeof
ushort
volatile
break
checked
default
enum
finally
get
int
long
operator
protected
sbyte
stackalloc
this
uint
using
while
10
true
Grade >= 60
print Passed
false
Fig. 4.3
11
12
false
true
Grade >= 60
print Failed
Fig. 4.4
print Passed
13
14
15
true
Product <= 1000
Product = 2 * product
false
Fig. 4.5
16
Fig. 4.6 Pseudocode algorithm that uses counter-controlled repetition to solve the classaverage problem.
2002 Prentice Hall. All rights reserved.
17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Outline
Average1.cs
class Average1
{
static void Main( string[] args )
{
int total,
// sum of grades
gradeCounter,
// numberInitialize
of grades
entered
total
to 0
gradeValue,
// grade value
average;
// average of all grades
Initialize gradeCounter to 1
// initialization phase
total = 0;
// clear total
gradeCounter = 1;
// prepare to loop
The
// processing phase
while ( gradeCounter <= 10 ) // loop 10 times
{
// prompt for input and read grade from user
Console.Write( "Enter integer grade: " );
// add 1 to gradeCounter
gradeCounter = gradeCounter + 1;
}
18
34
35
36
37
38
39
40
41
42
43
// termination phase
average = total / 10;
Outline
// integer division
Average1.cs
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
integer
integer
integer
integer
integer
integer
integer
integer
integer
integer
grade:
grade:
grade:
grade:
grade:
grade:
grade:
grade:
grade:
grade:
100
88
93
55
68
77
83
95
73
62
Program Output
Class average is 79
19
Creating pseudocode
Start with one task
Break it into several tasks
Continue breaking until each task is simple
Casting
20
Fig. 4.8 Pseudocode algorithm that uses sentinel-controlled repetition to solve the classaverage problem.
2002 Prentice Hall. All rights reserved.
21
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
Outline
Average2.cs
class Average2
{
static void Main( string[]
{
int total,
//
gradeCounter,
//
gradeValue;
//
double average;
args )
sum of grades
The variable average is set to a
number of grades entered
double so that it can be more exact
grade value
// initialization phase
total = 0;
// clear total
gradeCounter = 0;
// prepare to loop
// processing phase
// prompt for input and convert to integer
Console.Write( "Enter Integer Grade, -1 to Quit: " );
gradeValue = Int32.Parse( Console.ReadLine() );
22
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Outline
23
Average2.cs
Accumulate the total of the grades
// add 1 to gradeCounter
gradeCounter = gradeCounter + 1;
Makethe
sure
theby
total
Divide
total
theamount
number
of
entered
grades
was
not
of times the program looped
to zero
find to
theprevent
averageany errors
Enter
Enter
Enter
Enter
Integer
Integer
Integer
Integer
Grade,
Grade,
Grade,
Grade,
-1
-1
-1
-1
to
to
to
to
Quit:
Quit:
Quit:
Quit:
97
88
72
-1
Outline
Average2.cs
Program Output
24
Multiple loops
Loops with if statements
25
26
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
Outline
Analysis.cs
class Analysis
{
static void Main( string[] args )
{
Initialize both passes and failures
int passes = 0,
// number of passes
Set the student count to 1
failures = 0,
// number of failures
student = 1,
// student counter
result;
// one exam result
to 0
student = student + 1;
}
27
30
31
32
33
34
35
36
37
38
39
40
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
// termination phase
Console.WriteLine();
Console.WriteLine( "Passed: " + passes );
Console.WriteLine( "Failed: " + failures );
if ( passes > 8 )
Console.WriteLine( "Raise Tuition\n" );
Outline
Analysis.cs
Display the results to the user
result
result
result
result
result
result
result
result
result
result
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
1
2
1
1
1
1
1
1
1
1
Program Output
Passed: 9
Failed: 1
Raise Tuition
28
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
Enter
result
result
result
result
result
result
result
result
result
result
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
(1=pass,
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
2=fail):
1
2
2
2
2
2
1
1
1
1
Outline
Analysis.cs
Program Output
Passed: 5
Failed: 5
29
30
31
Sample expression
Explanation
+=
-=
*=
/=
%=
c
d
e
f
g
c
d
e
f
g
Assume: int c = 3,
d = 5, e = 4, f =
6, g = 12;
+=
-=
*=
/=
%=
7
4
5
3
9
Assigns
=
=
=
=
=
c
d
e
f
g
+
*
/
%
7
4
5
3
9
10 to c
1 to d
20 to e
2 to f
3 to g
32
Decrement operator
Used to subtract 1 from the variable
y--
++x or --x
Will add to or subtract one from the value and then perform an
action
2002 Prentice Hall. All rights reserved.
33
Called
Sample expression
Explanation
++
postincrement
a++
--
predecrement
--b
--
postdecrement
b--
++
preincrement
++a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
5
5
6
5
6
6
Outline
Increment.cs
class Increment
{
static void Main(string[] args)
{
int c;
Declare variable c
Set c equal
Display
c (5) to 5
c = 5;
Console.WriteLine( c );
// print 5
Console.WriteLine( c++ ); // print 5 then
postincrement
Display
c (5) then add
Console.WriteLine( c );
// print 6
Console.WriteLine();
Display c (6)
// skip a line
c = 5;
Console.WriteLine( c );
// print c5 is set to 5 Display c (5)
Console.WriteLine( ++c ); // preincrement then print 6
Console.WriteLine( c );
// print 6 Add 1 then display c (6)
} // end of method Main
Display c (6)
Program Output
34
35
()
++ -++ -- + - (type)
* / %
+ < <= > >=
== !=
?:
= += -= *= /= %=
Associativity
Type
right to left
unary prefix
left to right
multiplicative
left to right
additive
left to right
relational
left to right
equality
right to left
conditional
right to left
assignment
left to right
right to left
parentheses
unary postfix
Fig. 4.15 Precedence and associativity of the operators discussed so far in this
book.
Derived class
The class from which another class inherits
36
Collapsed
comment
Collapsed
code
37
38
Expanded code
39
Property initializations
for WelcomeLabel
Text property
40
Fig. 4.20 Windows Form Designer generated code reflecting new property values.
41
Text property
42
43
44
45