The Guessing Game

Programming Conditionals

Maybe you (copied what I said, and) wrote:

If they answered "y" then
change the value of top to what was the middle
and leave the bottom unchanged
If they answered "n" (or anything else)
then move bottom up to where middle is and leave top unchanged

Not bad really. Of course you don't need to tell the computer what not to do, anything you don't tell it to do, it won't do. In fact it would probably get confused if you told it "Don't do this" and "Leave that undone..."

Let's look at that first line. The computer doesn't know that "the other person answered," it only knows what's in its variables. We have a variable named "answer" right? We can tell the computer to examine that variable, to compare it to "y" and then do something if the comparison is equal:

If answer equals "y" then (put here or on the next line what to do when it's equal)

See? You are learning that the computer likes things simple. You already know the simple way to put a number into a variable. The simple version of "or anything else" is "otherwise" (just like in English). Do you think you can make all those changes? Try it, then turn the page.

Five Basic Concepts
Sequence
Iteration
Conditional
Variables
Input/Output

 

<<Previous | ToC | Next >>

[2021 May 12]