Alice in Action with Java Chapter 4 Flow Control.

Slides:



Advertisements
Similar presentations
3-1 Chapter 3 Flow of Control (part a - branching)
Advertisements

CHAPTER 5: Repetition Control Structure. Objectives  To develop algorithms that use DOWHILE and REPEAT.. UNTIL structures  Introduce a pseudocode for.
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Alice in Action with Java
Alice in Action with Java Chapter 10 Flow Control in Java.
Using Control Structures in Methods Chapter 5. Chapter Contents Objectives 5.1 Example: Improved Payroll Program 5.2 Methods That Use Selection 5.3 Methods.
Alice in Action with Java
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Study Guide For Test Chapter 5, 6,& 7 Test is Friday, May 15th.
Lecture Set 5 Control Structures Part D - Repetition with Loops.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Java Programming: From Problem Analysis to Program Design, Second Edition1 Lecture 4 Objectives  Learn about repetition (looping) control structures.
CIS 234: LOOPS Adapted from materials by Dr. Donald Bell, 2000 (updated April 2007)
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Summer Computing Workshop. Introduction  Boolean Expressions – In programming, a Boolean expression is an expression that is either true or false. In.
Mr. Dave Clausen1 La Cañada High School Chapter 6: Repetition Statements.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Chapter 5: Control Structures II J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
Flow Control: Repetition with For Loops (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
Chapter 4: Control Structures II
Algorithm Design.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Flow Control: boolean expressions, “if” selection statements (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September.
Chapter 5: Control Structures II
Control Structures II: Repetition.  Learn about repetition (looping) control structures  Explore how to construct and use count-controlled, sentinel-controlled,
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Alice in Action with Java Chapter 5 Lists and Arrays.
Pascal Programming Pascal Loops and Debugging. Pascal Programming Pascal Loops In our first brush with the while do loops, simple comparisons were used.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
Flow Control: Repetition with While Loops (Alice In Action, Ch 4) Slides Credit: Joel Adams, Alice in Action CS 120 Lecture September 2012.
Controlling Program Flow with Looping Structures
CONTROL STRUCTURE Chapter 3. CONTROL STRUCTURES ONE-WAY SELECTION Syntax: if (expression) statement Expression referred to as decision maker. Statement.
Chapter 6 - Repetition. while Loop u Simplest loop u Two parts: test expression and loop body u Pre-tested loop –Execute loop body if test true –Bypass.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Methods (part 1) Alice In Action, Ch 2 Slides Credit: Joel Adams, Alice in Action CS120 Lecture August 2012.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 5 Control Structures II: Repetition.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 5 Control Structures II: Repetition.
Alice in Action with Java Chapter 4 Flow Control.
Chapter 6 Controlling Program Flow with Looping Structures.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Alice in Action with Java Chapter 2 Methods. Alice in Action with Java2 Objectives Build world-level methods to help organize a story into scenes and.
Chapter 5: Control Structures II
Chapter 3: Decisions and Loops
Loops in Java.
Chapter 5: Control Structures II
Repetition-Counter control Loop
Chapter 5: Control Structures II
Alice in Action with Java
Microsoft Visual Basic 2005 BASICS
Alice in Action with Java
Alice in Action with Java
Chapter 6: Repetition Statements
Flow of Control.
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
LOOPS The loop is the control structure we use to specify that a statement or group of statements is to be repeatedly executed. Java provides three kinds.
Loops CGS3416 Spring 2019 Lecture 7.
REPETITION Why Repetition?
Presentation transcript:

Alice in Action with Java Chapter 4 Flow Control

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

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

Alice in Action with Java4 Flow Control (continued)

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

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

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

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

Alice in Action with Java9 Relational Operators (continued)

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

Alice in Action with Java11 Boolean Operators (continued)

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

Alice in Action with Java13 if Statement Mechanics (continued)

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

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

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

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

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

Alice in Action with Java19 while Statement Mechanics (continued)

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

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: ==, !=,, >=

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