Download presentation
Presentation is loading. Please wait.
Published byDoreen Lyons Modified over 8 years ago
1
1
2
2
3
3 https://en.wikipedia.org/wiki/Floating_point
4
for (float i = -1; i < 1; i +=.1F) { Console.WriteLine(i); } Console.ReadKey (); 4
5
5
6
The initialization, loop-continuation condition and increment portions of a for statement can contain arithmetic expressions. For example, assume that x = 2 and y = 10; if x and y are not modified in the body of the loop, then the statement for ( int j = x; j <= 4 * x * y; j += y / x ) is equivalent to the statement for ( int j = 2; j <= 80; j += 5 ) If the loop-continuation condition is initially false, the app does not execute the for statement’s body. 6
7
7
8
8
9
9
10
10
11
11
12
12
13
13
14
14 The Compound Interest Equation P = C (1 + r/n) nt where P = future value C = initial deposit r = interest rate (expressed as a fraction: eg. 0.06) n = # of times per year interest in compounded t = number of years invested Simplified Compound Interest Equation When interest is only compounded once per year (n=1), the equation simplifies to: P = C (1 + r) t
15
App: Compound-Interest Calculations Consider the following problem: A person invests $1,000 in a savings account yielding 5% interest, compounded yearly. Calculate and display the amount of money in the account at the end of each year for 10 years. a = p (1 + r ) n p is the original amount invested (i.e., the principal) r is the annual interest rate (use 0.05 for 5%) n is the number of years a is the amount on deposit at the end of the nth year. The app shown in Fig. 6.6 uses a loop that performs the calculation for each of the 10 years the money remains on deposit. 15
16
// Fig. 6.6: Interest.cs // Compound-interest calculations with for. using System; public class Interest { public static void Main( string[] args ) { decimal amount; // amount on deposit at end of each year decimal principal = 1000; // initial amount before interest double rate = 0.05; // interest rate // display headers Console.WriteLine( "Year{0,20}", "Amount on deposit" ); // calculate amount on deposit for each of ten years for ( int year = 1; year <= 10; year++ ) { // calculate new amount for specified year amount = principal * ( ( decimal ) Math.Pow( 1.0 + rate, year ) ); // display the year and the amount Console.WriteLine( "{0,4}{1,20:C}", year, amount ); } // end for } // end Main } 16
17
17
18
18
19
The initialization and increment expressions can be comma-separated lists that enable you to use multiple initialization expressions or multiple increment expressions: for (int number = 2; number <= 20; total += number, number += 2 ) { ; // empty statement } 19
20
Many classes provide static methods that cannot be called on objects—they must be called using a class name. ClassName.methodName ( arguments ) Console methods Write and WriteLine are static methods. Math.Pow(x, y) calculates the value of x raised to the yth power. 20
21
21
22
22
23
The switch multiple-selection statement performs different actions based on the value of an expression. Each action is associated with the value of a constant integral expression or a constant string expression that the expression may assume. A switch statement determines whether each grade is the equivalent of an A, B, C, D or F. 23
24
24
25
25
26
26
27
27
28
28
29
29
30
30
31
31
32
32
33
33
34
34
35
35
36
36
37
37
38
38
39
39
40
40
41
41
42
42
43
43
44
44
45
45
46
46
47
47
48
48
49
49
50
50
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.