Alice in Action with Java
Chapter 4
Flow Control
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
Alice in Action with Java
2
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
Alice in Action with Java
3
Flow Control (continued)
Alice in Action with Java
4
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
Alice in Action with Java
5
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
Alice in Action with Java
6
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
Alice in Action with Java
7
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
Alice in Action with Java
8
Relational Operators (continued)
Alice in Action with Java
9
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
Alice in Action with Java
10
Boolean Operators (continued)
Alice in Action with Java
11
if 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 selected
Alice in Action with Java
12
if Statement Mechanics (continued)
Alice in Action with Java
13
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
Alice in Action with Java
14
Mechanics of the for Statement
(continued)
Alice in Action with Java
15
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
Alice in Action with Java
16
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
Alice in Action with Java
17
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
Alice in Action with Java
18
while Statement Mechanics
(continued)
Alice in Action with Java
19
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
Alice in Action with Java
20
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: ==, !=, <, <=, >, >=
Alice in Action with Java
21
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
Alice in Action with Java
22
© Copyright 2026 Paperzz