'
'*** PROGRAM: Collecting student data: names/marks.
'
'*** Create 2 array variables to store each students name/marks...
DIM students$(10), marks$(10)
'*** SCREEN ONE/Collect the data...
CLS '...(CL)ear the Output (S)creen
'*** print heading...
PRINT "PROGRAM: Collecting each student names/marks..."
'*** A FOR/NEXT loop is used to collect each individual students data...
FOR eachStudentNo% = 1 TO 10
'*** Get each students names/marks
'by typing these values in from the keyboard....
PRINT eachStudentNo%; ">"
INPUT " Enter student name"; students$(eachStudentNo%)
INPUT "Enter student marks"; marks$(eachStudentNo%)
NEXT
'*** SCREEN TWO: Output the collected data...
CLS '...(CL)ear the Output (S)creen
'*** Print headings...
PRINT "Student No.", "Student Name", "Student Marks"
'*** FOR/NEXT loop is used to print out the 2 array student 'name/marks' values...
FOR eachStudentNo% = 1 TO 10
'*** print out each students 'number/name/mark' values...
PRINT eachStudentNo%,
PRINT students$(eachStudentNo%),
PRINT marks$(eachStudentNo%)
NEXT
END '...END of program/halt program code execution
Sub programs allow you to break up one long program; into being a series of much smaller tasks; usually, controlled by a main program. Example...
====
As you can see from the above 2 examples; the 2nd example, although longer, creates code that is much clearer and simpler to read. Even non-programmers should be able to figure out what the 'Main Program' section is doing.
NOTE: There are certain rules for the use of sub-routines. Such as the sub routine should confine itself to doing just 'one' task at a time. So, SUB clearScreen...does just 'one' job, alone...which is to (CL)ear the output (S)creen. It does NOT clear the screen AND get the user to input a number. This makes it quicker and easier to debug programs; as you know exactly where the code is going wrong; so, for example, if the screen didn't clear, properly...then, look at the Sub-routine called: SUB clearScreen. If the title didn't appear; then, look at the Sub-routine called: SUB printTitle. /-Etc.
10 cls
20 input"enter word";word$
30 print word$
40 print len(word$)
50 end
this program is for GW BASIC for other BASIC's the line numbers are optional.
While--wend statement is used to execute a loop until a given condition is true.if the condition is false the loop ends and the program continous to the line following eend.
swap (int *a, int *b) {
*a ^= *b;
*b ^= *a;
*a ^= *b;
}
DIM nums(10)
CLS
FOR i = 0 TO 9
nums(i) = (RND * 10) + 1
PRINT nums(i)
NEXT
PRINT : PRINT "Press Enter to sort numbers"
rem pause to look at numbers
DO WHILE INKEY$ = "": LOOP
CLS
FOR loop1 = 0 TO 9
FOR loop2 = loop1 + 1 TO 9
IF nums(loop1) > nums(loop2) THEN SWAP nums(loop1), nums(loop2)
NEXT
NEXT
FOR i = 0 TO 9: PRINT nums(i): NEXT
BASIC is an acronym which means...
(B)eginner's (A)ll-purpose (S)ymbollic (I)nstruction (C)ode
As a part of the name already implies: (A)ll-purpose/QBASIC is a 'general purpose' programming language; which may be used to write all different sorts of programs, including...
-games
-noughts & crosses/tic tac toe
-databases
-maths
-english
-random poetry
-guess the number
-random graphics
-musical notes
-etc.
This is in direct comparison to certain other programming languages which were designed to do only one main task alone; such as...
FORTRAN/main purpose: Science/Maths
COBOL/main purpose: Business
LOGO/main purpose: Graphics
HTML/main purpose: Write/present web pages
-etc.
Cls
print the multiples tables of 1 to 5
for x=1 to 5
for y=1 to 10
print x;"*";y;"=";x*y
next y
next x
end