[ Pobierz całość w formacie PDF ]
.ÿþC H A P T E RProgramming77Fundamentals,Part IIIn This ChapterHow controlstructures makedecisionsour tour of programming fundamentals continues in thisYchapter with subjects that have more intriguingHow to definepossibilities.For example, I show you how programs makefunctionsdecisions and learn why a program must sometimes repeatstatements over and over.Before you re finished here, youWhere to initializewill learn how to use one of the most powerful informationvariables efficientlyholders in the JavaScript language, the array.What those darnedDecisions and Loops curly braces areall aboutEvery waking hour of every day you make decisions ofsome kind  most of the time you probably don t even realizeThe basics of datait.Don t think so? Well, look at the number of decisions youarraysmake at the grocery store, from the moment you enter thestore to the moment you clear the checkout aisle.No sooner do you enter the store than you are faced with adecision: Based on the number and size of items you intendto buy, do you pick up a hand-carried basket or attempt toextricate a shopping cart from the metallic conga line nearthe front of the store? That key decision may have impactlater when you see a special offer on an item that is tooheavy to put into the hand basket.Now you head for the foodaisles.Before entering an aisle, you compare the range ofgoods stocked in that aisle against items on your shoppinglist.If an item you need is likely to be found in this aisle, youturn into the aisle and start looking for the item; otherwise,you skip the aisle and move to the head of the next aisle.Later you reach the produce section, in search of a juicytomato.Standing in front of the bin of tomatoes, you begininspecting them, one by one  picking one up, feeling itsfirmness, checking the color, looking for blemishes or signs ofpests.You discard one, pick up another, and continue thisprocess until one matches the criteria you have set in yourmind for an acceptable morsel.Your last stop in the store isthe checkout aisle. Paper or plastic? the clerk asks.One Part II JavaScript Tutorial72more decision to make.What you choose impacts how you get the groceries fromthe car to the kitchen as well as your recycling habits.In your trip to the store, you have gone through the same kinds of decisions andrepetitions that your JavaScript programs will also encounter.If you understandthese frameworks in real life, you can now look into the JavaScript equivalents andthe syntax required to make them work.Control StructuresIn the vernacular of programming, the kinds of statements that make decisionsand loop around to repeat themselves are called control structures.A controlstructure directs the execution flow through a sequence of script statements basedon simple decisions and other factors.An important part of a control structure is the condition.Just as you may havedifferent routes to work depending on certain conditions (for example, niceweather, nighttime, attending a soccer game), so, too, does a program sometimeshave to branch to an execution route if a certain condition exists.Each condition isan expression that evaluates to true or false  one of those Boolean data typesmentioned in the previous lesson.The kinds of expressions commonly used forconditions are expressions that include a comparison operator.You do the same inreal life: If it is true that the outdoor temperature is less than freezing, then you puton a coat before going outside.In programming, however, the comparisons arestrictly comparisons of number or string values.JavaScript provides several kinds of control structures for differentprogramming situations.Three of the most common control structures you ll useare if constructions; if.else constructions; for loops.Other common control structures you ll want to know, some of which wereintroduced only in Navigator 4 and Internet Explorer 4, are covered in great detailin Chapter 31.For this tutorial, however, you need to learn about the threecommon ones just mentioned.If constructionsThe simplest program decision is to follow a special branch or path of theprogram if a certain condition is true.Formal syntax for this construction follows.Items in italics get replaced in a real script with expressions and statements that fitthe situation.if (condition) {statement[s] if true}Don t worry about the curly braces yet.Instead, get a feel for the basicstructure.The keyword, if, is a must.In parentheses goes an expression thatevaluates to a Boolean value.This is the condition being tested as the programruns past this point.If the condition evaluates to true, then one or morestatements inside the curly braces execute before continuing on with the nextstatement after the closing brace; if the condition evaluates to false, thestatements inside the curly brace are ignored, and processing continues with thenext statement after the closing brace. Chapter 7 Programming Fundamentals, Part II73The following example assumes that a variable, myAge, has had its value setearlier in the script (exactly how is not important for this example).The conditionexpression compares the value myAge against a numeric value of 18.if (myAge [ Pobierz caÅ‚ość w formacie PDF ]

  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • czarkowski.pev.pl
  •