Lab 2 Variables, Branches, and Loops

advertisement

Lab 2

Variables, Branches, and Loops

This lab is a kind of crash course in basic programming techniques that are common to all programming languages. You might find it moves a little too quickly for you (it usually takes 3-4 weeks to cover this in a regular programming class), and you are highly encouraged to read (and reread) the text if you have any additional questions. Working with variables, loops and conditionals is something you will probably pick up over time as you develop your own video games, so it's not critically important if you're still mystified at the end of this lab. My goal is to give you the basic tools in this class that can let you explore independently after the class is over.

Pre-requisites

Read Chapter 2 and Chapter 3 through the top of p52

Vocabulary

Variables

Types

Input

Conditionals if, switch

Relational and logical operators

Loops while, goto, repeat

Post-lab Questions

Write your answers after completing the lab. TURN IN : the last program printout you worked on + your answer to these 4 questions next week.

1) Determine the output of: 2) Determine the output of: x = 31 y = 13 x=31 y=13 z = x - y

Print "x – y =" + z

If x >= y Then

Print x + "is bigger"

Else Print y + "is bigger"

EndIf

3) Determine the output of:

For counter = 5 To 50 Step 20

Print counter

Next

4) Determine the output of:

For counter = 5 To 50 Step 20

If counter > 35

Print counter

EndIf

Next

1

Discussion and Procedure

Part 1.

Using print to display text

;demo02-01.bb - Displays text "Hello World"

Print "Hello, World!"

;Wait for the user to press a key

WaitKey

Notes print is a function

The general syntax of Print Print [string$]

Try

Print two lines of text

Use Print with no argument

Change Print to Write and see what happens

Part 2.

Variables

Three kinds of variables

Declaring variables

Using variables

;demo02-02.bb - Adds too cool numbers

;VARIABLES favnum = 314 coolnum = 13

;Print the two variables

Print "I like " + favnum + " And I like " + coolnum ; A

;Print the variables added together)

Print "These numbers added together are " + (favnum + coolnum) ;B

WaitKey

Notes

A) you can make a string with numbers inside it using +

B) If you want to force arithmetic put parentheses around the variables that you're adding

C) store a 10 into coolnum Then display what's inside coolnum with a descriptive message ( should say coolnum=10)

D) store the product of coolnum and favnum into coolnum, then display what's inside coolnum with a descriptive message ( should say coolnum=3140)

E) add a third variable to the program called temp, then see if you can exchange the values stored in favnum and coolnum using temp for temporary storage

2

;demo02-03.bb - adds strings together string1$ = "I " string2$ = "like " string3$ = "programming!"

;concatenate the strings completestring$ = string1$ + string2$ + string3$ ; A

;print 'em out

Print completestring$

WaitKey

Notes

A) change the order of the string variables around in line ;A and see what happens

;demo02-04.bb asks user's name and shows it

;get the user's name name$ = Input$("Hi! May I know your name please? ")

Print "Hi " + name$ + "."

WaitKey

Notes

A) Input is a new function to get data from the user and store in a variable (in this case, name$)

B) input returns a string, but if you store the results in an integer variable you can use it to return integers as well

C) Declare another variable for age and read in the person's age

D) Declare another variable for year. Calculate the year the person was born using

2005-age and store the result inside the variable year, then display the year the person was born.

Part 3.

Using different branching techniques (relational and logical expressions, if, if-else)

;demo02-05.bb - Tests if you are old enough to vote

;Find out how old the user is age = Input$("How old are you? ")

;if older or equal to 18, print out confirmation that user is allowed to vote.

If age >= 18 Then

EndIf

Print "You are legally allowed to vote!"

WaitKey

;demo02-06.bb - Tests if you are old enough to vote age = Input$("How old are you? ") ;how old is user

If age >= 18 Then

Print "You are legally allowed to vote!" ;if older or equal to 18

Else Print "Sorry, you need To be a few years older." ;if younger than 18

EndIf

WaitKey

3

;demo02-07.bb - Tests if you are old enough to vote

;find out how old the user is age = Input$("How old are you? ")

;if exactly 18, write that voting is legal

If age = 18 Then

Print "You can now vote."

;if older tha 18, write out that voting has been legal for a while

Else If age > 18

Print "You've been able to vote for a while."

;if younger than 18, write out that voting is illegal.

Else If age < 18

EndIf

Print "Sorry, you will have to wait a few years to vote."

WaitKey

Notes

A) Notice the different forms, draw flowcharts in the margin indicating the way they work

B) Modify demo 2_7 to classify a person based on their age. It should identify whether they are senior citizens, adults, teenagers, children, or infants

;demo02-08.bb - tests the keys pressed x = Input$("Enter 1 to say hi, or 0 to quit. ")

Select x

Case 1

Print "Hi!"

Case 0

End

Default

Print "Huh?"

End Select

WaitKey

Notes

A) modify the previous example to display one of five mystery messages depending on what number the person types

;demo02-09.bb - Shows use of the And operator

;find out how old the user is age = Input$("How old are you? ")

;find out if the user lives in america location = Input$("Do you live in America? (1 For yes, 2 For no) ")

;Write out the proper string depending on the user's age and locations

4

If age >= 18 And location = 1 Then

Print "Congrats, you are eligible to vote!"

Else

EndIf

Print "Sorry, you can't vote."

WaitKey

Notes

A) modify the previous example to determine if a person is an adult using an and to chain two age range tests together.

B) modify the previous example to determine if a person is not an adult using an or to chain two age range tests together.

Part 4.

Using different looping techniques (goto, while, repeat)

;demo02-10.bb - Demonstrates the use of Goto

;Place a label

.label

Print "Hello"

;Does user want to repeat? selection = Input("Enter 1 if you want me to repeat 'Hello' ==> ")

If (selection = 1)

;Go to the top and print hello again

Goto label

EndIf

End

Notes

A) Draw a flowchart of the above code in the margin

B) Add a second label after hello called .label2 and Print "there" after it. Add a second condition (selection 2) that sends the program back to label2

C) Draw a flowchart of your modification in the margin

;demo03-01.bb - counts from 1 to 10

;start counter at one and loop till 10

For counter = 1 To 10

;Print whatever counter is equal to

Next

Print counter

WaitKey

Notes

A) Draw a flowchart of the above code in the margin

B) Change the loop so it counts from 15 to 25

C) Change Print to Write – What happens?

5

;demo03-02.bb - Counts backwards using step amounts

;start counter at 5 and loop till 0 by -1.2

For counter# = 5.0 To 0.0 Step -1.2

;Print value of counter

Print counter

Next

WaitKey

Notes

A) Modify above to count down from 25 in steps of 5, stopping at 0

;demo03-03.bb.bb - Counts using step amounts

;loop backwards, from 5.0 to 0.0 by -1.2

For counter# = 5.0 To 0.0 Step -1.2

;print out 3 digits of counter, so 5.000000 becomes 5.0

Print Left$( Str counter, 3)

Next

WaitKey

Notes

A) Modify above to step up from 1 to 2 in steps of 0.1234. and modify to display 6 digits of counter

;demo03-04.bb - Waits for a key and then exits

Print "This program is worthless."

Print "Press escape to exit."

;Wait until user presses 1 to Escape

While Not KeyDown(1)

Wend

End

;demo03-05.bb - Closes program after player presses ESC.

Print "Why did you open this progam?"

Repeat

Print "Press Esc to exit."

;wait a sec

Delay 1000

;repeat until user hits esc

Until KeyHit(1)

Print "Program is ending."

Notes

A) Copy the for loop from Demo3_3 into the code above after Repeat.

6

Part 5.

Application: Drawing Pictures with Nested Loops

;box drawing prog

For y = 1 To 7

For x=1 To 4

Write "*"

Next

Print

Next

WaitKey

Notes

A) draw the flowchart & show what shape the above program produces .

B) modify it to produce a box with 9 rows and 12 columns.

;empty box drawing prog

; draw the first line

Write "*"

For x=1 To 10

Write "*"

Next

Print "*"

; Draw the middle section

For y = 1 To 4

Write "*"

For x=1 To 10

Next

Write " "

Print "*"

Next

; Draw the last line

Write "*"

For x=1 To 10

Write "*"

Next

Print "*"

WaitKey

Notes

A) draw the flowchart & show what shape the above program produces .

B) modify it to produce a box with 9 rows and 12 columns.

7

For Fun!

Try drawing (using only Write and Print, using only one '*' or ' ' at a time, and using loops as shown above:

A) 2 boxes side by side separated by 8 spaces

***** *****

***** *****

***** *****

B) Basic checkerboard

*********

*********

*********

*********

*********

*********

*********

*********

*********

*********

C)

A right triangle (Hint: use for y = 1 to 7 then for x=1 to y …etc.)

*

**

***

****

*****

******

*******

D) A left triangle (this is even harder!)

*

**

***

****

*****

******

*******

8

Part 6.

Guessing Game

1) Play the guessing game demo2_11

2) Try the optimal strategy for guessing: the binary search algorithm. Always guess the midpoint between the range of possible values.

3) Mods to Guessing Game a.

redo the game without using goto: i.

remove the line label ii.

at the beginning, set guess to 0 iii.

then use a while loop like while guess <> numbertoguess to repeat the main game loop section b.

then put a for loop around the while loop to play 5 sessions of the game, make sure you reset the guess to 0 (and other init tasks) before each game. c.

Try to award points in such a way that a quicker guess gives a higher score: this could be a math formula for example

1 guess 10 points

5 guesses 2 points

10 guesses 1 points (can you figure out the formula)

9

Download