Business Appl Development
Business Appl Development BISI 3160
Popular in Course
Popular in Information System
verified elite notetaker
This 7 page Class Notes was uploaded by Morris Nicolas on Wednesday October 21, 2015. The Class Notes belongs to BISI 3160 at Tennessee State University taught by Jeffrey Siekpe in Fall. Since its upload, it has received 34 views. For similar materials see /class/225676/bisi-3160-tennessee-state-university in Information System at Tennessee State University.
Reviews for Business Appl Development
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/21/15
QBASIC HAN DOUT VARIABLES A variable simply defined is a name which can contain a value Programming involves giving values to these names and presenting them in some form to the user A variable has a type which is defined by the kind of value it holds If the variable holds a number it may be of integer floating decimal long integer or imaginary If the variable holds symbols or text it may be a character variable or a string variable These are terms you will become accustomed to as you continue programming Here are some examples of values a variable might contain STRING quothello this is a stringquot INTEGER 5 LONG 92883 SINGLE 392932 DOUBLE 983288l8 The first is a string Strings contain text The last four are number types But the computer does not know what kind of value you are trying to give a variable unless you tell itl There are two methods of telling the computer what kind of variable you are using I Explicitly declare the variable AS a type This is done by using the DIM statement Say you wanted to make a variable called number which would contain an integer whole number no digits after the decimal point You would do it like this DIM number AS INTEGER Then you would use that variable as an integer The word DIM actually originates from the word Dimension but you won39t see why until we discuss the topic of arrays 2 Put a symbol after the variable name which is defined as representing that type QBasic has a set of symbols which represent each variable type String Integer amp Long l Single Double Appending one of these symbols to the name of a variable when you use it in the program tells the computer that you are using it as that type This topic of data types is actually a difficult concept to grasp for newcomers to programming The most common error in QBasic is the infamous Type Mismatch which you will see a lot This means that you are trying to put a value into a variable of the wrong type You might be trying to put the letters quothi therequot into an integer variable If you don39t define the type of the variable then QBasic assumes it is of the Single type which can often yield unexpected results I personally prefer to use the type symbols after variable names but some explicitly declare them usually at the head of their programs The IF and THEN commands 4K The IE and THEN commands are used to compare an expression and then perform some task based on that expression x 5 IE x 5 THEN PRINT quotx equals 5quot Since x does equal 5 in this case the program outputs x equals Expression signs You can also enter the following statements instead ofthe equals sign x lt 5 x is less than 5 x gt 5 x is greater than 5 Run the following x 16 IE x gt 5 THEN PRINT quotx is greater than 5quot Output x is greater than 5 You can also combine the signs like this x lt 5 x is less than or equal to 5 x gt 5 x is greater than or equal to 5 x ltgt 5 x does not equal 5 Run the following example IE x gt 5 THEN PRINT quotx is greater than or equal to 5quot IE x lt 5 THEN PRINT quotx is less than or equal to 5quot IE x ltgt 5 THEN PRINT quotx does not equal 5quot Output x is greater than or equal to 5 x is less than or equal to 5 ELSE Using the ELSE command you can have the program perform a differentaction ifthe statement is false 3 IE x 5 THEN PRINT quotYesquot ELSE PRINT quotNoquot Since x doesn39t equal 5 the output is No END IF END IE allows you to have multiple commands afterthe IE THEN statement but they must start on the line afterthe IE statement END IE should appear right afterthe list of commands x T 5 IE x 5 THEN INPUT a PRINT a END IF The following program uses ELSE with the END IE command x 16 IE x 5 THEN INPUT a PRINT a W m ai W Ql we IHMFmi m m ELSE c V c Q1 396 It T K w1 maw i PRINT X 2 El A m w l END IE Output 32 ELSEIF The ELSEIE command allows you to perform a secondary action if the rst expression was false Unlike ELSE this task is only performed ifa speci ed statement is true x 6 IE x 5 THEN PRINT quotStatement I is truequot ELSEIE x 6 THEN PRINT quotStatement 2 is truequot E Output Statement 2 is true You can have multiple ELSEIE commands along with ELSE 8 x IE x 5 THEN PRINT quotStatement I is truequot ELSEIE x 6 THEN PRINT quotStatement 2 is truequot ELSEIE x 7 THEN PRINT quotStatement 3 is truequot ELSE PRINT quotNo above statements are truequot END IF Output No above statements are true Multiple expressions You can have more than one expression in IE 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 quotYesquot in the following program X IE X 5 OR X 20 THEN PRINT quotYesquot Output Yes The AND operator requires both expressions to be true X 2 IE x gt 5 AND x lt 10 THEN PRINT quotTruequot Output True This is a slightly more complex example x 16 y 3 IF x gt 5 AND x lt 10 OR y 3 THEN PRINT quotCorrectquot Output since y is 3 Co ec Strings in FTHEN So far in this chapter we39ve only been dealing with numbers but you can also use strings with the IF THEN command x quotHelloquot IF x quotHelloquot OR x quotWorldquot THEN PRINT x Output Hello You can also compare two variable strings x quotHelloquot y quotWorldquot IF x ltgt y THEN PRINT x quot quot y Output Hello World Labels and the GOTO and GOSUB commands i The GOTO and GOSUB commands enables you to jump to certain positions in your program Labels are used to specify what point in the program to continue execution GOTO To use GOTO place a label somewhere in your program and then enter GOTO ltlabelgt Run the following example program PRINT quot1 TIPlTheLabel can be placed on the39same line as GOTO TheLabel PRINT quot3 PRINT quot239 TheLabel TheLabel PRINT quot3quot INT quot3quot Output notice how PRINT quot2quot is skipped 1 3 GOSUB The GOSUB command is the same as GOTO except when it encounters a RETURN statement the program quotreturnsquot back to the GOSUB command In other words RETURN continues program execution immediately after the previous GOSUB statement PRINT quot1quot Since the program returns to the GOSUB GOSUB TheLabel PRINT quot2 command the number 2 Is prInted thIs tIme 1 END NU TheLabeI PRINT quot3quot RN Note The END command exits the program Line numbers quotLine numbersquot can be used as labels PRINT quot1quot GOTO 10 PRINT quot2quot 10 PRINT quot3quot Notice the line number You can also write the program like this INT quot1 20 GOTO 40 30 PRINT quot2quot 40 PRINT quot3quot The line numbers don39t even have to be in sequence 17 PRINT quot1quot 2 GOTO 160 701 PRINT quot2quot 160 PRINT quot3quot Each ofthese programs output 1 3 Guessing game The following is a simple guessing game CLS start PRINT quotGuess a number between 1 and 10 quot INPUT num IF num lt 1 OR num gt 10 THEN PRINT quotThat is not between 1 and 10quot GOTO start END IF IF num 6 THEN PRINT quotCorrectlllquot ELSE PRINT quotTry againquot PRINT GOTO start END IF Output may be slightly different Guess a number between 1 and 10 2 Try again TIP Notice the second39PRiINT statement n a n Guess a number between I and 10 7 Elndek39ijRINTd 39Tr39y agal l adctj a Try again an Ine Uri er Try agalnW en 8 Guess a number between 1 and 10 6 Correctlll Loops quotLoopsquot make it easier to do an action multiple times There are at least four types of loops IFGOTOWHILEWENDDOLOOPand FORNEXT FGOTO This program uses IF GOTO to create a loop X 10 Output start 11 PRINT X 12 13 X X 1 This adds 1 to X 14 IF X lt IS THEN GOTO start WHILEWEND The WHILE WEND commands continue a loop until a specified expression is false To use WHILE WENDI 1 Place an expression after WHILE 2 Enter a list of commands 3 Place WEND at the end Run the following X 10 Output same as in previous example WHILE X lt IS 10 RINT X 11 X X l 12 WEND 13 14 DOLOOP DO LOOP is exactly the same as WHILE WEND except it has at least two slight advantages V th DO LOOP you can 1 Loop until an eXpression is true 2 Loop at least one time regardless of whether the eXpression is true or not To use DO LOOPI l Specify whether the loop continues quotwhilequot the eXpression is true or quotuntilquot the eXpression is true using the WHILE and UNTIL statements respectively 2 Place an eXpression after WHILEUNTIL 3 Enter a list of commands 4 Place LOOP at the end The following uses the WHILE statement DO WHILE X lt 15 PRINT X LOOP This program uses the UNTIL statement X 10 DO UNTIL X 15 PRINT X LOOP They both output 10 If you place the expression at the end ofthe loop instead the program goes through the loop at least once X 32 DO LOOP WHILE X lt 5 This is the output because the loop was only gone through one time 32 FORNEXT FOR NEXT provides an easier way to create a loop FOR X I TO 5 PRINT X T X NEX Output TIP The X afterNEXT Is optionalllunless you have a 39loop within a loop lbme 5 Also you can use the STEP attribute to specify how much x will be increased each time through the loop FOR X I TO 5 STEP 2
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'