6: Controlling Your Program s Flow in Java

Generation ECC200 in Java 6: Controlling Your Program s Flow

CHAPTER 6: Controlling Your Program s Flow
Data Matrix Maker In Java
Using Barcode printer for Android Control to generate, create ECC200 image in Android applications.
www.OnBarcode.com
Code-39 Creation In Java
Using Barcode creation for Android Control to generate, create Code 39 Full ASCII image in Android applications.
www.OnBarcode.com
Where to Place the Semicolon
Print Code 128 Code Set B In Java
Using Barcode creator for Android Control to generate, create Code 128 Code Set A image in Android applications.
www.OnBarcode.com
EAN / UCC - 14 Maker In Java
Using Barcode generation for Android Control to generate, create EAN / UCC - 13 image in Android applications.
www.OnBarcode.com
So far, the statements you ve seen fall into two categories: simple statements and compound statements. Function calls, such as calls to printf(), and assignment statements are called simple statements. Always place a semicolon at the end of a simple statement, even if it is broken over several lines, like this:
Generate Data Matrix ECC200 In Java
Using Barcode creator for Android Control to generate, create DataMatrix image in Android applications.
www.OnBarcode.com
Printing QR Code JIS X 0510 In Java
Using Barcode encoder for Android Control to generate, create Quick Response Code image in Android applications.
www.OnBarcode.com
printf( "%d%d%d%d", var1, var2, var3, var4 );
Barcode Generator In Java
Using Barcode drawer for Android Control to generate, create Barcode image in Android applications.
www.OnBarcode.com
GTIN - 8 Printer In Java
Using Barcode generator for Android Control to generate, create GTIN - 8 image in Android applications.
www.OnBarcode.com
Statements made up of several parts, including, possibly, other statements, are called compound statements. Compound statements obey some pretty strict rules of syntax. The if statement, for example, always looks like this:
ECC200 Generation In Java
Using Barcode generator for Java Control to generate, create Data Matrix 2d barcode image in Java applications.
www.OnBarcode.com
Data Matrix 2d Barcode Generation In None
Using Barcode generation for Microsoft Word Control to generate, create Data Matrix 2d barcode image in Office Word applications.
www.OnBarcode.com
if ( expression ) statement
Making Code128 In Visual Basic .NET
Using Barcode drawer for .NET Control to generate, create ANSI/AIM Code 128 image in Visual Studio .NET applications.
www.OnBarcode.com
Creating PDF-417 2d Barcode In None
Using Barcode creator for Microsoft Excel Control to generate, create PDF417 image in Microsoft Excel applications.
www.OnBarcode.com
Notice that there are no semicolons in this definition. The statement part of the if can be a simple statement or a compound statement. If the statement is simple, follow the semicolon rules for simple statements and place a semicolon at the end of the statement. If the statement is compound, follow the semicolon rules for that particular type of statement. Notice that using curly braces, or curlies, to build a superstatement or block out of smaller statements does not require the addition of a semicolon.
ANSI/AIM Code 128 Maker In Objective-C
Using Barcode generator for iPad Control to generate, create Code128 image in iPad applications.
www.OnBarcode.com
Encoding UCC - 12 In Visual Studio .NET
Using Barcode generation for VS .NET Control to generate, create USS-128 image in .NET framework applications.
www.OnBarcode.com
The Loneliest Statement
EAN 13 Encoder In None
Using Barcode creator for Software Control to generate, create EAN13 image in Software applications.
www.OnBarcode.com
Recognizing Data Matrix In C#
Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
Guess what A single semicolon qualifies as a statement, albeit a somewhat lonely one. For example, this code fragment
Drawing Data Matrix 2d Barcode In Objective-C
Using Barcode drawer for iPhone Control to generate, create ECC200 image in iPhone applications.
www.OnBarcode.com
Code 128A Printer In Java
Using Barcode printer for Java Control to generate, create ANSI/AIM Code 128 image in Java applications.
www.OnBarcode.com
if ( bored ) ;
Create UPC Symbol In None
Using Barcode generation for Software Control to generate, create UPC-A Supplement 5 image in Software applications.
www.OnBarcode.com
USS-128 Encoder In Visual Studio .NET
Using Barcode drawer for ASP.NET Control to generate, create EAN128 image in ASP.NET applications.
www.OnBarcode.com
is a legitimate (and thoroughly useless) if statement. If bored is true, the semicolon statement gets executed. The semicolon by itself doesn t do anything but fill the bill where a statement is needed. There are times where the semicolon by itself is exactly what you need.
The while Statement
The if statement uses the value of an expression to decide whether to execute or skip over a statement. If the statement is executed, it is executed just once. Another type of statement, the while statement, repeatedly executes a statement as long as a specified expression is true. The while statement follows this pattern:
while ( expression ) statement
CHAPTER 6: Controlling Your Program s Flow
The while statement is also known as the while loop, because once the statement is executed, the while loops back to reevaluate the expression. Here s an example of the while loop in action:
int i=0; while ( ++i < 3 ) printf( "Looping: %d\n", i ); printf( "We are past the while loop." ); i;
This example starts by declaring a variable, i, to be of type int. i is then initialized to 0. Next comes the while loop. The first thing the while loop does is evaluate its expression. The while loop s expression is
++i < 3
Before this expression is evaluated, i has a value of 0. The prefix notation used in the expression (++i) increments the value of i to 1 before the remainder of the expression is evaluated. The evaluation of the expression results in true, since 1 is less than 3. Since the expression is true, the while loop s statement, a single printf() is executed. Here s the output after the first pass through the loop:
Looping: 1
Next, the while loops back and reevaluates its expression. Once again, the prefix notation increments i, this time to a value of 2. Since 2 is less than 3, the expression evaluates to true, and the printf() is executed again. Here s the output after the second pass through the loop:
Looping: 1 Looping: 2
Once the second printf() completes, it s back to the top of the loop to reevaluate the expression. Will this never end Once again, i is incremented, this time to a value of 3. Aha! This time, the expression evaluates to false, since 3 is not less than 3. Once the expression evaluates to false, the while loop ends, and control passes to the next statement, the second printf() in our example:
printf( "We are past the while loop." );
Copyright © OnBarcode.com . All rights reserved.