Bridges To Computing Brooklyn College



Download 382.13 Kb.
Page3/4
Date09.06.2018
Size382.13 Kb.
#54045
1   2   3   4

Creating a batch file


A batch file (or script file) is simply a text file which contains a list of commands designed to be run by a CLI. We are going to create a batch file today that will act as a very simple game. In the process of creating this game we will see that the windows command line environment is effectively a programming language environment as we have the ability to create variables and we can access the SEQUENCE, SELECTION and REPETITION operations necessary to support the Imperative paradigm.
    1. Planning the game


You should NEVER coding a game (or any other large programming project) without first planning out the flow of the game and accounting for any special resources that the game will require.
We would like to create a simple number game, where the computer picks a number at random, and the player tries to guess that number using a limited number of guesses. After the computer has picked a random number the player will get 6 guesses. After the player makes a guess, if it is incorrect, the computer will tell the player if the guess was too high, or too low. If the player correctly guesses the number they will win, but if after 6 guesses they fail to correctly guess the number, then the player will lose.
Take a moment and thing about the program you are going to create. Try and write down a pseudo-code program below, making sure to clearly identify the places where SELECTION (making a choice in the program) and REPETITION (repeating sections of code) will come into play:

START



END

    1. Creating the game

      1. Batch File Flow (Sequence)


A bath file is a sequence of commands and in general that sequence (the order in which instructions will be read) is left to right and then top down LRTD. However there are exceptions to this general rule. We have the ability to use loops (repetition – repeating sections of code) in batch files and we can also use labels to alter the flow of the program.
      1. Comments


Comments are statements (bits of code) that are ignored by the computer. You should ALWAYS comment the code you write, in order to help other programmers understand what you have written. In batch files we can create comments by using the REM command. With your batch file open, add the following line to the top of the page:

The ***** sections that appear at the beginning and end of the line are not necessary, only the command REM is necessary. However, I like to add ***** sections to comments to help the comments stand out from the rest of the batch file. We will continue to add comments throughout our batch file.
Save your batch file at this point and then go back to your CLI (command line interface). If you type in the name of your batch file, followed by enter, you should see the following (assuming you are in the correct folder):

What happened! Although our REM command is ignored by the computer, it is still displayed. And that is because by default all lines of a batch file will be "echoed" to the CLI window. Sometimes this is desirable, but in our case, it is not.


      1. Download 382.13 Kb.

        Share with your friends:
1   2   3   4




The database is protected by copyright ©ininet.org 2024
send message

    Main page