CHAPTER 5
Decision and loop statements
69
int count = 1;
while (count <= 5)
{
Console::WriteLine(count * count);
count++;
}
Console::WriteLine("The end");
You must follow the while keyword with a conditional expression enclosed in parentheses. As long
as the conditional expression evaluates to true, the while body executes. After the loop body has been
executed, control returns to the while statement and the conditional expression is tested again. This
sequence continues until the test evaluates to false.
You must, of course, remember to include some kind of update statement in the loop so that it will
terminate eventually. In this example count++ is incrementing the loop counter. If you don’t provide
an update statement, the loop will iterate forever, which probably isn’t what you want.
The preceding example displays the following output:
In this exercise, you will enhance your Calendar Assistant application so that the user can type five
dates.
Do'stlaringiz bilan baham: |