Designing a Program

January 30th, 2008 yudi Posted in Programming Structure No Comments »

Designing a program involves two crucial steps:

  1. Identify what you want the program to do.

    The first step is fairly easy (because it’s always easy to tell someone else what to do). You might want a program that balances your budget, plays Chinese checkers, or removes the sound of static from a poorly recorded audio file.

  2. Tell the computer, step by step, how to do what you want it to do.

    The second (and much harder) step is to figure out how to achieve your desired result. This process of telling the computer what to do, step by step, is what programming is really all about regardless of the programming language you ultimately use.

When you have a clear idea about what you want your program to do and you know how you want to make the computer work, the next task is writing the instructions, in a particular programming language, that tells the computer what to do.

AddThis Social Bookmark Button

Spaghetti programming

January 30th, 2008 yudi Posted in Programming Structure No Comments »

In the early days of computers, many programs were small and simple, so programmers could often start writing their programs without any planning whatsoever. They’d write part of a program, test it, and add more instructions over and over again until the program finally worked (if it ever did).

Unfortunately, this haphazard approach to programming often lead to something called spaghetti programming, which gets its name from the idea of untangling strands of spaghetti.

In a well-organized program, you can easily see where a program starts, where it ends, and how it proceeds step by step. In a spaghetti program, the programmer just starts typing commands without any thought of organization whatsoever, which can be like dumping your laundry on the floor and then wondering why you can never find a matching pair of socks in a hurry.

In a small program, dumping commands haphazardly may be troublesome but still manageable. In a large program, such haphazard spaghetti programming can make a program completely incomprehensible so no one (including the original programmer) can figure out how it works ever again (if it ever works).

The code used in the program is the following:


GOTO [LabelOne]
[LabelFour]
 PROMPT “How much money do you have”; MyCash
 GOTO [LabelThree]
[LabelTwo]
 END
[LabelOne]
GOTO [LabelFour]
[LabelThree]
 PRINT “You owe me = “; MyCash * .95
 GOTO [LabelTwo]

Believe it or not, this program actually works, but trying to follow the logic is nearly impossible. Reorganizing the program can create the much simpler equivalent program:


PROMPT “How much money do you have:”; MyCash
PRINT “You owe me =”; MyCash * .95
END

Which version of the program do you think is easier to read, understand, and modify?

AddThis Social Bookmark Button

Sequential instructions

January 30th, 2008 yudi Posted in Programming Structure No Comments »

Sequential simply means that the program follows instructions, one after another, from the first instruction at the top to the last instruction at the bottom.

Every program organizes most of its instructions sequentially, but few programs organize all of their instructions sequentially. The reason is that if a program organizes all of its instructions sequentially, the program can run through its instructions exactly once before it stops.

Such a program might be handy for creating simple programs that print I am a useless program on-screen, but such programs ignore any data they might receive from the user. No matter how many times you press a key or click the mouse, running the following two-line Liberty BASIC program always displays I am a useless program on-screen followed by Because this is all I can do.


PRINT “I am a useless program”
PRINT “Because this is all I can do”
AddThis Social Bookmark Button

Branching instructions

January 30th, 2008 yudi Posted in Programming Structure No Comments »

To make programs more useful and responsive, they need to accept and react to outside data. When you play a video game and a monster appears on-screen, the game needs to react differently if you shoot and kill the monster or if the monster eats and kills you. If you shoot and kill the monster, the game lets you keep playing. If the monster kills and eats you, the game displays the words GAME OVER on-screen.

Branching instructions simply tell the computer to run different instructions depending on the value of certain data. When you delete a file, many programs ask, Do you really want to delete the file?. If you click the Yes button, the program follows the instructions to delete the file. If you click the No button, the program follows a different set of instructions

The following Liberty BASIC program shows how a simple branching instruction can work. If the user types the letter Y, the program displays the message Deleting file. If the user types any other letter, the program displays the message File NOT deleted.


PROMPT “Do you want to delete the file? (Y or N)”; answer$

IF answer$ = “Y” THEN

  NOTICE “Deleting file”

ELSE

  NOTICE “File NOT deleted”

END IF

END

Remember Branching instructions let a program respond to data. Branching instructions typically contain two or more alternative instructions, but the computer can follow only one of these sets of instructions at a time.

AddThis Social Bookmark Button

Looping instructions

January 30th, 2008 yudi Posted in Programming Structure No Comments »

No matter how many sequential or branching instructions a program has, it runs only once before stopping. To keep a program running until you want it to stop, you need to use looping instructions.

Looping instructions make the computer repeat one or more instructions. In a video game, a loop might keep running all the instructions in the program until the game character dies. Then the loop stops and the program (and game) ends.

Besides keeping a program running until the user wants to quit, looping instructions save you from having to type nearly identical instructions over and over again.

Suppose you wanted to print the numbers 1 through 5 on-screen. You could use the following Liberty BASIC program:


PRINT 1
PRINT 2
PRINT 3
PRINT 4
PRINT 5

Typing multiple instructions may be tedious, but still tolerable for a small number of instructions. However, if you want a program that prints the numbers 1 through 1000 on-screen, you would have to type nearly a thousand identical instructions.

Obviously, typing that many instructions over and over again is a waste of time, so that’s where looping instructions come in handy. Looping instructions tell the computer to repeat one or more lines of code, eliminating the need for you to type them yourself. A Liberty BASIC looping program that prints the numbers 1 through 5 on-screen might look like this:


FOR X = 1 TO 5
   PRINT X
NEXT X

Although this looping instruction looks more cryptic than the straightforward list of five instructions that printed the numbers 1 through 5 on-screen, the looping instruction requires much less typing. Even better, if you want to print the numbers 1 though 1000 on-screen, you just have to rewrite the looping instruction like this:


FOR X = 1 TO 1000
   PRINT X
NEXT X
AddThis Social Bookmark Button

The building blocks of programming

January 30th, 2008 yudi Posted in Programming Structure No Comments »

Every program in the world has been built by using the three basic building blocks (sequential, branching, and looping instructions). 

Enter Your Password!

Image from book

password$ = “”
WHILE password$ <> “open”
  PROMPT “Enter your password”; password$
  IF password$ = “open” THEN
    NOTICE “Welcome to the FBI’s secret computer network”
  ELSE
    NOTICE “Try again”
  END IF
WEND
END
Image from book
 

you can break this program in the following parts:

Sequential instructions

Image from book

password$ = “”
WHILE password$ <> “open”
  PROMPT “Enter your password”; password$
  IF password$ = “open” THEN
    NOTICE “Welcome to the FBI’s secret computer
        network”
  ELSE
    NOTICE “Try again”
  END IF
WEND
END
Image from book
 

Branching instruction

Image from book

password$ = “”
WHILE password$ <> “open”
  PROMPT “Enter your password”; password$
  IF password$ = “open” THEN
    NOTICE “Welcome to the FBI’s secret computer
        network”
  ELSE
    NOTICE “Try again”
  END IF
WEND
END
Image from book
 

Looping instruction

Image from book

password$ = “”
WHILE password$ <> “open”
  PROMPT “Enter your password”; password$
  IF password$ = “open” THEN
    NOTICE “Welcome to the FBI’s secret computer
        network”
  ELSE
    NOTICE “Try again”
  END IF
WEND
END
Image from book
 
AddThis Social Bookmark Button

Dividing and Conquering with Subprograms

January 30th, 2008 yudi Posted in Programming Structure No Comments »

In general, the more complicated the task, the larger your program needs to be. The larger your program gets, the harder it can be to read and understand. Reading a large program can be like trying to read a long novel printed on a scroll rather than on separate pages.

Just as books divide large text into separate pages for easy reading, so do programmers divide large programs into separate parts. When you divide a large program into smaller parts, those smaller parts are called subprograms.

Rather than clutter your main program with a long list of instructions, subprograms let you group related instructions in one place, making your main program smaller and easier to read and understand.

In the Liberty BASIC program, you can store most of the instructions in a subprogram, so your main program looks like this:


‘ This is the main program
GOSUB [password]
END

This Liberty BASIC program tells the computer to run all the instructions in the [password] subprogram, which looks like this:


‘ This is the subprogram
[password]
password$ = “”
WHILE password$ <> “open”
  PROMPT “Enter your password”; password$
  IF password$ = “open” THEN
    NOTICE “Welcome to the FBI’s secret computer network”
  ELSE
    NOTICE “TRY AGAIN”
  END IF
WEND
RETURN

By dividing a single program into subprograms, you gain the following advantages:

  •  Subprograms divide a large program into smaller parts to make reading and writing large programs easier.

  •  Subprograms isolate related program instructions. So if you need to modify a program later, you just need to modify a subprogram, much like removing a defective brick from a wall and putting in a better one.

AddThis Social Bookmark Button




eXTReMe Tracker