BOISE STATE CS 120 - Chapter 4 Flow Control

Unformatted text preview:

Alice in Action with JavaObjectivesFlow ControlPowerPoint PresentationThe Boolean TypeBoolean FunctionsBoolean VariablesRelational OperatorsSlide 9Boolean OperatorsSlide 11Introducing Selective Flow ControlSlide 13if Statement MechanicsSlide 15Building if Statement ConditionsSlide 17Building if Statement Conditions (continued)Slide 19Slide 20The wait()StatementValidating Parameter ValuesSlide 23Slide 24Slide 25Introducing RepetitionSlide 27Slide 28Mechanics of the for StatementSlide 30Mechanics of the for Statement (continued)Slide 32Slide 33Nested LoopsNested Loops (continued)Slide 36The while StatementIntroducing the while StatementSlide 39Introducing the while Statement (continued)Slide 41Slide 42while Statement MechanicsSlide 44Comparing the for and while StatementsA Second ExampleSlide 47Slide 48Slide 49Flow-Control FunctionsSpirals and the Fibonacci FunctionSlide 52Spirals and the Fibonacci Function (continued)Slide 54The Fibonacci FunctionSlide 56SummarySummary (continued)Alice in Action with JavaChapter 4Flow ControlAlice in Action with Java 2Objectives•Use the Boolean type and its basic operations•Use the if statement to perform some statements while skipping others•Use the for and while statements to perform (other) statements more than once•Use Boolean variables and functions to control if and while statements•Use the wait()message to temporarily suspend program executionAlice in Action with Java 3Flow Control•Flow: sequence of steps for performing a user story•Flow control statement: structure for managing flow•Flow control statements used in previous chapters–doInOrder: produces a sequential execution –doTogether: produces a parallel execution •Control statements introduced in the current chapter –if: directs program flow along one of two paths–for: directs flow into a fixed number of loops –while: directs flow into an arbitrary number of loopsAlice in Action with Java 4Flow Control (continued)Alice in Action with Java 5The Boolean Type•A basic Alice type used to define Boolean variables•A Boolean variable holds a value of true or false•Other basic types: Number and Object•Condition (Boolean expression)–Produces a value of true or false–Basis for decision-making in programsAlice in Action with Java 6Boolean Functions•Return a value of true or false•Can act as a condition in an if or while statement•Many refer to an object’s bounding box •Example: obj.isBehind(obj2)–true, if obj’s position is beyond obj2’s rear edge–false, otherwiseAlice in Action with Java 7Boolean Variables•Used to store a value of true or false•Can be used in condition for if or while statement•How to create a Boolean variable–Click create new variable (or parameter) button–Specify Boolean as variable (or parameter) typeAlice in Action with Java 8Relational Operators•Produce true or false values•Six relational operators: ==, !=, <, <=, >, >=•Located in functions pane of world’s details area•Most often used to compare Number values•Example: hoursWorked > 40–hoursWorked is a Number variable–true when more than 40 hours have been workedAlice in Action with Java 9Relational Operators (continued)Alice in Action with Java 10Boolean Operators•Used to modify or combine relational operations•Three Boolean operators: AND, OR, NOT•Located in functions pane of world’s details area•Example: age > 12 && age < 20–age is a Number variable–Teen number compared to condition returns trueAlice in Action with Java 11Boolean Operators (continued)Alice in Action with Java 12Introducing Selective Flow Control•Summary of a scene with a princess and a dragon–Princess meets a mute dragon and asks questions–Dragon shakes its head to respond yes or no•Objective: write a shakeHead()method•Requirements for shakeHead()–Parameter: yesOrNo, a String–If yesOrNo == “yes”, dragon shakes head up and down–If yesOrNo == “no”, dragon shakes head sideways •Use an if statement to produce conditional behavior–The if control structure is at bottom of editing areaAlice in Action with Java 13Introducing Selective Flow Control (continued)Alice in Action with Java 14if Statement Mechanics•Value of a condition determines direction of flow •Structure of an if statement:–if (Condition ) { Statements1 } else { Statements2 }•if statement behavior is also called selective flow–If Condition is true, Statements1 are selected–If Condition is false, Statements2 are selectedAlice in Action with Java 15if Statement Mechanics (continued)Alice in Action with Java 16Building if Statement Conditions•Coding the condition of the if statement–Click on the yesOrNo parameter–Drag parameter into the editing area–Drop the parameter onto the condition’s placeholder–Choose other and then type “yes”•Overview for coding the remainder of shakeHead()–Add headMovement variable for amount of turn–Add turn()statements for up and down motion–Add turn()statements for sideways motionAlice in Action with Java 17Building if Statement Conditions (continued)Alice in Action with Java 18Building if Statement Conditions (continued)•Building a scene method that uses shakeHead()–princess greets dragon using a say()message–princess asks four questions –shakeHead()is called in response to each question •Click the Play button to test the programAlice in Action with Java 19Building if Statement Conditions (continued)Alice in Action with Java 20Building if Statement Conditions (continued)Alice in Action with Java 21The wait()Statement•Pauses a program for specified number of seconds•Form of wait()statement: wait(numSecs);•Use of wait()scene with dragon and princess–Inserted between princess’s first and second linesAlice in Action with Java 22Validating Parameter Values•if statement can be used to guard set of statements–Flow enters only if parameter values are valid •Example: check distance value passed to jump()–Check for positive value with condition distance > 0–Check jump length with distance < MAX_DISTANCE–Combine two conditions with the AND (&&) operator •distance > 0 && distance <= MAX_DISTANCE•How to incorporate validating logic using if structures–Place original jump()logic onto true path (outer if)–Place validating logic in the false path (nested if)Alice in Action with Java 23Validating Parameter Values (continued)Alice in Action with Java 24Validating Parameter


View Full Document

BOISE STATE CS 120 - Chapter 4 Flow Control

Documents in this Course
Load more
Download Chapter 4 Flow Control
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 4 Flow Control and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 4 Flow Control 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?