Making Multiple Choices with the SELECT CASE Statement in Java

Generator Denso QR Bar Code in Java Making Multiple Choices with the SELECT CASE Statement

Making Multiple Choices with the SELECT CASE Statement
QR Generator In Java
Using Barcode creation for Java Control to generate, create QR Code JIS X 0510 image in Java applications.
computer to choose from three or more commands (or blocks of commands), the SELECT CASE (switch) statement may be easier to read and write instead Branching simply gives the computer multiple options to use when running By accepting outside information and comparing its value, a branching statement can help the computer choose an appropriate response out of many possible responses
Generating Barcode In Java
Using Barcode maker for Java Control to generate, create bar code image in Java applications.
Book II 4
Reading Barcode In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
Making Decisions by Branching
Generating QR-Code In C#.NET
Using Barcode printer for .NET Control to generate, create Denso QR Bar Code image in Visual Studio .NET applications.
Book II: Programming Basics
Denso QR Bar Code Generation In .NET Framework
Using Barcode generation for .NET framework Control to generate, create Denso QR Bar Code image in .NET applications.
5: Repeating Commands by Looping
QR Code Generator In VB.NET
Using Barcode encoder for VS .NET Control to generate, create QR Code 2d barcode image in VS .NET applications.
In This
Barcode Printer In Java
Using Barcode printer for Java Control to generate, create bar code image in Java applications.
Looping a fixed number of times with a FOR-NEXT loop Looping zero or more times with a WHILE loop Looping at least once with a DO loop Playing around with nested loops Exiting prematurely from a loop Examining your loops
Code 128 Code Set B Generation In Java
Using Barcode creator for Java Control to generate, create Code128 image in Java applications.
o write any program, you must specify what the computer needs to do at any given time Sometimes, you may need to write the same command multiple times For example, suppose you want to print your name five times You could just write the same command five times like this:
Create Code 39 In Java
Using Barcode creator for Java Control to generate, create Code-39 image in Java applications.
PRINT PRINT PRINT PRINT PRINT John John John John John Smith Smith Smith Smith Smith
UPC - 13 Creation In Java
Using Barcode encoder for Java Control to generate, create EAN13 image in Java applications.
Writing the same five commands is cumbersome Even worse, what if you suddenly decide you want to print your name not just five times, but five thousand times Do you really want to write the same command five thousand times Probably not, which is why computer scientists invented loops A loop is just a shortcut for making the computer run one or more commands without writing those commands multiple times So rather than type the same command five times as in the preceding example, you could use a loop like this:
Creating Bar Code In Java
Using Barcode generation for Java Control to generate, create barcode image in Java applications.
FOR I = 1 TO 5 PRINT John Smith NEXT I
Encoding British Royal Mail 4-State Customer Code In Java
Using Barcode encoder for Java Control to generate, create British Royal Mail 4-State Customer Barcode image in Java applications.
Looping a Fixed Number of Times with the FOR-NEXT Loop
Printing EAN13 In Visual Basic .NET
Using Barcode creator for Visual Studio .NET Control to generate, create EAN-13 image in Visual Studio .NET applications.
This tells the computer to run the PRINT John Smith command five times If you want to print John Smith five thousand times, you just have to change the number of times you want the loop to run by replacing the 5 with 5000, such as
Barcode Scanner In .NET Framework
Using Barcode recognizer for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications.
FOR I = 1 TO 5000 PRINT John Smith NEXT I
ANSI/AIM Code 128 Drawer In Visual Studio .NET
Using Barcode encoder for .NET framework Control to generate, create Code 128 Code Set A image in .NET applications.
Loops basically make one or more commands run more than once, as shown in Figure 5-1
Generating GTIN - 128 In VB.NET
Using Barcode drawer for .NET Control to generate, create EAN / UCC - 14 image in .NET applications.
Command
Generate USS Code 39 In Visual C#.NET
Using Barcode maker for .NET Control to generate, create USS Code 39 image in .NET applications.
Command
Barcode Printer In .NET Framework
Using Barcode creator for Visual Studio .NET Control to generate, create bar code image in .NET applications.
Figure 5-1: A loop can run one or more commands over and over
Decode Barcode In Java
Using Barcode scanner for Java Control to read, scan read, scan image in Java applications.
True
Make Data Matrix 2d Barcode In .NET
Using Barcode creation for VS .NET Control to generate, create ECC200 image in VS .NET applications.
Loop again False
Command
Looping a Fixed Number of Times with the FOR-NEXT Loop
The simplest loop runs one or more commands a fixed number of times, such as five or ten times Such loops that run a fixed number of times are FOR-NEXT loops and look like this:
FOR Variable = InitialValue TO EndValue Command NEXT Variable
The first line serves two purposes The first time the FOR-NEXT loop runs, this line sets the value of the variable to an initial value, such as 1 The second and all additional times the FOR-NEXT loop runs, it checks if its
Looping a Fixed Number of Times with the FOR-NEXT Loop
variable is still within a range of values, such as between 1 and 10 If so, the FOR-NEXT loop runs again The second line consists of one or more commands that you want to run multiple times The third line tells the FOR-NEXT loop to increase the value of its variable by 1 and run the FOR-NEXT loop again The FOR-NEXT loop defines four items: A variable The initial value of the variable (usually 1) The ending value of the variable One or more commands that run multiple times Repeating Commands by Looping
Book II 5
Using a FOR-NEXT loop variable
Like all variables, the name of a FOR-NEXT loop variable can be anything, although it s best to use a descriptive name if possible So if you want to print the names of all the employees of a company by using a FOR-NEXT loop, you could use EmployeeID as a descriptive variable name, such as
FOR EmployeeID = 1 TO 150 PRINT EmployeeName NEXT EmployeeID
This example would print out each name (EmployeeName) starting with the employee who has the EmployeeID of 1 and continuing until it prints the employee with the EmployeeID of 150 If your FOR-NEXT loop variable is meant only for counting and doesn t represent anything, like employee numbers, you can just use a generic variable name, such as I or J, such as
FOR I = 1 TO 15 PRINT John Smith NEXT I
This FOR-NEXT loop just prints the name John Smith on-screen 15 times Never change the value of a FOR-NEXT loop s variable within the loop or else you risk creating an endless loop the computer keeps running the same commands over and over again without stopping This makes your program appear to freeze or hang, essentially stopping your program from working altogether The following example creates an endless loop:
ALL RIGHTS RESERVED. Business Refinery (c) 2006 - 2010. Terms of Use | Privacy Policy