Professional Documents
Culture Documents
Chapter 5c
Chapter 5c
Iteration
Void Functions
int i;
for (i=2;i<11;i=i+2)
{
Console.WriteLine(“the number is {0}“,i);
}
int i = 20;
while (i > 0)
{
Console.WriteLine(“ i now equals {0}“,i);
i = i – 5;
}
do
{
statement1;
statement2;
statement3;
}
while (conditional expression)
Using this format of the while, the condition is tested
AFTER the commands are executed.
This means that the statements are executed at
least one time regardless of the conditional.
Post-Test vs. Pre-Test while Loop
int i = 0; int i = 0;
do while (i > 0)
{ {
Console.WriteLine (“ i now Console.WriteLine (“ i now
equals {0}, i); equals {0}, i);
i = i – 5; i = i – 5;
} }
while (i > 0)