Skip to content

Commit 9276b98

Browse files
committed
program flow (loop)
1 parent f24e476 commit 9276b98

File tree

11 files changed

+100
-32
lines changed
  • LISTING 1-49 cancel a task/LISTING 1-49 cancel a task
  • LISTING 1-50 cancel with exception/LISTING 1-50 cancel with exception
  • LISTING 1-51 unsafe thread method/LISTING 1-51 unsafe thread method
  • LISTING 1-52 while loops/LISTING 1-52 while loops
  • LISTING 1-53 do while loops/LISTING 1-53 do while loops
  • LISTING 1-54 for loops/LISTING 1-54 for loops
  • LISTING 1-55 iterate with for/LISTING 1-55 iterate with for
  • LISTING 1-56 iterate with foreach/LISTING 1-56 iterate with foreach
  • LISTING 1-57 uppercase Person/LISTING 1-57 uppercase Person
  • LISTING 1-58 using break/LISTING 1-58 using break
  • LISTING 1-59 using continue/LISTING 1-59 using continue

11 files changed

+100
-32
lines changed

LISTING 1-49 cancel a task/LISTING 1-49 cancel a task/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,4 @@ Clock stopped.
4949
*/
5050
}
5151
}
52-
}
52+
}

LISTING 1-50 cancel with exception/LISTING 1-50 cancel with exception/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,4 @@ Clock task completed.
9898
*/
9999
}
100100
}
101-
}
101+
}

LISTING 1-51 unsafe thread method/LISTING 1-51 unsafe thread method/Program.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,4 @@ static void Main(string[] args)
3434
*/
3535
}
3636
}
37-
}
37+
}

LISTING 1-52 while loops/LISTING 1-52 while loops/Program.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace LISTING_1_52_while_loops
84
{
5+
/*
6+
7+
The condition that controls the looping behavior is tested before the statements controlled by the loop are obeyed.
8+
9+
A while loop is very effective when creating a consumer of data.
10+
11+
*/
912
class Program
1013
{
1114
static void Main(string[] args)
@@ -18,7 +21,7 @@ static void Main(string[] args)
1821
int count = 0;
1922
while(count < 10)
2023
{
21-
Console.WriteLine("Hello {0}", count);
24+
Console.WriteLine($"Hello {count}");
2225
count = count + 1;
2326
}
2427

@@ -28,4 +31,4 @@ static void Main(string[] args)
2831
}
2932
}
3033
}
31-
}
34+
}

LISTING 1-53 do while loops/LISTING 1-53 do while loops/Program.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
namespace LISTING_1_53_do_while_loops
44
{
5+
/*
6+
7+
The important thing to note about this code is that although the logical expression contolling it is false, which
8+
means that the loop will never repeat, the message Hello will be printed once, since the printing takes place
9+
before the logical expression is tested.
10+
11+
A do-while construction is useful when you want to create code that continuously fetches data until a(n) (in)valid
12+
value is entered.
13+
14+
*/
515
class Program
616
{
717
static void Main(string[] args)
@@ -12,4 +22,4 @@ static void Main(string[] args)
1222
} while (false);
1323
}
1424
}
15-
}
25+
}

LISTING 1-54 for loops/LISTING 1-54 for loops/Program.cs

+15-6
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,43 @@
22

33
namespace LISTING_1_54_for_loops
44
{
5+
/*
6+
7+
A loop that is not infinite (one that should terminate at some point) can be made up of three things:
8+
9+
1. Initialization that is performed to set up the loop.
10+
2. A test that will determine if the loop should continue.
11+
3. An update to be performed each time the action of the loop has been performed.
12+
13+
*/
514
class Program
615
{
716
static int counter;
817

918
static void initalize()
1019
{
11-
Console.WriteLine("Initialize called");
20+
Console.WriteLine("Initialize called.");
1221
counter = 0;
1322
}
1423

1524
static void update()
1625
{
17-
Console.WriteLine("Update called");
26+
Console.WriteLine("Update called.");
1827
counter = counter + 1;
1928
}
2029

2130
static bool test()
2231
{
23-
Console.WriteLine("Test called");
32+
Console.WriteLine("Test called.");
2433
return counter < 5;
2534
}
2635
static void Main(string[] args)
2736
{
28-
for(initalize(); test(); update())
37+
for (initalize(); test(); update())
2938
{
30-
Console.WriteLine("Hello {0}", counter);
39+
Console.WriteLine($"Hello {counter}.");
3140
}
3241
Console.ReadKey();
3342
}
3443
}
35-
}
44+
}

LISTING 1-55 iterate with for/LISTING 1-55 iterate with for/Program.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ static void Main(string[] args)
99
string[] names = { "Rob", "Mary", "David", "Jenny", "Chris", "Imogen" };
1010

1111
for (int index = 0; index < names.Length; index++)
12+
{
1213
Console.WriteLine(names[index]);
14+
}
1315

1416
Console.ReadKey();
1517
}
1618
}
17-
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,32 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace LISTING_1_56_iterate_with_foreach
84
{
5+
/*
6+
7+
The iterating value must match the type of the items in the collection.
8+
9+
It isn't possible for code in a foreach construction to modigfy the iterating value.
10+
11+
*/
912
class Program
1013
{
1114
static void Main(string[] args)
1215
{
1316
string[] names = { "Rob", "Mary", "David", "Jenny", "Chris", "Imogen" };
1417

15-
foreach(string name in names)
18+
//foreach (int name in names)
19+
//Error CS0030 Cannot convert type 'string' to 'int'
20+
21+
foreach (string name in names)
1622
{
1723
Console.WriteLine(name);
24+
25+
// Error CS1656 Cannot assign to 'name' because it is a 'foreach iteration variable'
26+
//name = name.ToUpper();
1827
}
1928

2029
Console.ReadKey();
2130
}
2231
}
23-
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
62

73
namespace LISTING_1_57_uppercase_Person
84
{
5+
/*
6+
7+
If the foreach loop is working on a list of references to objects, the objects on the ends of those references can
8+
be changed.
9+
10+
The foreach construction can iterate through any object which implements the IEnumerable interface. These objects
11+
expose a method called GetIterator(). This method must return an object that implements the
12+
System.Collections.IEnumerator interface. This interface exposes methods that the foreach construction can use to
13+
get the next item from the enumerator and determine if there any more items in the collection. Many collection
14+
classes, including listst and dictionaries, implement the IEnumerable interface.
15+
16+
Note that the iteration can be implemented in a "lazy" way; the next item to be iterated only needs to be fetched
17+
when requested. The result of database queries can be returned as objects that implement the IEnumerable interface
18+
and then only fetch the actual data items when needed. It is important that the item being iterated is not changed
19+
during iteration, if the iterating code tried to remove items from the list it was iteraqting through this would
20+
cause the program to throw an exception when it ran.
21+
22+
*/
923
class Program
1024
{
1125
class Person
@@ -20,18 +34,24 @@ public Person(string name)
2034

2135
static void Main(string[] args)
2236
{
23-
Person[] people = new Person[] {
37+
Person[] people = new Person[]
38+
{
2439
new Person("Rob"), new Person("Mary"),
2540
new Person("David"), new Person("Jenny"),
26-
new Person("Chris"), new Person("Imogen") };
41+
new Person("Chris"), new Person("Imogen")
42+
};
2743

2844
foreach (Person person in people)
45+
{
2946
person.Name = person.Name.ToUpper();
47+
}
3048

3149
foreach (Person person in people)
50+
{
3251
Console.WriteLine(person.Name);
52+
}
3353

3454
Console.ReadKey();
3555
}
3656
}
37-
}
57+
}

LISTING 1-58 using break/LISTING 1-58 using break/Program.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace LISTING_1_58_using_break
44
{
5+
/*
6+
7+
When the break statement is reached, the program immediatly exits the loop.
8+
9+
*/
510
class Program
611
{
712
static void Main(string[] args)
@@ -12,10 +17,12 @@ static void Main(string[] args)
1217
{
1318
Console.WriteLine(names[index]);
1419
if (names[index] == "David")
20+
{
1521
break;
22+
}
1623
}
1724

1825
Console.ReadKey();
1926
}
2027
}
21-
}
28+
}

LISTING 1-59 using continue/LISTING 1-59 using continue/Program.cs

+9-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
namespace LISTING_1_59_using_continue
44
{
5+
/*
6+
7+
The continue statement does not cause a loop to end. Instead, it ends the current pass through the code controlled
8+
by the loop. The terminating condition is then tested to determine if the loop should continue.
9+
10+
*/
511
class Program
612
{
713
static void Main(string[] args)
@@ -11,11 +17,13 @@ static void Main(string[] args)
1117
for (int index = 0; index < names.Length; index++)
1218
{
1319
if (names[index] == "David")
20+
{
1421
continue;
22+
}
1523

1624
Console.WriteLine(names[index]);
1725
}
1826
Console.ReadKey();
1927
}
2028
}
21-
}
29+
}

0 commit comments

Comments
 (0)