Embed Size (px)
Transcript of Basic computer-programming-2
Basic Computer Programming ft. QBasicBy:Blessy B. Campos Michelangelo LemonGrade 9 SSC-Lavoisier QBASIC TUTORIALWhat is QBasic?
BASIC stands for Beginners All Purpose Symbolic Instruction Code. It was invented in 1963, at Darmouth College, by the mathematicians John George Kemeny and Tom Kurtzas.BASIC is an interpreter which means it reads every line, translates it and lets the computer execute it before reading another. Each instruction starts with a line number.QBasic DataData is a collection of facts and figures that is entered into the computer through the keyboard. Data has 2 Types:1) CONSTANT: Data whose value does not change or remains fixed. There are two types of constants: (a) NUMERIC CONSTANT: Numbers negative or positive used for mathematical calculations e.g. -1,20,0 (b) ALPHANUMERIC CONSTANT/STRING: Numbers or alphabets written within double quotes (inverted commas ) e.g. Computer, Operating System2) VARIABLE: Data whose value is not constant and may change due to some calculation during the program execution. It is a location in the computers memory, which stores the values. Depending on what value is held, variables are of 2 Types:
(a) NUMERIC VARIABLE: The variable that holds a Numeric Constant for arithmetic calculations (+,-,*,/) is called a Numeric Variable. e.g. A = 50, here A is the Numeric Variable.
(b) ALPHANUMERIC VARIABLE: The variable that holds an Alphanumeric Constant, which cannot be used for arithmetic calculations, is called Alphanumeric Variable or String Variable. An Alphanumeric variable must end with a $ sign and the Alphanumeric constant must be enclosed in inverted commas. e.g. Name$ = Akanksha, here Name$ is an Alphanumeric Value.
Types of Mode in QBasicOnce QBASIC program is loaded into the computer memory, it displays Ok prompt. Ok means it is ready to accept the commands. QBASIC can be made to translate your instructions in two modes:Direct ModeProgram Mode
Direct Mode: The mode accepts single line instructions from the user and the output is viewed as soon as enter key is pressed. The instruction are not stored in the memory. This mode can be used to do quick calculation. They do not have line numbers. E.g. Print 3+4Program Mode: The mode is used to type a program which is stored in the memory. They have line numbers. We have to give the command to get the output. E.g. 10 Print 3+4 20 End
RUNIntroduction to ProgrammingProgram is a set of step-by-step instructions that tells or directs the computer what to do. It sequences the tasks and user wants to be done and produces results or output needed.The set of rules or instructions that tells the computer what to perform is done through a Programming Language. There are various types of Programming Languages you can choose from.A Programmer is the person who designs a program. It converts problem solution into instructions for the computer. The programmer designs the program, decides which of the programs or set of instructions to use and tests the program to see if it is working as designed.Retrieving keyboard input from the userOne way to receive input from the keyboard is with the INPUT command. The INPUT command allows the user to enter either a string or a number, which is then stored in a variable.When this program is executed, the INPUT command displays a question mark, followed by a blinking cursor. And when you enter text, the program stores that text into the variable data which is printed to the screen. *Just a TIP:If you place a string and a semi-colon between input and the variable, the program will print the stringExample:INPUT Enter some text :; dataTo receive a number, use a non-string variable.INPUT numberPRINT number
If you enter text instead of a number, the QBASIC interpreter displays an error message ( Redo from start)
Example in QBasic
OUTPUTThe IF and THEN commandsThe IF and THEN commands are used to compare an expression and then perform some task based on that expression.
Example:x = 5IF x = 5 THEN PRINT x is equals 5Since x does equal 5 in this case , the program outputs;x equals 5Expression SignsYou can also enter the following statements, instead of the equals sign;x < 5 ( x is less than 5)x > 5 (x is greater than 5)Run the following:x = 16IF (x > 5) THEN PRINT x is greater than 5Output:x is greater than 5You can also combine the signs like this:x = (x is greater than or equal to 5)
x 5 (x does not equal 5)ELSEUsing the ELSE command, you can have the program perform a different action of the statement if FALSE.
Example:x = 3IF x = 5 THEN PRINT Yes ELSE PRINT NoSince x is not equal to 5, the output is:NoEND IFEND IF allows you to have multiple commands after the IF . . . THEN statement, but they must start on the line after the IF statement. END IF should appear right after the list off commands.
Example:x = 5IF (x = 5) THENINPUT a$PRINT a$END IFMultiple ExpressionsYou can have more than one expression in IF. . .THEN by using either the OR operator or the AND operator.
The OR operator only requires one expression to be true in order to print Yes.The AND operator requires both expressions to be true.
Example:x = 16y = 3IF (x > 5 AND x < 10 OR y = 3) THEN PRINT CorrectOUTPUT (since Y is 3):CorrectStrings in IFTHENSo far in this topic, weve only been dealing with numbers, but you can also use strings with the IF. . .THEN command.Example:x = HelloIF (x = Hello OR x = World) THEN PRINT xOUTPUT:HelloGuessing GameThe following is a simple guessing game:CLSstart :PRINT Guess a number between 1 and 10: ;INPUT num
If (num < 1 OR num > 10) THEN PRINT That is not between 1 and 10GOTO startIF (num=6) THEN PRINT CORRECT! ! !ELSEPRINT TRY AGAINPRINTGOTO STARTEND IF
OUTPUT (may be sligthly different):Guess a number between 1 and 10: ? 2Try AgainGuess a number between 1 and 10: ? 7Try AgainGuess a number between 1 and 10: ? 6Correct! ! !
Blessy B. Campos Michelangelo Lemon