c# control statements part 2
DESCRIPTION
C# Control Statements part 2. 6.2 Essentials of Counter-Controlled Repetition. 6.3 for Repetition Statement. 6.3 for Repetition Statement (Cont.). The initialization, loop-continuation condition and increment portions of a for statement can contain arithmetic expressions. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/1.jpg)
C# Control Statements part 2
1
![Page 2: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/2.jpg)
6.2 Essentials of Counter-Controlled Repetition
2
![Page 3: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/3.jpg)
3
![Page 4: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/4.jpg)
6.3 for Repetition Statement
4
![Page 5: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/5.jpg)
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 statementfor ( 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.3 for Repetition Statement (Cont.)
5
![Page 6: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/6.jpg)
6
![Page 7: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/7.jpg)
7
![Page 8: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/8.jpg)
8
![Page 9: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/9.jpg)
9
![Page 10: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/10.jpg)
6.4 Examples Using the for Statement
10
![Page 11: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/11.jpg)
11
![Page 12: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/12.jpg)
// 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}
12
![Page 13: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/13.jpg)
13
![Page 14: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/14.jpg)
14
![Page 15: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/15.jpg)
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
6.4 Examples Using the for Statement (Cont.)
15
![Page 16: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/16.jpg)
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 yearsa 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.
6.4 Examples Using the for Statement (Cont.)
16
![Page 17: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/17.jpg)
17
![Page 18: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/18.jpg)
18
![Page 19: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/19.jpg)
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.
6.4 Examples Using the for Statement (Cont.)
19
![Page 20: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/20.jpg)
6.5 do…while Repetition Statement
20
![Page 21: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/21.jpg)
21
![Page 22: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/22.jpg)
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.
Figure 6.9 contains an enhanced version of the GradeBook class.
A switch statement determines whether each grade is the equivalent of an A, B, C, D or F.
6.6 switch Multiple-Selection Statement
22
![Page 23: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/23.jpg)
switch
23
![Page 24: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/24.jpg)
6.6 switch Multiple-Selection Statement
24
![Page 25: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/25.jpg)
25
![Page 26: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/26.jpg)
26
![Page 27: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/27.jpg)
27
![Page 28: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/28.jpg)
28
![Page 29: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/29.jpg)
29
![Page 30: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/30.jpg)
30
![Page 31: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/31.jpg)
31
![Page 32: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/32.jpg)
32
![Page 33: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/33.jpg)
6.7 break and continue Statements
33
![Page 34: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/34.jpg)
34
![Page 35: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/35.jpg)
35
![Page 36: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/36.jpg)
6.8 Logical Operators
36
![Page 37: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/37.jpg)
37
![Page 38: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/38.jpg)
38
![Page 39: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/39.jpg)
39
![Page 40: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/40.jpg)
40
![Page 41: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/41.jpg)
41
![Page 42: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/42.jpg)
42
![Page 43: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/43.jpg)
43
![Page 44: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/44.jpg)
6.9 Structured-Programming Summary
44
![Page 45: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/45.jpg)
45
![Page 46: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/46.jpg)
46
![Page 47: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/47.jpg)
47
![Page 48: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/48.jpg)
48
![Page 49: C# Control Statements part 2](https://reader034.vdocuments.mx/reader034/viewer/2022050805/568134be550346895d9bdfcb/html5/thumbnails/49.jpg)
49