PART I
C h a p t e r 2 :
A n O v e r v i e w o f C #
31
PART IPART I
for(i=1; i <= 10; i++) {
sum = sum + i;
prod = prod * i;
}
Console.WriteLine("Sum is " + sum);
Console.WriteLine("Product is " + prod);
}
}
The output is shown here:
Sum is 55
Product is 3628800
Here, the block enables one loop to compute both the sum and the product. Without the use
of the block, two separate
for
loops would have been required.
One last point: Code blocks do not introduce any runtime inefficiencies. In other words,
the
{
and
}
do not consume any extra time during the execution of a program. In fact, because
of their ability to simplify (and clarify) the coding of certain algorithms, the use of code blocks
generally results in increased speed and efficiency.
Do'stlaringiz bilan baham: |