vb.net barcode font 6: Controlling Your Program s Flow in Java

Generating Data Matrix 2d barcode in Java 6: Controlling Your Program s Flow

CHAPTER 6: Controlling Your Program s Flow
Make ECC200 In Java
Using Barcode drawer for Android Control to generate, create Data Matrix image in Android applications.
www.OnBarcode.com
Printing Code-128 In Java
Using Barcode creator for Android Control to generate, create Code-128 image in Android applications.
www.OnBarcode.com
NOTE
GTIN - 128 Drawer In Java
Using Barcode generator for Android Control to generate, create GS1-128 image in Android applications.
www.OnBarcode.com
Painting Barcode In Java
Using Barcode creator for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Since you can always rewrite a for loop as a while loop, why introduce the for loop at all Sometimes, a programming idea fits more naturally into the pattern of a for statement. If the for loop makes for more readable code, why not use it As you write more and more code, you ll develop a sense for when to use while and when to use for.
Draw GS1 - 12 In Java
Using Barcode maker for Android Control to generate, create UPC Symbol image in Android applications.
www.OnBarcode.com
EAN 13 Printer In Java
Using Barcode creator for Android Control to generate, create UPC - 13 image in Android applications.
www.OnBarcode.com
Here s an example of a for loop:
Printing Barcode In Java
Using Barcode generation for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
Codabar Creator In Java
Using Barcode generator for Android Control to generate, create Codabar image in Android applications.
www.OnBarcode.com
int i;
Printing ECC200 In VB.NET
Using Barcode printer for .NET framework Control to generate, create Data Matrix image in .NET applications.
www.OnBarcode.com
ECC200 Drawer In .NET
Using Barcode creation for .NET Control to generate, create Data Matrix 2d barcode image in VS .NET applications.
www.OnBarcode.com
for ( i = 1; i < 3; i++ ) printf( "Looping: %d\n", i ); printf( "We are past the for loop." );
Universal Product Code Version A Creation In Visual Basic .NET
Using Barcode generator for .NET framework Control to generate, create UPC Symbol image in .NET applications.
www.OnBarcode.com
USS-128 Recognizer In Visual C#.NET
Using Barcode decoder for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
This example is identical in functionality to the while loops presented earlier. Note the three expressions on the first line of the for loop. Before the loop is entered, the first expression is evaluated (remember, assignment statements make great expressions):
Creating Data Matrix ECC200 In None
Using Barcode generation for Office Word Control to generate, create Data Matrix image in Microsoft Word applications.
www.OnBarcode.com
Make EAN 128 In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create EAN128 image in .NET framework applications.
www.OnBarcode.com
i = 1
Print Barcode In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
QR Code Creation In Java
Using Barcode printer for BIRT reports Control to generate, create QR Code JIS X 0510 image in BIRT applications.
www.OnBarcode.com
Once the expression is evaluated, i has a value of 1. We are now ready to enter the loop. At the top of each pass through the loop, the second expression is evaluated:
Scanning ANSI/AIM Code 39 In Java
Using Barcode decoder for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Recognize UPCA In Visual Basic .NET
Using Barcode decoder for .NET framework Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
i < 3
EAN 13 Decoder In Visual Studio .NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
www.OnBarcode.com
EAN-13 Creator In None
Using Barcode creation for Online Control to generate, create EAN13 image in Online applications.
www.OnBarcode.com
If the expression evaluates to true, the loop continues. Since i is less than 3, we can proceed. Next, the statement is executed:
printf( "Looping: %d\n", i );
Here s the first line of output:
Looping: 1
Having reached the bottom of the loop, the for evaluates its third expression:
This changes the value of i to 2. We go back to the top of the loop and evaluate the termination expression:
i < 3
CHAPTER 6: Controlling Your Program s Flow
Since i is still less than 3, the loop continues. Once again, printf() does its thing. The console window looks like this:
Looping: 1 Looping: 2
Next, the for statement evaluates expression3, incrementing the value of i to 3:
Again, we go back to the top of the loop, and evaluate the termination expression:
i < 3
Lo and behold! Since i is no longer less than 3, the loop ends and the second printf() in our example is executed:
printf( "We are past the for loop." );
As was the case with while, for can take full advantage of a pair of curly braces:
for ( i = 0; i < 10; i++ ) { DoThis(); DoThat(); DanceALittleJig(); }
In addition, both while and for can take advantage of the loneliest statement, the lone semicolon:
for ( i = 0; i < 1000; i++ ) ;
The preceding example does nothing 1,000 times. Actually, the example does take some time to execute. The initialization expression is evaluated once, and the modification and termination expressions are each evaluated 1,000 times. Here s a while version of the loneliest loop:
i = 0; while ( i++ < 1000 ) ;
CHAPTER 6: Controlling Your Program s Flow
NOTE
Some compilers will eliminate a loop containing only the semicolon and just set i to its terminating value (the value it would have if the loop executed normally). This is an example of code optimization. The nice thing about code optimization is that it can make your code run faster and more efficiently. The downside is that an optimization pass on your code can sometimes cause unwanted side effects, like eliminating the while loop just discussed. Some people turn optimization off during development for this reason. Getting to know your compiler s optimization capabilities and tendencies is a good idea. Read the documentation!
loopTester.xcodeproj
Interestingly, there is an important difference between the for and while loops you just saw. Take a minute to look back and try to predict the value of i the first time through each loop and after each loop terminates. Were the results the same for the while and for loops Hmm . . . you might want to take another look. Here s a sample program that should clarify the difference between these two loops. Look in the folder Learn C Projects, inside the subfolder named 06.02 - loopTester, and open the project loopTester.xcodeproj. loopTester implements a while loop and two slightly different for loops. Run the project. Your output should look like that shown in Figure 6-1.
Figure 6-1. The output from loopTester, showing the results from a while loop and two slightly different for loops
Copyright © OnBarcode.com . All rights reserved.