January 10th, 2008
Chapter 6: Controlling Program Flow with Loops 131 Listing 6-3 (continued) out.println( Ohhhhhhhh… ); out.println(); } out.print( Al s all wet. ); out.println( Oh, why is Al all wet? Oh, ); out.print( Al s all wet cause ); out.println( he s standing in the rain. ); } } Listing 6-3 is nice because it combines many of the ideas from Chapters 5 and 6. In Listing 6-3, two switchstatements are nested inside a forloop. One of the switchstatements uses breakstatements; the other switchstatement uses fall-through. As the value of the forloop s counter variable (verse) goes from 1to 2and then to 3, all the cases in the switchstatements are executed. When the program is near the end of its run and execution has dropped out of the forloop, the program s last four statements print the song s final verse. When I boldly declare that a forstatement is for counting, I m stretching the truth just a bit. Java s forstatement is very versatile. You can use a forstatement in situations that have nothing to do with counting. For instance, a statement with no update part, such as for (i = 0; i < 10; ), just keeps on going. The looping ends when some action inside the loop assigns a big number to the variable i. You can even create a forstatement with nothing inside the parentheses. The loop for ( ; ; )runs forever, which is good if the loop controls a serious piece of machinery. Usually, when you write a forstatement, you re counting how many times to repeat something. But, in truth, you can do just about any kind of repetition with a forstatement. Listing 6-3 uses breakstatements to jump out of a switch. But a break statement can also play a role inside a loop. To see an example, visit this book s Web site. Repeating Until You Get What You Want (Java do Statements) Fools rush in where angels fear to tread. Alexander Pope Today, I want to be young and foolish (or, at the very least, foolish). Look back at Figure 6-2 and notice how Java s whileloop works. As execution
The UK economy was the first in the world to enter the Industrial Revolution, and initially concentrated on heavy industry such as shipbuilding, coal mining, steel production and textiles.All our UK Web Hosting plans are very cheap and they fully support PHP mysql Dreamweaver frontpage and much more.
Posted in Tomcat EJB Blog | No Comments »
January 9th, 2008
130 Part II: Writing Your Own Java Programs The world premiere of Al s All Wet Listing 6-2 is very nice, but the program in that listing doesn t do anything interesting. For a more eye-catching example, see Listing 6-3. In Listing 6-3, I make good on a promise I made in Chapter 5. The program in Listing 6-3 prints all the lyrics of the hit single, Al s All Wet. (You can find the lyrics in Chapter 5.) Listing 6-3: The Unabridged Al s All Wet Song import static java.lang.System.out; class AlsAllWet { public static void main(String args[]) { for (int verse = 1; verse <= 3; verse++) { out.print( Al s all wet. ); out.println( Oh, why is Al all wet? Oh, ); out.print( Al s all wet cause ); out.println( he s standing in the rain. ); out.println( Why is Al out in the rain? ); switch (verse) { case 1: out.println ( That s because he has no brain. ); break; case 2: out.println ( That s because he is a pain. ); break; case 3: out.println ( Cause this is the last refrain. ); break; } switch (verse) { case 3: out.println( Last refrain, last refrain, ); case 2: out.println( He s a pain, he s a pain, ); case 1: out.println( Has no brain, has no brain, ); } out.println( In the rain, in the rain. );
Our stuff is composed of devoted and highly-tainted professionals, creating a mix of powerful and high-quality web hosting perl services, check web hosting perl.
Posted in Tomcat EJB Blog | No Comments »
January 8th, 2008
Chapter 6: Controlling Program Flow with Loops 129 Figure 6-4: The action of the for loop in Listing 6-2. Set count to 1 Is count less than or equal to 10? yes The value of count is … Add 1 to count no Done Each of the three items in parentheses plays its own distinct role: The initialization is executed once, when the run of your program first reaches the forstatement. The expression is evaluated several times (before each iteration). The update is also evaluated several times (at the end of each iteration). If it helps, think of the loop as if its text is shifted all around: int count = 1 for count <= 10 { out.print( The value of count is ); out.print(count); out.println( . ); count++ } You can t write a real forstatement this way. Even so, this is the order in which the parts of the statement are executed. If you declare a variable in the initialization of a forloop, you can t use that variable outside the loop. For instance, in Listing 6-2, you get an error message if you try putting out.println(count)after the end of the loop. Anything that can be done with a forloop can also be done with a while loop. Choosing to use a forloop is a matter of style and convenience, not necessity.
With our Unix hosting accounts you have total control of your Web site content from anywhere in the world.For more information please follow link Unix Web Hosting.
Posted in Tomcat EJB Blog | No Comments »
January 7th, 2008
128 Part II: Writing Your Own Java Programs Figure 6-3 shows you what you get when you run the program of Listing 6-2. (You get exactly what you deserve.) The forstatement in Listing 6-2 starts by setting the countvariable equal to 1. Then the statement tests to make sure that countis less than or equal to 10 (which it certainly is). Then the forstatement dives ahead and executes the printing statements between the curly braces. (At this early stage of the game, the computer prints The value of count is 1.) Finally, the forstatement does that last thing inside its parentheses it adds 1 to the value of count. Figure 6-3: Counting to ten. With countnow equal to 2, the forstatement checks again to make sure that countis less than or equal to 10. (Yes, 2 is smaller than 10.) Because the test turns out okay, the forstatement marches back into the curly braced statements and prints The value of count is 2on the screen. Finally, the forstatement does that last thing inside its parentheses it adds 1 to the value of count, increasing the value of countto 3. And so on. This whole thing keeps being repeated over and over again until, after 10 iterations, the value of countfinally reaches 11. When this happens, the check for countbeing less than or equal to 10 fails, and the loop s execution ends. The computer jumps to whatever statement comes immediately after the forstatement. In Listing 6-2, the computer prints Done!The whole process is illustrated in Figure 6-4. The anatomy of a for statement After the word for, you always put three things in parentheses. The first of these three things is called an initialization, the second is an expression, and the third thing is called an update. for ( initialization ; expression ; update )
As web cam capabilities have been added to instant messaging text chat services such as Yahoo Messenger, AOL Instant Messenger (AIM), MSN Messenger and Skype, one-to-one live video communication over the internet has now reached millions of mainstream PC users worldwide.You can see details on web cam web hosting section.
Posted in Tomcat EJB Blog | No Comments »
January 6th, 2008
Chapter 6: Controlling Program Flow with Loops 127 Repeating a Certain Number of Times (Java for Statements) Write I will not talk in class on the blackboard 100 times. What your teacher really meant was, Set the count to 0. As long as the count is less than 100, Write I will not talk in class on the blackboard, Add 1 to the count. Fortunately, you didn t know about loops and counters at the time. If you pointed out all this stuff to your teacher, you d have gotten into a lot more trouble than you were already in. One way or another, life is filled with examples of counting loops. And computer programming mirrors life or is it the other way around? When you tell a computer what to do, you re often telling the computer to print three lines, process ten accounts, dial a million phone numbers, or whatever. Because counting loops are so common in programming, the people who create programming languages have developed statements just for loops of this kind. In Java, the statement that repeats something a certain number of times is called a for statement. The use of the forstatement is illustrated in Listings 6-2 and 6-3. Listing 6-2 has a rock-bottom simple example, and Listing 6-3 has a more exotic example. Take your pick. Listing 6-2: The World s Most Boring for Loop import static java.lang.System.out; class Yawn { public static void main(String args[]) { for (int count = 1; count <= 10; count++) { out.print( The value of count is ); out.print(count); out.println( . ); } out.println( Done! ); } }
The UK has been a member of the European Union since 1973. The attitude of the present government towards further integration is conservative, with the official opposition favoring a return of some powers and competencies to the UK.From our experience, we can recommend Cheap UK Web Hosting services.
Posted in Tomcat EJB Blog | No Comments »
January 5th, 2008
126 Part II: Writing Your Own Java Programs When you look over Listing 6-1, you see the code that does all this work. At the core of the code is a thing called a while statement (also known as a while loop). Rephrased in English, the whilestatement says: while the inputNumber is not equal to the randomNumber keep doing all the stuff in curly braces: { } The stuff in curly braces (the stuff that repeats over and over again) is the code that prints Try again, Enter an int . . ., gets a value from the keyboard and adds 1 to the count of the user s guesses. When you re dealing with counters, like numGuessesin Listing 6-1, you may easily become confused and be off by 1 in either direction. You can avoid this headache by making sure that the ++statements stay close to the statements whose events you re counting. For example, in Listing 6-1, the variable numGuessesstarts off with a value of 0. That s because, when the program starts running, the user hasn t made any guesses. Later in the program, right after each call to myScanner.nextInt, is a numGuesses++ statement. That s how you do it you increment the counter as soon as the user enters another guess. The statements in curly braces are repeated as long as inputNumber != randomNumberkeeps being true. Each repetition of the statements in the loop is called an iteration of the loop. In Figure 6-1, the loop undergoes three iterations. (If you don t believe that Figure 6-1 has exactly three iterations, count the number of Try againprintings in the program s output. A Try againappears for each incorrect guess.) When, at long last, the user enters the correct guess, the computer goes back to the top of the whilestatement, checks the condition in parentheses, and finds itself in double negative land. The not equal (!=) relationship between inputNumberand randomNumberno longer holds. In other words, the whilestatement s condition, inputNumber != randomNumber, has become false. Because the whilestatement s condition is false, the computer jumps past the whileloop and goes on to the statements just below the whileloop. In these two statements, the computer prints You win after 4 guesses. With code of the kind shown in Listing 6-1, the computer never jumps out in mid-loop. When the computer finds that inputNumberisn t equal to randomNumber, the computer marches on and executes all five statements inside the loop s curly braces. The computer performs the test again (to see whether inputNumberis still not equal to randomNumber) only after it fully executes all five statements in the loop.
Hosting services offered by our company comes with free domain name if you pay at yearly basis. Find out more at New Orleans Web Hosting services.
Posted in Tomcat EJB Blog | No Comments »
January 4th, 2008
Chapter 6: Controlling Program Flow with Loops 125 Figure 6-1: Play until you drop. In Figure 6-1, the user makes four guesses. Each time around, the computer checks to see whether the guess is correct. An incorrect guess generates a request to try again. For a correct guess, the user gets a rousing You win, along with a tally of the number of guesses he or she made. The computer repeats several statements over and over again, checking each time through to see whether the user s guess is the same as a certain randomly generated number. Each time the user makes a guess, the computer adds 1 to its tally of guesses. When the user makes the correct guess, the computer displays that tally. The flow of action is illustrated in Figure 6-2. Welcome to the Guessing Game Enter an int from 1 to 10: Get inputNumber from the user Add 1 to numGuesses Compare inputNumber and randomNumber They’re different Try again… They’re the same Enter an int from 1 to 10: Get inputNumber from the user Add 1 to numGuesses Figure 6-2: Around and around you go. You win after numGuesses
File Transfer Protocol comes together with anonymous ftp access with every FTP Web Hosting account we offer today. Enjoy burs table ftp transfers together with fast FTP connection.
Posted in Tomcat EJB Blog | No Comments »
January 4th, 2008
124 Part II: Writing Your Own Java Programs Repeating Instructions Over and Over Again (Java while Statements) Here s a guessing game for you. The computer generates a random number from 1 to 10. The computer asks you to guess the number. If you guess incorrectly, the game continues. As soon as you guess correctly, the game is over. The program to play the game is shown in Listing 6-1, and a round of play is shown in Figure 6-1. Listing 6-1: A Repeating Guessing Game import static java.lang.System.out; import java.util.Scanner; import java.util.Random; class GuessAgain { public static void main(String args[]) { Scanner myScanner = new Scanner(System.in); int numGuesses = 0; int randomNumber = new Random().nextInt(10) + 1; out.println( ************ ); out.println( Welcome to the Guessing Game ); out.println( ************ ); out.println(); out.print( Enter an int from 1 to 10: ); int inputNumber = myScanner.nextInt(); numGuesses++; while (inputNumber != randomNumber) { out.println(); out.println( Try again… ); out.print( Enter an int from 1 to 10: ); inputNumber = myScanner.nextInt(); numGuesses++; } out.print( You win after ); out.println(numGuesses + guesses. ); } }
You need web hosting, easy to use web template and great support. What else could I ask for?All of our reseller accounts include free web hosting templates just check web hosting templates for more information.
Posted in Tomcat EJB Blog | No Comments »
January 3rd, 2008
Chapter 6 Controlling Program Flow with Loops In This Chapter Using basic looping Counting as you loop Impressing your friends with Java s enhanced loops In 1966, the company that brings you Head & Shoulders shampoo made history. On the back of the bottle, the directions for using the shampoo read, LATHER-RINSE-REPEAT. Never before had a complete set of directions (for doing anything, let alone shampooing your hair) been summarized so succinctly. People in the direction-writing business hailed this as a monumental achievement. Directions like these stood in stark contrast to others of the time. (For instance, the first sentence on a can of bug spray read, Turn this can so that it points away from your face. Duh!) Aside from their brevity, the thing that made the Head & Shoulders directions so cool was that, with three simple words, they managed to capture a notion that s at the heart of all instruction-giving the notion of repetition. That last word, REPEAT, took an otherwise bland instructional drone and turned it into a sophisticated recipe for action. The fundamental idea is that when you re following directions, you don t just follow one instruction after another. Instead, you take turns in the road. You make decisions ( If HAIR IS DRY, then USE CONDITIONER, ) and you go into loops ( LATHER-RINSE, and then LATHER-RINSE again. ). In computer programming, you use decision making and looping all the time. This chapter explores looping in Java.
Every our account comes with web mail client, free virus scanner, free anti-spam tool, free web templates and much more. For complete list of all our virtual web hosting features check our Virtual Web Hosting section.
Posted in Tomcat EJB Blog | No Comments »
January 2nd, 2008
122 Part II: Writing Your Own Java Programs Figure 5-10 shows several runs of the program in Listing 5-7. Because the switch has no breakstatements in it, fall-through happens all over the place. For instance, when the user selects verse 2, the computer executes the two statements in case 2: out.print ( He s a pain, ); out.println( he s a pain, ); Then, the computer marches right on to execute the two statements in case 1: out.print ( Has no brain, ); out.println( has no brain, ); That s good, because the song s second verse has all these lines in it. Figure 5-10: Running the code of Listing 5-7. Notice what happens when the user asks for verse 6. The switchstatement in Listing 5-7 has no case 6 and no default, so none of the actions inside the switchstatement are executed. Even so, with statements that print In the rain, in the rainand Ohhhhhhhhright after the switchstatement, the computer displays something when the user asks for verse 6.
Our window web hosting plans are bursting with features and FREE tools, at the very small rate. Sign up today window web hosting.
Posted in Tomcat EJB Blog | No Comments »