visula c# programming lecture 4
DESCRIPTION
lecture 4TRANSCRIPT
Lecture #04:
Loops
2
C# Control Structures: Repetition
T
F
while structure
T
F
do/while structureF
T
for structure/foreach structure
3
while Statement
The while statement has the following syntax:
while ( condition ) statement;
whilewhile is a is areserved wordreserved word
If the condition is true, the statement is executed.If the condition is true, the statement is executed.Then the condition is evaluated again.Then the condition is evaluated again.
The statement (or a block of statements) is executed The statement (or a block of statements) is executed repetitively until the condition becomes false. repetitively until the condition becomes false.
4
while Statement (cont’d)
true
false
Product = 2 * productProduct <= 1000
int product;
product = 2;
while (product <= 1000)
{
product = 2 * product;
}
// beginning of the next statement
5
while Statement
Note that if the condition of a while statement is false initially, the statement is never executed
Therefore, the body of a while loop will execute zero or more times
6
Infinite Loops
The body of a while loop must eventually make the condition false
If not, it is an infinite loop, which will execute until the user interrupts the program
This is a common type of logical error You should always double check to ensure that
your loops will terminate normally
7
Example 1: Counter Controlled While Loop
Control variable• The variable used as a counter to determine
whether or not the loop should continue
Three components• Initial value of the counter• Check whether or not the counter has reached
target– When the looping should continue
• Incrementing/decrementing of the counter
8
Example 2: Sentinel Controlled while Loops
This is typical of an input-driven program
Continues an arbitrary amount of times
Sentinel value• Causes loop to break• Avoid collisions
– When flag value = user entered value
9
The do Statement
The do statement has the following syntax:
do{ statement;}while ( condition );
Uses bothUses boththe the dodo and andwhilewhile
reservedreservedwordswords
The statement is executed once initially, then the condition is evaluatedThe statement is executed once initially, then the condition is evaluated
The statement is repetitively executed until the condition becomes falseThe statement is repetitively executed until the condition becomes false
10
do/while Flowchart
true
false
action(s)
condition
Fig. 5.13 Flowcharting the do/while repetition structure.
11
Comparing the while and do Loops
The while loops vs. the do/while loops Using a while loop
• Condition is tested• The action is performed• Loop could be skipped altogether
Using a do/while loop• Action is performed• Then the loop condition is tested• Loop will be run at least once
T
F
while structure
T
F
do/while structure
Question: write a program to get max from user and then print the numbers from 1 to max
12
The for Statement
The for statement has the following syntax:
for ( initialization ; condition ; increment ) statement;
ReservedReservedwordword
The The initializationinitialization portion portionis executed onceis executed once
before the loop beginsbefore the loop begins
The statement isThe statement isexecuted until theexecuted until the
conditioncondition becomes false becomes false
The The incrementincrement portion is executed at the end of each iteration portion is executed at the end of each iteration
13
Flowchart of a for loop
increment
condition action(s)true
false
initialization
for ( initialization ; condition ; increment ) action(s);
14
The for Statement: Example
counter++
Establish initial value of control variable.
Determine if final value of control variable has been reached.
counter <= 10
Console.WriteLine( counter * 10 );
true
false
int counter = 1
Body of loop (this may be multiple statements)
Increment the control variable.
for (int counter = 1; counter <= 10; counter++)
Console.WriteLine (counter * 10);
// beginning of the next statement
15
The for Statement
A for loop is equivalent to the following while loop:
initialization;while ( condition ){ statement; increment;}
16
The for Statement
It is well suited for executing a specific number of times that can be determined in advance Increment/Decrement
• When incrementing– In most cases < or <= is used
• When decrementing– In most cases > or >= is used
17
The flexibility of the for Statement Each expression in the header of a for loop is
optional If the initialization is left out, no initialization is
performed If the condition is left out, it is always considered to be
true, and therefore creates an infinite loop If the increment is left out, no increment operation is
performed
Both semi-colons are always required in the for loop header
for ( ; ; ){
// do something
}
18
A Problem to Think About
How to print this?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
What about this?
xxxxxxxxxxxxxxx xxxxxxxxxxxxx
xxxxxxxxxxx xxxxxxxxx xxxxxxx xxxxx xxx x
19
Statements break and continue
Used to alter the flow of control The break statement
• Used to exit a loop early
The continue statement• Used to skip the rest of the statements in a loop
and restart at the first statement in the loop
Programs can be completed without their usage; use with caution.