Control Structures VB - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Control Structures VB

Description:

Control Structures VB Overview Without control structures, everything happens in sequence, the same way every time Jeroo has two basic control structures while ( to ... – PowerPoint PPT presentation

Number of Views:36
Avg rating:3.0/5.0
Slides: 19
Provided by: dianneme
Category:

less

Transcript and Presenter's Notes

Title: Control Structures VB


1
Control StructuresVB
2
Overview
  • Without control structures,
  • everything happens in sequence,
  • the same way every time
  • Jeroo has two basic control structures
  • while ( to repeat statements )
  • if (includes if-else, cascaded if)

3
Repetition
  • The Jeroo language only contains one repetition
    structure
  • the pretest while loop.
  • Execute some statement
  • Check the condition
  • if it is true execute body
  • recheck condition
  • if it is false terminate loop
  • execute next statement

some statement While condition body of
loop End While next statement
4
While loop notes
  • The pretest while structure (also called a while
    loop) is used to define a block of code that will
    be executed repeatedly as long as a specified
    condition is true.
  • Note The while structure is not a method, which
    means that we do not send it as a message to a
    Jeroo object.

Example while there is a flower ahead of the
jeroo named Sam, keep picking them and
hopping while Sam.isFlower(ahead)
Sam.hop() Sam.pick()end while
its called pre-test because first it tests to
see if its true that there is a flower ahead
before picking and hopping
5
another loop example
Example while there is not a net ahead of the
jeroo named James, keep hopping while not
James.isnet(ahead) James.hop() end while
its called pre-test because first it tests to
see if its true that there is NOT a net ahead
before hopping
6
Example problem using loops
  • Assume that a Jeroo named Kim is not standing on
    a flower, but there is a line of flowers ahead.
    Have Kim pick all of those flowers, and stop as
    soon as there is no flower directly ahead. After
    picking all of the flowers, Kim should turn to
    the left.

While Kim.isFlower(AHEAD) Kim.hop() Kim.pick()
End While Kim.turn(LEFT)
7
While loop style
  • There are two important things to observe about
    the coding style.
  • 1. End While marks the end of the loop
  • 2. The statements in the loop are indented.

While condition 'statements that comprise the
body of the loop End While
8
The form of a Conditional Statement
  • Execute some statement
  • Check the condition
  • true execute true branch
  • false skip true branch
  • execute next statement

some statement if condition THEN do if
true End If next statement
9
If statement style
  • There are two important things to observe about
    the coding style.
  • 1. The conditional statements are indented.
  • 2. The End If is aligned with the start of the
    word If.

If condition Then 'statements that execute if
condition is true End If
10
If Example
  • Have the Jeroo named Jessica check for a net to
    her right. If there is one, have her disable it
    and return to her current state. Whether or not
    she disables a net, Jessica should hop one space
    ahead.

If Jessica.isNet(RIGHT) then Jessica.turn(RIGHT)
Jessica.toss() Jessica.turn(LEFT) End
If Jessica.hop()
11
If-else statement
  • Execute some statement
  • Check the condition
  • true execute true branch , skip false branch
  • false skip true branch, execute false branch
  • execute next statement

some statement if (condition) Then do if
true else do if false End If next statement
12
If-else statement style
  • There are two important things to observe about
    the coding style.
  • 1. The End If is aligned with the words if and
    else.
  • 2. The conditional statements are indented.

if( condition ) Then 'statements that execute if
condition is true else ' statements that
execute if condition is false End If
13
If-else Example
Notice where the code matches the problem
description
  • Have the Jeroo named Timmy check for a net
    straight ahead. If there is one, have him disable
    it and turn around. If there is not a net
    straight ahead, Timmy should turn right.
  • After he disables the net and turns around or
    simply turns right, Timmy must move one space
    forward.

If Timmy.isNet(AHEAD) then Timmy.toss() Timmy.t
urn(LEFT) Timmy.turn(LEFT) else
Timmy.turn(RIGHT) End If Timmy.hop()
14
Sensor methods
  • In the Jeroo language, the sensor methods are the
    basic building blocks for creating conditions.
    The simplest way to create a condition is to
    invoke a sensor method.
  • hasFlower() Does this Jeroo have any flowers?
  • isFacing( compassDirection ) Is this Jeroo
    facing in the indicated direction?
  • isFlower( relativeDirection ) Is there a flower
    in the indicated direction?
  • isJeroo( relativeDirection ) Is there another
    Jeroo in the indicated direction?
  • isNet( relativeDirection ) Is there a net in the
    indicated direction?
  • isWater( relativeDirection ) Is there water in
    this direction?
  • isClear( relativeDirection ) Is there a clear
    space in the indicated direction? A clear
    space contains no flower, no net, no water,
    and no Jeroo.
  • Each sensor method is Boolean. It has either a
    true or a false result.

Use these examples to write some Boolean
conditions
15
Cascaded if statement style
This particular structure is often called a
cascaded if. Only one block of code will be
executed.
  • 3 things about the coding style.
  • 1. The End IF is aligned with the start of the
    words if and else.
  • 2. Elseif is one word.
  • 3. The other statements are indented.

If condition_1 then 'statements that execute
if condition_1 is true elseif condition_2 then
'statements to execute when condition_2 is
true 'more else if blocks as necessary elseif
last_condition then 'statements to execute
when last_condition is true else
'statements to execute when all conditions are
false End If
elseif is 1 word
16
Cascaded if syntax
  • There are 2 important things to observe about
    this structure
  • 1. There is no limit on the number of else-if
    blocks.
  • 2. The final else block is optional.

17
Cascaded if Example
  • Assume that a Jeroo named Louisa is carrying at
    least one flower.
  • Have her check the cell ahead.
  • If that cell contains a flower, pick it.
  • If that cell contains a net, disable it.
  • If that cell contains water, plant a flower at
    the current location.
  • If that cell contains another Jeroo, give that
    Jeroo a flower.
  • Finally, if there is nothing in that cell, have
    her hop once and turn left.

if Louisa.isFlower(AHEAD) then Louisa.hop()
Louisa.pick() elseif Louisa.isNet(AHEAD)
then Louisa.toss() elseif Louisa.isWater(AHEAD)
then Louisa.plant() elseif Louisa.isJeroo(AHEA
D) then Louisa.give(AHEAD) else Louisa.hop()
Louisa.turn(LEFT) End If
18
The End
Write a Comment
User Comments (0)
About PowerShow.com