make barcode with vb.net Iteration (Looping) Statements in Visual C#

Generation Code 128 Code Set C in Visual C# Iteration (Looping) Statements

Iteration (Looping) Statements
Code 128 Code Set A Drawer In Visual C#
Using Barcode drawer for .NET framework Control to generate, create USS Code 128 image in .NET framework applications.
www.OnBarcode.com
Decode Code 128B In Visual C#
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
There are many situations in which you will want to do the same thing again and again, perhaps slightly changing a value each time you repeat the action. This is called iteration, or looping. Typically, you ll iterate (or loop) over a set of items, taking the same action on each item in the collection. This is the programming equivalent of an assembly line. On an assembly line, you might take 100 car bodies and put a windshield on each one as it comes by. In an iterative program, you might work your way through a collection of text boxes on a form, retrieving the value from each in turn and using those values to update a database. C# provides an extensive suite of iteration statements, including for and while, and also do...while and foreach loops. You can also create a loop by using the goto statement. In the remainder of this chapter, we ll consider the use of goto, for, while, and do...while. However, we ll postpone coverage of foreach until 10, until after we ve introduced you to arrays.
Generate QR Code ISO/IEC18004 In Visual C#
Using Barcode encoder for .NET framework Control to generate, create QR Code 2d barcode image in Visual Studio .NET applications.
www.OnBarcode.com
Draw Matrix In C#.NET
Using Barcode printer for .NET Control to generate, create 2D image in .NET framework applications.
www.OnBarcode.com
|
UCC-128 Printer In Visual C#.NET
Using Barcode encoder for VS .NET Control to generate, create UCC.EAN - 128 image in .NET framework applications.
www.OnBarcode.com
Generating UPCA In Visual C#
Using Barcode creator for Visual Studio .NET Control to generate, create UPC-A Supplement 2 image in Visual Studio .NET applications.
www.OnBarcode.com
5: Branching
Linear Printer In Visual C#
Using Barcode generator for VS .NET Control to generate, create Linear image in .NET applications.
www.OnBarcode.com
ISSN - 13 Creation In C#.NET
Using Barcode creation for .NET framework Control to generate, create ISSN image in Visual Studio .NET applications.
www.OnBarcode.com
Creating Loops with goto
Code 128 Code Set B Encoder In C#.NET
Using Barcode drawer for Visual Studio .NET Control to generate, create Code 128 Code Set B image in .NET framework applications.
www.OnBarcode.com
Painting Code 128 Code Set A In None
Using Barcode encoder for Software Control to generate, create Code-128 image in Software applications.
www.OnBarcode.com
We used the goto statement earlier in this chapter as an unconditional branch in a switch statement. The more common use of goto, however, is to create a loop. In fact, the goto statement is the seed from which all other looping statements have been germinated. Unfortunately, it is a semolina seed, producer of spaghetti code (see the Spaghetti Code sidebar) and endless confusion.
PDF417 Maker In None
Using Barcode creator for Software Control to generate, create PDF 417 image in Software applications.
www.OnBarcode.com
Paint USS-128 In None
Using Barcode creator for Excel Control to generate, create EAN / UCC - 13 image in Office Excel applications.
www.OnBarcode.com
Spaghetti Code
Creating EAN / UCC - 14 In Java
Using Barcode generation for BIRT Control to generate, create GS1 128 image in BIRT reports applications.
www.OnBarcode.com
UPC A Generator In Visual Studio .NET
Using Barcode encoder for ASP.NET Control to generate, create Universal Product Code version A image in ASP.NET applications.
www.OnBarcode.com
goto can cause your method to loop back and forth in ways that are difficult to follow.
Printing PDF-417 2d Barcode In None
Using Barcode encoder for Font Control to generate, create PDF-417 2d barcode image in Font applications.
www.OnBarcode.com
Barcode Creation In None
Using Barcode printer for Font Control to generate, create Barcode image in Font applications.
www.OnBarcode.com
If you were to try to draw the flow of control in a program that makes extensive use of
Paint USS-128 In Visual Studio .NET
Using Barcode generation for Reporting Service Control to generate, create EAN / UCC - 14 image in Reporting Service applications.
www.OnBarcode.com
Barcode Drawer In Java
Using Barcode printer for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
goto statements, the resulting morass of intersecting and overlapping lines might look
Paint Denso QR Bar Code In Java
Using Barcode encoder for Android Control to generate, create QR Code image in Android applications.
www.OnBarcode.com
GS1 - 12 Encoder In Objective-C
Using Barcode printer for iPad Control to generate, create GS1 - 12 image in iPad applications.
www.OnBarcode.com
like a plate of spaghetti hence the term spaghetti code. Spaghetti code is a contemptuous epithet; no one wants to write spaghetti code, and so most experienced programmers avoid using goto to create loops.
Because of the problems created by the goto statement, it is rarely used in C# outside of switch statements, but in the interest of completeness, here s how you create goto loops: 1. Create a label. 2. goto that label. The label is an identifier followed by a colon. You place the label in your code, and then you use the goto keyword to jump to that label. The goto command is typically tied to an if statement, as illustrated in Example 5-11.
using using using using System; System.Collections.Generic; System.Linq; System.Text;
namespace Example_5_11_ _ _ _The_goto_Statement { class Program { public static void Main( ) { int counterVariable = 0; repeat: // the label
Iteration (Looping) Statements |
Console.WriteLine("counterVariable: {0}", counterVariable); // increment the counter counterVariable++; if (counterVariable < 10) { goto repeat; // the dastardly deed } } } }
The output looks like this:
counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: 0 1 2 3 4 5 6 7 8 9
This code is not terribly complex; you ve used only a single goto statement. However, with multiple such statements and labels scattered through your code, tracing the flow of execution becomes very difficult. Back in the days before compilers, goto was the only option for branching, but it resulted in some unsightly code. It was the phenomenon of spaghetti code that led to the creation of alternatives, such as the while loop.
The while Loop
The semantics of the while loop are While this condition is true, do this work. The syntax is:
while (Boolean expression) statement
As usual, the Boolean expression is any expression that evaluates to true or false. The statement executed within the while statement can of course be a block of statements within braces. Example 5-12 illustrates the use of the while loop.
|
5: Branching
using using using using System; System.Collections.Generic; System.Linq; System.Text;
namespace Example_5_12_ _ _ _The_while_Loop { class Program { public static void Main( ) { int counterVariable = 0; // while the counter variable is less than 10 // print out its value while (counterVariable < 10) { Console.WriteLine("counterVariable: {0}", counterVariable); counterVariable++; } } } }
The output looks like this:
counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: counterVariable: 0 1 2 3 4 5 6 7 8 9
The code in Example 5-12 produces results identical to the code in Example 5-11, but the logic is a bit clearer. The while statement is nicely self-contained, and it reads like an English sentence: while counterVariable is less than 10, print this message and increment counterVariable. Notice that the while loop tests the value of counterVariable before entering the loop. This ensures that the loop will not run if the condition tested is false. Thus, if counterVariable is initialized to 11, the loop will never run.
Copyright © OnBarcode.com . All rights reserved.