Professional Documents
Culture Documents
Outline
3.1
3.2
3.3
3.4
3.5
3.6
3.7
3.8
3.9
Introduction
Essentials of Counter-Controlled Repetition
for Repetition Structure
Examples Using the for Structure
switch Multiple-Selection Structure
do/while Repetition Structure
Statements break and continue
Logical and Conditional Operators
Structured-Programming Summary
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1
2
3
4
5
Outline
WhileCounter.cs
class WhileCounter
This is where the counter variable
{
static void Main( string[] args
)
is initialized.
It is set to 1.
{
int counter = 1;
// initialization
// increment
Program Output
Variable scope
for keyword
Loop-continuation condition
Fig. 3.3
int counter = 1
counter <= 10
false
Fig. 3.4
true Console.WriteLine
( counter * 10 );
Body of loop (this may
be multiple statements)
counter++
Increment the
control variable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
Outline
ForCounter.cs
class
{
static void Main( string[]
args
greater than
five) (it will stop once it
{
gets to six)
// initialization, repetition condition and incrementing
// are all included in the for structure
for ( int counter = 1; counter <= 5; counter++ )
Console.WriteLine( counter );
}
}
Program Output
When decrementing
In most cases > or >= is used
Message boxes
Buttons
OK
OKCancel
YesNo
AbortRetryIgnore
YesNoCancel
RetryCancel
Exclamation
Question
Error
Information
Formatting
(variable : format)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Outline
Sum.cs
class Sum
The counter. It is initialized to 2
{
static void Main( string[] args )
{
int sum = 0;
box
Argument 4:
MessageBoxIcon
(Optional)
Argument 3: OK
dialog button.
(Optional)
Program Output
Argument 1:
Message to display
10
11
Icon
MessageBoxIcon.Information
MessageBoxIcon.Question
MessageBoxIcon.Error
Fig. 5.6
Description
Displays a dialog with an
exclamation point. Typically
used to caution the user against
potential problems.
Displays a dialog with an
informational message to the
user.
Displays a dialog with a question
mark. Typically used to ask the
user a question.
Displays a dialog with anxin a
red circle. Helps alert user of
errors or important messages.
12
MessageBox Buttons
Description
MessageBoxButton.OKCancel
MessageBoxButton.YesNo
MessageBoxButton.YesNoCancel
MessageBoxButton.RetryCancel
MessageBoxButton.AbortRetryIgnore
MessageBoxButton.OK
Fig. 5.7
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
Outline
Interest.cs
class Interest
{
static void Main( string[] args )
Loops through 10 times
{
decimal amount, principal =starting
( decimal
) 1000.00;
at 1 and
ending at 10,
double rate = .05;
adding 1 to the counter (year)
string output;
each time
Insert a Tab
}
13
Outline
Interest.cs
Program Output
14
15
Description
D or d
N or n
E or e
Formats the number using scientific notation with a default of six decimal places.
F or f
Formats the string with a fixed number of decimal places (two by default).
G or g
General. Either E or F.
X or x
C or c
Fig. 5.9
Formats the string as currency. Precedes the number with an appropriate currency
symbol ($ in the US). Separates digits with an appropriate separator character
(comma in the US) and sets the number of decimal places to two by default.
16
Cases
Case x :
Use of constant variable cases
Empty cases
The default case
Outline
1
// Fig. 3.10: SwitchTest.cs
2
// Counting letter grades.
3
4
using System;
SwitchTest.cs
5
6
class SwitchTest
7
{
8
static void Main( string[] args )
9
{
10
char grade;
// one grade
A
for
loop
that
initializes
1, loops
11
int aCount = 0, //i to
number
of 10
As
Each of these variables acts as a
12
bCount
=
0,
//
number
of
times and increments i by one each time Bs
13
cCount = 0, // number of Cs
counter so they are initialized to zero
14
dCount = 0, // number of Ds
Prompt the user for a grade and
15
fCount = 0; // number of Fs
16
store it into the grade variable
17
for ( int i = 1; i <= 10; i++ )
18
{
start of the switch
19
Console.Write( "Enter a letter grade:The
" );
20
grade = Char.Parse( Console.ReadLine()
);
statement.
The grade variable is
21 Both cases add one to aCount
used as the data to be tested for
22
switch ( grade )
each
23
{
casecase.
A is empty so it is the
24
case 'A':
// grade is uppercase A
same as case a
25
case 'a':
// or lowercase a
26
++aCount;
The break statement is used to exit the
27
break;
switch statement and not perform the rest
28
29
case 'B':
// grade is uppercase of
B the operations
30
case 'b':
// or lowercase b
31
++bCount;
Both case B and case b add one to
32
break;
the bCount variable
33
17
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Outline
case 'C':
// grade
uppercase
Bothiscases
add 1 toCcCount
case 'c':
// or lowercase c
++cCount;
break;
case 'D':
// grade is uppercase D
case 'd':
// or lowercase
d
If grade
equals D or
++dCount;
add one to dCount
break;
case 'F':
// grade is uppercase F
case 'f':
// or lowercase
f one to
Add
++fCount;
break;
SwitchTest.cs
d
} // end for
Console.WriteLine(
"\nTotals for each letter grade are:\nA: {0}" +
"\nB: {1}\nC: {2}\nD: {3}\nF: {4}", aCount, bCount,
cCount, dCount, fCount );
} // end method Main
} // end class SwitchTest
18
Outline
SwitchTest.cs
Program Output
19
20
true
case a action(s)
break;
case b action(s)
break;
case z action(s)
break;
false
case: b
true
false
.
.
.
case: z
true
false
default action(s)
break;
TASK
This program takes 2 number as input using ReadLine ()
function. As we have said earlier, ReadLine () function only
takes input as string datatype. So, to calculate real numbers we
need to use datatype such as double, float etc.
To convert to double we can use the ToDouble () function and to
convert to float use ToSingle () function.
Then we have to take another input that is the operator. Because
we have to specify which type of operation we want to perform.
21
22
SOLUTION
23
24
Action is performed
Then the loop condition is tested
Loop must be run though once
Always uses brackets ({) to prevent confusion
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
1
2
3
4
5
Outline
DoWhileLoop.cs
class DoWhileLoop
{
static void Main( string[] args )
{
int counter = 1;
do
{
Console.WriteLine( counter );
counter++;
} while ( counter <= 5 );
} // end method Main
Program Output
25
26
action(s)
true
condition
false
27
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
Outline
BreakTest.cs
class BreakTest
{
static void Main(
) goes
A loopstring[]
that startsargs
at one,
{
to ten, and increments by one
string output = "";
int count;
for ( count = 1; count <= 10; count++ )
{
if ( count == 5 )
If //
count
= 5remaining
then breakcode
out of
loop
break;
skip
inthe
loop
Displays a message
// if count == 5
" ";
28
Outline
BreakTest.cs
Program Output
29
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
ContinueTest.cs
class ContinueTest
A loop that starts at 1, goes
{
to 10, and increments by 1
static void Main( string[] args )If count = 5 then continue looping causing
{
the program to skip the rest of the loop
string output = "";
for ( int count = 1; count <= 10; count++ )
{
if ( count == 5 )
Create
a message
box that
the output, has
continue;
// skip
remaining
codedisplays
in loop
only
if count
== 5 statement, uses an
the//title
using
the continue
30
Outline
ContinueTest.cs
Program Output
31
32
33
Fig. 5.17
expression1 &&
expression2
false
false
false
true
false
true
false
true
Truth table for the && (logical AND) operator.
expression1
false
false
true
true
expression2
expression2
expression1 ||
expression2
false
true
true
true
false
true
false
true
Truth table for the || (logical OR) operator.
34
expression2
expression1 ^
expression2
false
false
true
true
false
true
true
false
Truth table for the logical exclusive OR (^) operator.
expression
false
True
Fig. 5.19
!expression
true
false
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
Outline
LogicalOperators.cs
class LogicalOperators
Outputs a truth table for the
{
// main entry point for application
conditional AND operator (&&)
static void Main( string[] args )
{
// testing the conditional AND operator (&&)
Console.WriteLine( "Conditional AND (&&)" +
"\nfalse && false: " + ( false && false ) +Only true if both inputs are true
"\nfalse && true: " + ( false && true ) +
"\ntrue && false: " + ( true && false ) +
"\ntrue && true:
" + ( true && true ) );
// testing the conditional OR operator (||)
Console.WriteLine( "\n\nConditional OR (||)" +
"\nfalse || false: " + ( false || false ) +
"\nfalse || true: " + ( false || true ) +
"\ntrue || false: " + ( true || false ) +
"\ntrue || true:
" + ( true || true ) );
// testing the logical AND operator (&)
Console.WriteLine( "\n\nLogical AND (&)" +
"\nfalse & false: " + ( false & false ) +
"\nfalse & true: " + ( false & true ) +
"\ntrue & false: " + ( true & false ) +
"\ntrue & true:
" + ( true & true ) );
35
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
Conditional AND
false && false:
false && true:
true && false:
true && true:
(&&)
False
False
False
True
Conditional OR (||)
false || false: False
false || true: True
true || false: True
true || true:
True
Program Output
36
Outline
LogicalOperators.cs
Program Output
False
True
True
True
37
38
39
Selection (3 ways)
if selection (one choice)
if/else selection (two choices)
switch statement (multiple choices)
Repetition (4 ways)
while structure
do/while structure
for structure
foreach structure (chapter 7)
40
Associativity
Type
()
++ -++ -- + - ! (type)
left to right
right to left
right to left
parentheses
unary postfix
unary prefix
left to right
multiplicative
left to right
additive
<
<= >
left to right
relational
== !=
left to right
equality
&
left to right
logical AND
left to right
logical exclusive OR
left to right
logical inclusive OR
&&
left to right
conditional AND
||
left to right
conditional OR
?:
right to left
conditional
right to left
assignment
>=
+= -= *= /= %=
41
.
.
Fig. 3.22 C#s single-entry/single-exit sequence, selection and repetition structures. (part 1)
42
Selection
else/if structure
(double selection)
T
break
F
T
break
.
.
if structure
(single selection)
T
break
F
break
Fig. 3.22 C#s single-entry/single-exit sequence, selection and repetition structures. (part 2)
43
while structure
T
F
T
do/while structure
T
F
Fig. 3.22 C#s single-entry/single-exit sequence, selection and repetition structures. (part 3)
44
Fig. 5.23
45
46
Rule 2
Rule 2
Rule 2
.
.
.
Fig. 3.25 Repeatedly applying rule 2 of Fig. 3.23 to the simplest flowchart.
47
Rule 3
48
49