Presentation is loading. Please wait.

Presentation is loading. Please wait.

Alice in Action with Java Chapter 4 Flow Control.

Similar presentations


Presentation on theme: "Alice in Action with Java Chapter 4 Flow Control."— Presentation transcript:

1 Alice in Action with Java Chapter 4 Flow Control

2 Alice in Action with Java2 Objectives 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 execution

3 Alice in Action with Java3 Flow 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 loops

4 Alice in Action with Java4 Flow Control (continued)

5 Alice in Action with Java5 The 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 programs

6 Alice in Action with Java6 Boolean 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, otherwise

7 Alice in Action with Java7 Boolean 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) type

8 Alice in Action with Java8 Relational 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 worked

9 Alice in Action with Java9 Relational Operators (continued)

10 Alice in Action with Java10 Boolean 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 true

11 Alice in Action with Java11 Boolean Operators (continued)

12 Alice in Action with Java12 if Statement Mechanics Value of a condition determines direction of flow Structure of an if statement: –if (Condition ) { Statements 1 } else { Statements 2 } if statement behavior is also called selective flow –If Condition is true, Statements 1 are selected –If Condition is false, Statements 2 are selected

13 Alice in Action with Java13 if Statement Mechanics (continued)

14 Alice in Action with Java14 Mechanics of the for Statement Repeat statement execution a fixed number of times Example: pass 3 to flapWings() for 3 flaps Structure of the simple for statement –for(int index = 0;index < limit;index++){ Statements } The for statement is also known as a counting loop –First statement in ( ) initializes the index –Second statement in ( ) checks index against limit –Third statement in ( ) increments the index

15 Alice in Action with Java15 Mechanics of the for Statement (continued)

16 Alice in Action with Java16 Mechanics of the for Statement (continued) To test a for loop, trace the behavior with values –Statements are executed while index < numTimes –Example: send flapWings(3) to the dragon object Simple version of for lets you modify limit value Purpose of show complicated version button –Change initial value of index and/or update to index –Example: change update to index+=2 Note: neither version of for allows you to count down

17 Alice in Action with Java17 The while Statement limit value in for loop must be set to a fixed value Circumstance when the for loop is appropriate –Statements are to be executed a fixed number of times Problem: looping when the limit value is unknown Solution: use a while statement

18 Alice in Action with Java18 while Statement Mechanics Provides for both definite and indefinite looping Structure of a while loop –while ( Condition ) { Statements } The while loop is more general than the for loop –Flow enters while structure if Condition is true –One statement must eventually falsify Condition

19 Alice in Action with Java19 while Statement Mechanics (continued)

20 Alice in Action with Java20 Comparing the for and while Statements while statement can produce any type of repetition for statement is used for fixed number of repetitions Loop selection question to ask: “Am I counting?” –If yes, use a for statement; otherwise, use while Both loops test conditions before flow enters structure Both loops are bypassed if initial condition is false

21 Alice in Action with Java21 Summary Flow control statement: controls the flow of statement execution Condition: Boolean expression producing a true or false value Boolean function: returns true or false value Boolean variable: holds value of true or false Relational operators: ==, !=,, >=

22 Alice in Action with Java22 Summary (continued) Boolean operators: AND, OR, NOT if statement: directs flow along one of two paths based on evaluation of a condition for statement: repeats execution of a group of statements a fixed number of times while statement: repeats execution of a group of statements an arbitrary number of times wait() statement: a statement that pauses program flow for a specified number of seconds


Download ppt "Alice in Action with Java Chapter 4 Flow Control."

Similar presentations


Ads by Google