Branching and Looping - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Branching and Looping

Description:

I've got a really cool project for 3-5 people ... continue skips the current iteration of the loop and goes to the next iteration ... – PowerPoint PPT presentation

Number of Views:183
Avg rating:3.0/5.0
Slides: 19
Provided by: leepher
Category:

less

Transcript and Presenter's Notes

Title: Branching and Looping


1
Branching and Looping
  • Managing Flow in a Script

2
Overview
  • What is branching and looping
  • if then
  • while
  • for each
  • lists
  • continue, break, return, exit
  • In-class exercise next week

3
Wait- Before getting into this
  • Ive got a really cool project for 3-5 people
  • Engineering Application/Transportation Design --
    GIS for Preliminary Line
  • Wed 3/21 -- 930 a.m. Bray 315 -- Scope out
    project with users
  • Wed 4/11 -- 930 a.m. Bray 315 -- Progress report
  • Wed 4/25 -- 930 a.m. Bray 315 -- Instruction
  • Thurs. 4/26 -- 1 to 4 p.m. Moon 14 --
    Lab/Instruction

4
Also
  • Paid Internship with CS Engineering here in
    Syracuse
  • Email Abeer Khatib
  • akhatib_at_cscos.com
  • Map making (ArcView proficiency)
  • Database (Access)
  • Could be more..

5
Branching and Looping
  • Branching statements use Boolean conditions to
    determine which set of code runs - if/then/else
  • Looping statements commonly use collections
    (lists), or groups of objects to determine how
    many times a set of code is applied. Loops can
    also use a boolean condition to determine whether
    or not they loop and when to stop - while/for each

6
Why?
  • To control flow of the program!
  • You tell it what to do upon a certain condition
    being met
  • Basic and essential in programming
  • Dictates user interaction, when calculations are
    being made, what order to execute, etc

7
Conditional Branching
  • If/Then statements use a condition to determine
    which part of the script executes. You can check
    a condition, and depending upon whether it is
    true or false, execute different lines of code.
  • Must end with end
  • Lets try it 1

8
Nesting?
  • Basically an if/then statement within another
  • or using elseif
  • Lets try it -- 2

9
Boolean Expressions
  • true, false
  • could be returned value from expression
  • myAttCond myTheme.HasAttributes
  • Boolean expressions must be in parenthesis
  • Compound expressions are done with boolean logic
    and, or, not, xor
  • my Condition ((thePword lagis) and (theNum gt
    3))
  • Lets try it 3

10
Loops - While
  • Loops execute lines of code more than once based
    on the condition being met.
  • We use while and for each
  • A while statement loops through a set of requests
    as long as (while) the condition is true! When
    the condition changes to false, it stops.
  • To make sure you dont hang-up, change the
    condition within the loop -- just in case!
  • Must have end Lets try 4

11
Loops - For each
  • The for each statement loops through each object
    in a collection and performs a set of requests.
    It initializes anObject to the first object in
    aCollection. The loop continues until the last
    object in the collection is read.
  • Huh?
  • Must also have end

12
Loops
  • In other words we can send the same set of
    requests to the objects in the list
  • for each theObject in aCollection
  • do something
  • end
  • Increments too!
  • for each num in 0 .. 5 by 2
  • do something
  • end Lets try 5

13
Lists
  • We will do more in following weeks, but...
  • From the Collection Class
  • A list is an ordered group, or collection of
    objects. A list can contain objects of different
    types.(strings, numbers, themes)
  • Referenced by index number starting with 0 and up
    to n-1
  • Make them with Make or myList x,y!
  • Lets do 6

14
Lists
  • Get them with Get
  • Populate them (put stuff in them) with Insert and
    Add
  • Use Remove to delete an object from the list
  • Count counts the number of objects in the list
  • Find to return index number of an object
  • FindbyValue for strings or numbers

15
List Issues/Number Issues!
  • When counting or doing calculations,
  • a lot of times we want to make sure to zero-out
    our variables so that previous memory does not
    get in our way .sum0 before adding numbers
  • When using count in a list zero-out with -1

16
Flow Control
  • continue, break, return and exit
  • continue skips the current iteration of the loop
    and goes to the next iteration
  • break causes immediate exit from the loop you are
    in and goes to the next statement after the end
  • return immediately terminates the current script
  • exit terminates ALL running scripts
  • Lets check out 7 to put it all together

17
For next week
  • In-class exercise (1 of 3)
  • Show us one of your modified scripts (bringing
    your disk)
  • Demonstrate and explain what it does
  • Display the script
  • Everyone signed-up for credit will do one
    (thirds)
  • next week ex5
  • following ex6 and ex7
  • when not presenting you all will assess the
    several presenters

18
Ex5?
Write a Comment
User Comments (0)
About PowerShow.com