- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
The for-in Loop in Java
The for-in Loop Print QR Code 2d Barcode In Java Using Barcode creator for Java Control to generate, create Denso QR Bar Code image in Java applications. Scan QR Code JIS X 0510 In Java Using Barcode reader for Java Control to read, scan read, scan image in Java applications. I only want to talk about the for-in loop here for the sake of context You will see a proper discussion of it in 5, when we talk about classes and objects For now, think of an object as a tool chest that contains a bunch of tools The for-in loop will extract each tool individually from that tool chest, and allow you to examine and manipulate it on its own In the following code, we create a simple object (compatible with JavaScript 15) and retrieve each of its properties using the for-in loop Barcode Encoder In Java Using Barcode maker for Java Control to generate, create barcode image in Java applications. Recognize Barcode In Java Using Barcode recognizer for Java Control to read, scan read, scan image in Java applications. <html> <head> <title>JavaScript Test</title>
QR Code Maker In Visual C# Using Barcode drawer for Visual Studio .NET Control to generate, create QR image in VS .NET applications. Denso QR Bar Code Generator In Visual Studio .NET Using Barcode encoder for ASP.NET Control to generate, create Quick Response Code image in ASP.NET applications. CHAPTER 2: Learn JavaScript Fundamentals
Denso QR Bar Code Printer In .NET Framework Using Barcode creation for Visual Studio .NET Control to generate, create QR Code ISO/IEC18004 image in .NET applications. Paint QR Code JIS X 0510 In Visual Basic .NET Using Barcode generator for VS .NET Control to generate, create QR Code 2d barcode image in .NET applications. </head> <body> <h1>for-in Loop</h1> <script language="JavaScript" type="text/javascript"> <!-- // Begin // We define the tool chest object var toolchest = {tool1:"Wrench", tool2:"Hammer", tool3:"Cordless drill", tool4:"Needlenose pliers"}; // for-in iterates over its properties for (var tool in toolchest) { documentwrite(toolchest[tool] + "<BR>"); } // End --> </script> </body> </html> Print GS1 DataBar Truncated In Java Using Barcode generation for Java Control to generate, create GS1 DataBar image in Java applications. Linear Creation In Java Using Barcode creator for Java Control to generate, create Linear 1D Barcode image in Java applications. This program creates an object named toolchest The object is created using an object literal, as discussed in 5 Matrix 2D Barcode Printer In Java Using Barcode creator for Java Control to generate, create Matrix Barcode image in Java applications. Data Matrix ECC200 Drawer In Java Using Barcode printer for Java Control to generate, create Data Matrix ECC200 image in Java applications. // We define the tool chest object var toolchest = {tool1:"Wrench", tool2:"Hammer", tool3:"Cordless drill", tool4:"Needlenose pliers"}; Identcode Encoder In Java Using Barcode creator for Java Control to generate, create Identcode image in Java applications. Painting Data Matrix In None Using Barcode creator for Microsoft Word Control to generate, create DataMatrix image in Word applications. The code that we re interested in is the for-in loop: Painting Code 3 Of 9 In None Using Barcode maker for Microsoft Excel Control to generate, create Code-39 image in Microsoft Excel applications. Print Code 128 Code Set A In None Using Barcode generation for Microsoft Word Control to generate, create ANSI/AIM Code 128 image in Office Word applications. // for-in iterates over its properties for (var tool in toolchest) { documentwrite(toolchest[tool] + "<BR>"); } Generating EAN13 In None Using Barcode encoder for Font Control to generate, create European Article Number 13 image in Font applications. Painting Code128 In C# Using Barcode creator for Visual Studio .NET Control to generate, create Code 128 Code Set B image in Visual Studio .NET applications. How to Do Everything with JavaScript
Generate EAN13 In Visual Studio .NET Using Barcode generator for ASP.NET Control to generate, create European Article Number 13 image in ASP.NET applications. Making Code 128A In None Using Barcode creator for Font Control to generate, create Code128 image in Font applications. This code picks loops through the contents of the toolchest object tool1, tool2, tool3, and tool4 When executed, the program outputs the value of each property, like so The break and continue Statements
We already saw the break statement in action when we looked at the switch statement When JavaScript encounters a break statement inside a switch, it stops executing code inside the switch and continues execution at the first statement that follows it The break statement works in a similar fashion with loops When JavaScript encounters a break statement, it stops executing the code inside the loop and starts again at the first statement outside the loop The break statement is not valid outside a loop or a switch statement The continue statement causes JavaScript to stop executing code inside the loop as well, but execution continues at the next iteration of the loop instead In effect, the break statement says, Exit the loop, while the continue statement says, Skip this iteration and move on to the next The continue statement is also not valid outside of a loop var Fahrenheit, Celsius; for (Celsius = -70; Celsius <= 70; Celsius += 10) { Fahrenheit = (Celsius * 9/5) + 32; documentwrite ("Celsius = " + Celsius); documentwrite (", Fahrenheit = " + Fahrenheit + "<br>"); if (Fahrenheit > 100) { // It's getting hot in here break; } } CHAPTER 2: Learn JavaScript Fundamentals
The for loop in this code is designed to run through all of the degrees Celsius starting at 70, incrementing by 10 degrees per iteration, until Celsius reaches +70 degrees ( 70, 60, 50, 40, etc) But if you examine the code inside the loop, it tells JavaScript to break out of the loop when the temperature exceeds 100 Fahrenheit Since 100 Fahrenheit is approximately 37 Celsius, the loop will actually exit when Celsius hits 40, and not 70 The break statement causes the loop to exit prematurely The continue statement is very useful when you want to loop through a range of values, ignoring some that don t meet certain criteria But when you find some that do, you have a lot of code to execute inside the loop Instead of surrounding dozens of lines of code with a giant if statement, the continue statement can be used to skip on to the next iteration // Which numbers are evenly divisible by 23 var iNumber; for (iNumber = 1; iNumber <= 1000; iNumber++) { if (iNumber % 23 > 0) { // Not divisible by 23 continue; } documentwrite("<b>" + iNumber + "</b>"); documentwrite(" is evenly divisible by 23!<br>"); }
|
|