Вы находитесь на странице: 1из 22

INTRODUCTION TO

PROBLEM SOLVING
CST1101
LOOPS

Lesson 5

Lecture 4

CST1101

Printing Numbers
Using a IF/THEN/ELSE

Long Way
PRINT 1
PRINT 2
PRINT 3

PRINT 10
PRINT 11
PRINT 12
..
PRINT 99
PRINT 100

NUMBER:
1. NUMBER = 1
2. PRINT NUMBER
3. IF (NUMBER = 100)
THEN {STOP}
ELSE {ADD 1 TO NUMBER
GO TO 2}

Issue: Must know the first


and last number
before you write the
instructions

Lecture 4

1. How would you print the


numbers from 2 to 50?
2. How would you change
the instructions to print
every other number
(count by 2) ?
CST1101

The previous slide has an algorithm we can


try out in Visual Logic. Keep the program
after you finish it. Do not use New.

It contains all three control structures:


sequence, decision, and iteration.
Note the word Print is used to mean
console output. Computer people often
use the word that way. It goes back to the
days when all output was printed.
Lecture 4

CST1101

Printing Numbers
Lets allow our user to tell
us the starting number, and ending number

Using a IF/THEN/ELSE
NUMBER:
FIRST:

How would you change


the instructions to have
the user to tell us the
count by number (for
example 2 on the
previous slide) ?
Lets call this number the
Step number

LAST:

1.
2.
3.
4.

INPUT FIRST, LAST


NUMBER = FIRST
PRINT NUMBER
IF (NUMBER = LAST)
THEN {STOP}
ELSE {ADD 1 TO NUMBER
GO TO 3}

Lecture 4

CST1101

- Note how here he uses three variables.


- Note how the variables are used in the
FOR loop.
Lets answer his question by using Visual
Logic.

Lecture 4

CST1101

Printing Numbers

FOR LOOP
FOR (INDEX = START ; INDEX < LAST ; ADD STEP VALUE)
{
(instructions)
}
1. INPUT FIRST, LAST
2. FOR (NUMBER = FIRST; NUMBER <= LAST; ADD 1 to NUMBER)
{
PRINT NUMBER
}

Lecture 4

Notes
1. The end of loop body (}) instruction
adds one (the step value) to the
index (NUMBER) and jumps back
to instruction 2
2. Every time we get to instruction 2
we check that NUMBER is still <=
LAST
CST1101

We would trace through the


algorithm with a pen and paper
pretending we are the computer.
This is a skill we cover in this
course some more examples will
be posted on Blackboard at some
point. Its called desk checking.
Lecture 4

CST1101

Printing Numbers

FOR LOOP
FOR (INDEX = START ; INDEX < LAST ; ADD STEP VALUE
{
(instructions)
}
1. INPUT FIRST, LAST, STEP
2. FOR (NUMBER = FIRST; NUMBER < LAST; ADD STEP to NUMBER)
{
PRINT NUMBER
}

What numbers will print in these situations?


1. FIRST = 2, LAST = 10, STEP = 3
2. FIRST = 4, LAST = 10, STEP = 3
3. FIRST = -2, LAST = 1, STEP = 1
4. FIRST = 5, LAST = 5, STEP = 1
5. FIRST = 9, LAST = 6, STEP = 1
Lecture 4

CST1101

Lets try the examples on the


previous slide in Visual Logic.

Lecture 4

CST1101

Printing Numbers
Using a WHILE Loop
There is another way to think about
a loop like this in many programming
languages:

NUMBER:

1.
2.
3.
4.

WHILE LOOP
While a condition is true,
Keep doing the loop
1. INPUT FIRST, LAST
2. STORE FIRST IN NUMBER
3. WHILE (NUMBER < LAST)
{
ADD 1 TO NUMBER
}

Lecture 4

CST1101

INPUT FIRST, LAST


STORE FIRST IN NUMBER
PRINT NUMBER
IF NUMBER = LAST
THEN STOP
ELSE
ADD 1 TO NUMBER
GO TO 3
CONDITION

10

In the previous problem we do


something similar with the WHILE
loop.
Lets try desk checking and
programming this example too,
preferably in that order.
Lecture 4

CST1101

11

Printing Numbers
Using a WHILE Loop
There is another way to think about
a loop like this in many programming
languages:

NUMBER:

1.
2.
3.
4.

WHILE LOOP
While a condition is true,
Keep doing the loop
1. INPUT FIRST, LAST
2. NUMBER = FIRST
3. WHILE (NUMBER < LAST)
{
PRINT NUMBER
ADD 1 TO NUMBER
}
Lecture 4

CST1101

INPUT FIRST, LAST


NUMBER = FIRST
PRINT NUMBER
IF NUMBER = LAST
THEN {STOP}
ELSE
{ADD 1 TO NUMBER
GO TO 3}
CONDITION

12

Same with that one.

Lecture 4

CST1101

13

Printing Numbers
There is another way to think about
a loop like this in many programming
languages:

Using a WHILE Loop

WHILE LOOP
While a condition is true,
Keep doing the loop

What numbers print in these situations?


1. FIRST = -4, LAST = 2
2. FIRST = 4, LAST = 2
3. FIRST = -12, LAST = -4
4. FIRST = -10, LAST = -15

1. INPUT FIRST, LAST


2. STORE FIRST IN NUMBER
3. WHILE (NUMBER <= LAST)
4. {
5.
PRINT NUMBER
6.
ADD 1 TO NUMBER
7. }
More questions
1. What happens if we erase command 6?
2. Which commands holds the logical STEP value?

Lecture 4

CST1101

14

Lets cover all of the combinations


that Prof. Moody has, both with
desk checking and Visual Logic
respectively.

Lecture 4

CST1101

15

PLAYING THE TIC TAC TOE GAME


Making a CheckWinner function

X OO
X
O X X

Function CheckWinner (Player)


If (Board(1,1) = PLAYER AND
Board(1,2) = PLAYER AND
Board(1,3) = PLAYER )
OR
(Board(2,1) = PLAYER AND
Board(2,2) = PLAYER AND
Board(2,3) = PLAYER )
OR
(Board(3,1) = PLAYER AND
Board(3,2) = PLAYER AND
Board(3,3) = PLAYER )
{return YES}
else
{ return NO}
Lecture 4

How can we do this with LOOPS?

CST1101

16

A list of similar instructions can


often be replaced by a smaller list
of instructions by using loops.
Lets consider how we would a
loop on the example in the
previous slide.
Lecture 4

CST1101

17

PLAYING THE TIC TAC TOE GAME


Making a CheckWinner function

X OO
X
O X X

Function CheckWinner (Player)


checks first row

1. SET COUNT TO ZERO


2. FOR (NUMBER=1; NUMBER <= 3; NUMBER =
NUMBER + 1)
3. {
4.
IF (BOARD(1,NUMBER) = Player)
5.
THEN {COUNT = COUNT + 1 }
6. }
7. IF (COUNT = 3 )
8. THE {RETURN YES}
Note:
9. ELSE {RETURN NO}
An assignment statement is

used to change the value of a


variable. The = means to
Questions
calculate the right side of
1. How would you check the second row?
2. How would you check the second column? instruction and store the result in
the variable on the left side.
3. How would you check the diagonal?
Lecture 4

CST1101

18

Lets go through the algorithm and


answer Prof. Moodys questions.
Prof. Moody is one of the senior
professors who teaches this
course. He created most of these
slides.
Lecture 4

CST1101

19

PLAYING THE TIC TAC TOE GAME


NESTING LOOPS ONE LOOP WITHIN ANOTHER LOOP

X OO
X
O X X

Function CheckWinner (Player)

checks all rows


1. FOR ( ROW = 1; ROW <= 3; ROW =
2.
ROW + 1)
3. {
4.
COUNT = 0
5.
FOR (COL = 1 ; COL<=3; COL =
6.
COL + 1)
7.
{
8.
IF (BOARD(ROW,COL) = Player)
9.
THEN { COUNT = COUNT + 1 }
10.
}
11.
IF (COUNT = 3)
12.
THEN { RETURN YES}
Questions
13. }
1. How would you check for columns?
14. RETURN NO
2. Would checking diagonals work this way?
Lecture 4

CST1101

20

Same with that one.

Lecture 4

CST1101

21

Then lets do a problem from


scratch. It wont have loops or
decisions. Just input, output,
assignment and variables.

Lecture 4

CST1101

22

Вам также может понравиться