Review of variables What is a boolean expression?



Yüklə 25,35 Kb.
tarix24.12.2017
ölçüsü25,35 Kb.
#17003
növüReview

Topic:

Lab05: loops

Name: _____________________________________________________

Period: _______ Date: _______________________



Objectives

Review of variables


What is a boolean expression?

Testing a condition

:

Examples:


Not operator

Loops


Types of Loops:

While loop

Move until you reach a wall

Pick up a pile of beepers

Go until you reach the wall, pick up beepers along the way

For loops: when we know how many iterations – how many times we want to repeat the loop.


Put down 5 beepers:

  1. Initialize a variable k

  2. Loop while k < 5

  3. Increment k by 1 after each iteration.

Write an instance method to move a specified number of times.

  • I will be able to implement an if statement.

  • I will be able to implement a while loop.

  • I will be able to implement a for loop.

Declare an integer called count with an initial value of 0

int count = 0;
Increment the count by 1

count = count + 1

or: count++

Declare an boolean called result with an initial value of true

Boolean result = true;
count < 5 - it evaluates to either true or false

result
if (boolean – expression)

{

Code body



}
if (boolean-expression)

System.out.println(“It’s true”);

else

System.out.println(“It’s false”);


if (karel.frontIsClear() )

karel.move();


if (karel.nextToABeeper() )

karel.pickBeeper();

!false = true

if ( ! karel.frontIsClear() )

karel.turnLeft();
When we want to do things multiple number of times.


  • Based on a condition: ex. While the front is clear, keep moving

  • Based on a count: move 5 spaces

2 types of loops we will cover in this class:



  • while loop

  • for loop

while (boolean-expression)

{

Code – body - if more than 1 statement, curly braces are required.



}
while (karel.frontIsClear() )

karel.move();


while (karel.nextToABeeper() )

karel.pickBeeper();

while (karel.frontIsClear() )

{

if ( karel.nextToABeeper() )



karel.pickBeeper();

karel.move();

}

for ( initial – condition ; loop continuation condition ; end of loop statement )



{

Code – body - if more than 1 statement, curly braces are required.

}

for (int k=0; k < 5; k++)



karel.putBeeper();
public class Racer extends Athlete

{

public void sprint ( int n )



{

for (int k = 0; k < n ; k++ )

move();

}


Racer:

sprint( int n )

put ( int n)

pick (int n)

jumpRight ()

jumpLeft ()



Summary:


Yüklə 25,35 Kb.

Dostları ilə paylaş:




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə