Danh mục tài liệu

beginning programming with java for dummies: part 2

Số trang: 293      Loại file: pdf      Dung lượng: 44.26 MB      Lượt xem: 24      Lượt tải: 0    
Xem trước 10 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

part 2 this book includes these contents: controlling the flow (creating loops within loops, the old runaround,...), using program units (using loops and arrays, programming with objects and classes,...), the part of tens. invite you to consult.
Nội dung trích xuất từ tài liệu:
beginning programming with java for dummies: part 2Part IIIControlling the FlowCheck out the article “How to Squeeze Nanoseconds out of a Java Loop” (and more)online at www.dummies.com/extras/beginningprogrammingwithjavaIn this part...✓ Making big decisions (or, more accurately, making not-so-bigdecisions)✓ Repeating yourself, repeating yourself, and repeating yourselfagain✓ Getting data from files on your computer’s hard driveChapter 9Forks in the RoadIn This Chapter▶Writing statements that choose between alternatives▶Putting statements inside one another▶Writing several kinds of decision-making statementsHere’s an excerpt from Beginning Programming with Java For Dummies,4th Edition, Chapter8:If you’re trying to store words or sentences (not just single letters), thenyou need to use something called a String.*This excerpt illustrates two important points: First, you may have to usesomething called a String. Second, your choice of action can depend on something being true or false.If it’s true that you’re trying to store words or sentences,you need to use something called a String.This chapter deals with decision-making, which plays a fundamental role inthe creation of instructions. With the material in this chapter, you expandyour programming power by leaps and bounds.Decisions, Decisions!Picture yourself walking along a quiet country road. You’re enjoying a pleasantsummer day. It’s not too hot, and a gentle breeze from the north makes you feelfresh and alert. You’re holding a copy of this book, opened to Chapter9. Youread the paragraph about storing words or sentences, and then you look up.*This excerpt is reprinted with permission from John Wiley & Sons, Inc. If you can’t finda copy of Beginning Programming with Java For Dummies, 4th Edition in your local bookstore, visit http://www.wiley.com.178Part III: Controlling the FlowYou see a fork in the road. You see two signs—one pointing to the right; theother pointing to the left. One sign reads, “Storing words or sentences? True.”The other sign reads, “Storing words or sentences? False.” You evaluate thewords-or-sentences situation and march on, veering right or left dependingon your software situation. A diagram of this story is shown in Figure9-1.Figure9-1:Which wayto go?Life is filled with forks in the road. Take an ordinary set of directions for heating up a frozen snack:✓Microwave cooking directions:Place on microwave-safe plate.Microwave on high for 2 minutes.Turn product.Microwave on high for 2 more minutes.✓Conventional oven directions:Preheat oven to 350 degrees.Place product on baking sheet.Bake for 25 minutes.Again, you choose between alternatives. If you use a microwave oven, dothis. Otherwise, do that.Chapter 9: Forks in the RoadIn fact, it’s hard to imagine useful instructions that don’t involve choices. Ifyou’re a homeowner with two dependents earning more than $30,000 peryear, check here. If you don’t remember how to use curly braces in Javaprograms, see Chapter4. Did the user correctly type his password? If yes,then let the user log in; if no, then kick the bum out. If you think the marketwill go up, then buy stocks; otherwise, buy bonds. And if you buy stocks,which should you buy? And when should you sell?Making Decisions (Java if Statements)When you work with computer programs, you make one decision afteranother. Almost every programming language has a way of branching inone of two directions. In Java (and in many other languages), the branchingfeature is called an if statement. Check out Listing 9-1 to see an if statement.Listing 9-1:  An if Statementif (randomNumber > 5) {System.out.println(Yes. Isnt it obvious?);} else {System.out.println(No, and dont ask again.);}To see a complete program containing the code from Listing 9-1, skip toListing 9-2 (or, if you prefer, walk, jump, or run to Listing 9-2).The if statement in Listing 9-1 represents a branch, a decision, two alternative courses of action. In plain English, this statement has the followingmeaning:If the randomNumber variables value is greater than 5,display Yes. Isnt it obvious? on the screen.Otherwise,display No, and dont ask again. on the screen.Pictorially, you get the fork shown in Figure9-2.Looking carefully atif statementsAn if statement can take the following form:if (Condition) {SomeStatements} else {OtherStatements}179