Free mag vol1 | Page 180

CHAPTER 3  CORE C# PROGRAMMING CONSTRUCTS, PART I } Console.Write("Values in subset: "); foreach (var i in subset) { Console.Write("{0} ", i); } The while and do/while Looping Constructs The while looping construct is useful should you wish to execute a block of statements until some terminating condition has been reached. Within the scope of a while loop, you will need to ensure this terminating event is indeed established; otherwise, you will be stuck in an endless loop. In the following example, the message "In while loop" will be continuously printed until the user terminates the loop by entering yes at the command prompt: static void WhileLoopExample() { string userIsDone = ""; } // Test on a lower-class copy of the string. while(userIsDone.ToLower() != "yes") { Console.WriteLine("In while loop"); Console.Write("Are you done? [yes] [no]: "); userIsDone = Console.ReadLine(); } Closely related to the while loop is the do/while statement. Like a simple while loop, do/while is used when you need to perform some action an undetermined number of times. The difference is that do/while loops are guaranteed to execute the corresponding block of code at least once. In contrast, it is possible that a simple while loop may never execute if the terminating condition is false from the onset. static void DoWhileLoopExample() { string userIsDone = ""; do { Console.WriteLine("In do/while loop"); Console.Write("Are you done? [yes] [no]: "); userIsDone = Console.ReadLine(); }while(userIsDone.ToLower() != "yes"); // Note the semicolon! } Decision Constructs and the Relational/Equality Operators Now that you can iterate over a block of statements, the next related concept is how to control the flow of program execution. C# defines two simple constructs to alter the flow of your program, based on various contingencies: 114