c# net qr code generator Breaking Out of a Loop in Visual C#.NET

Drawer QR Code in Visual C#.NET Breaking Out of a Loop

Breaking Out of a Loop
QR Code JIS X 0510 Drawer In C#.NET
Using Barcode creation for .NET Control to generate, create QR Code ISO/IEC18004 image in .NET applications.
www.OnBarcode.com
Scan QR Code 2d Barcode In Visual C#
Using Barcode decoder for .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
It can sometimes be useful to abandon a loop earlier than its natural end. In the case of a foreach loop, this might mean stopping before you ve processed every item in the collection. With for or while loops, you get to write the loop condition so that you can stop under whatever conditions you like, but it can sometimes be more convenient to put the code that makes a decision to abandon a loop somewhere inside the loop body rather than in the condition. For these eventualities, C# provides the break keyword.
Linear Barcode Generation In C#.NET
Using Barcode printer for .NET Control to generate, create 1D Barcode image in .NET applications.
www.OnBarcode.com
Make Barcode In Visual C#.NET
Using Barcode drawer for VS .NET Control to generate, create Barcode image in .NET framework applications.
www.OnBarcode.com
We saw break already in a switch statement in Example 2-11 we used it to say that we re done with the switch and want to break out of that statement. The break keyword does the same thing in a loop:
QR Code Maker In Visual C#
Using Barcode maker for .NET Control to generate, create QR Code JIS X 0510 image in .NET framework applications.
www.OnBarcode.com
Encoding GS1 128 In C#.NET
Using Barcode maker for .NET Control to generate, create EAN 128 image in Visual Studio .NET applications.
www.OnBarcode.com
using (StreamReader times = File.OpenText("LapTimes.txt")) { while (!times.EndOfStream) { string line = times.ReadLine(); if (line == "STOP!") { break; } double lapEndTime = double.Parse(line); Console.WriteLine(lapEndTime); } }
Make PDF417 In Visual C#.NET
Using Barcode maker for VS .NET Control to generate, create PDF417 image in .NET applications.
www.OnBarcode.com
OneCode Creator In Visual C#.NET
Using Barcode generator for VS .NET Control to generate, create 4-State Customer Barcode image in VS .NET applications.
www.OnBarcode.com
This is the loop from Example 2-16, modified to stop if it comes across a line in the input file that contains the text STOP! This breaks out immediately, abandoning the rest of the loop and leaping straight to the first line of code after the enclosing loop s closing brace. (In that case, this happens to be the enclosing using statement s closing brace, which will close the file handle.)
Recognizing QR Code In VS .NET
Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Draw QR Code In None
Using Barcode encoder for Software Control to generate, create Quick Response Code image in Software applications.
www.OnBarcode.com
Some people regard this use of break as bad practice. It makes it harder to understand the loop. When a loop contains no break statements, you can understand its lifetime by looking at the while (or for, or foreach) part. But if there are break statements, you need to look at more of the code to get a complete understanding of when the loop will finish. More generally, flow control that jumps suddenly out of the middle of a construct is frowned upon, because it makes it much harder for someone to understand how execution flows through a program, and programs that are hard to understand tend to be buggy. The computer scientist Edsger Dijkstra submitted a short letter on this topic in 1968 to an academic journal, which was printed under a now infamous heading, Go-to statement considered harmful . If you re interested in iconic pieces of computing history, or if you d like a detailed explanation of exactly why this sort of jumpy flow control is problematic, you can find the original letter at http://www.cs.utexas.edu/users/EWD/ewd02xx/ EWD215.PDF.
Barcode Decoder In C#.NET
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
Reading PDF 417 In None
Using Barcode recognizer for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
To recap what we ve explored so far, we ve seen how to work with variables to hold information, how to write expressions that perform calculations, how to use selection statements that decide what to do, and how to build iteration statements that can do things repeatedly. There s one more basic C# programming feature we need to look at to cover the most important everyday coding features: methods.
Making UPCA In VS .NET
Using Barcode generation for Reporting Service Control to generate, create UPC A image in Reporting Service applications.
www.OnBarcode.com
Create Matrix 2D Barcode In Visual Studio .NET
Using Barcode creator for .NET framework Control to generate, create 2D Barcode image in VS .NET applications.
www.OnBarcode.com
Methods
Decoding Barcode In Java
Using Barcode Control SDK for Java Control to generate, create, read, scan barcode image in Java applications.
www.OnBarcode.com
Barcode Generation In Visual Basic .NET
Using Barcode drawer for VS .NET Control to generate, create Barcode image in .NET applications.
www.OnBarcode.com
As we saw earlier, a method is a named block of code. We wrote a method already the Main method that runs when our program starts. And we used methods provided by the .NET Framework class library, such as Console.WriteLine and File.ReadAll Lines. But we haven t looked at how and why you would introduce new methods other than Main into your own code. Methods are an essential mechanism for reducing your code s complexity and enhancing its readability. By putting a section of code into its own method with a carefully chosen name that describes what the method does, you can make it much easier for someone looking at the code to work out what your program is meant to do. Also, methods can help avoid repetition if you need to do similar work in multiple places, a method can help you reuse code. In our race car example, there s a job we may need to do multiple times: reading in numeric values from a file. We did this for timing information, but we re going to need to do the same with fuel consumption and distance. Rather than writing three almost identical bits of code, we can put the majority of the code into a single method. The first thing we need to do is declare the method we need to pick a name, define the information that comes into the method, and optionally define the information that comes back out. Let s call the method ReadNumbersFromFile, since that s what it s going to do. Its input will be a text string containing the filename, and it will return an array of double-precision floating-point numbers. The method declaration, which will go inside our Program class, will look like this:
Encoding QR In None
Using Barcode encoder for Excel Control to generate, create QR image in Microsoft Excel applications.
www.OnBarcode.com
Generate UPC - 13 In Java
Using Barcode generation for Android Control to generate, create GTIN - 13 image in Android applications.
www.OnBarcode.com
static double[] ReadNumbersFromFile(string fileName)
Barcode Maker In Java
Using Barcode generation for BIRT reports Control to generate, create Barcode image in BIRT applications.
www.OnBarcode.com
Drawing QR-Code In Java
Using Barcode printer for Java Control to generate, create QR Code ISO/IEC18004 image in Java applications.
www.OnBarcode.com
As you may recall from the discussion of Main earlier, the static keyword indicates that we do not need an instance of the containing Program type to be created for this method to run. (We ll be looking at nonstatic methods in the next chapter when we start dealing with objects.) C# follows the C-family convention that the kind of data coming out of the method is specified before the name and the inputs, so next we have double[], indicating that this method returns an array of numbers. Then we have the name, and then in parentheses, the inputs required by this method. In this example there s just one, the filename, but this would be a comma-separated list if more inputs were required. After the method declaration comes the method body the statements that make up the method, enclosed in braces. The code isn t going to be quite the same as what we ve seen so far up until now, we ve converted the text to numbers one at a time immediately before processing them. But this code is going to return an array of numbers, just like File.ReadAllLines returns an array of strings. So our code needs to build up that array. Example 2-17 shows one way of doing this.
static double[] ReadNumbersFromFile(string fileName) { List<double> numbers = new List<double>(); using (StreamReader file = File.OpenText(fileName)) { while (!file.EndOfStream) { string line = file.ReadLine(); // Skip blank lines if (!string.IsNullOrEmpty(line)) { numbers.Add(double.Parse(line)); } } } return numbers.ToArray(); }
This looks pretty similar to the example while loop we saw earlier, with one addition: we re creating an object that lets us build up a collection of numbers one at a time a List<double>. It s similar to an array (a double[]), but an array needs you to know how many items you want up front you can t add more items onto an existing array. The advantage of a List<double> is that you can just keep adding new numbers at will. That matters here because if you look closely you ll see we ve modified the code to skip over blank lines, which means that we actually don t know how many numbers we re going to get until we ve read the whole file. Once you re done adding numbers to a list, you can call its ToArray() method to get an array of the correct size. This list class is an example of a collection class. .NET offers several of these, and they are so extremely useful that s 7, 8, and 9 are related to working with collections. Notice the return keyword near the end of Example 2-17. This is how we return the information calculated by our method to whatever code calls the method. As well as specifying the value to return, the return keyword causes the current method to exit immediately, and for execution to continue back in the calling method. (In methods with a void return type, which do not return any value, you can use the return keyword without an argument to exit the method. Or you can just let execution run to the end of the method, and it will return implicitly.) If you re wondering how the method remembers where it s supposed to go back to, see the sidebar on the next page. With the ReadNumbersFromFile method in place, we can now write this sort of code:
double[] lapTimes = ReadNumbersFromFile("LapTimes.txt"); double[] fuelLevels = ReadNumbersFromFile("FuelRemainingByLap.txt");
Copyright © OnBarcode.com . All rights reserved.