260 likes | 376 Views
Programming Getting started. A computer program is a set of statements or instructions to be used directly or indirectly in a computer in order to bring about a certain result There are many languages that can be used to program a computer.
E N D
ProgrammingGetting started • A computer program is a set of statements or instructions to be used directly or indirectly in a computer in order to bring about a certain result • There are many languages that can be used to program a computer. • Machine language (program written for one type of computers) • High-level language (general-purpose language, compatible with human language)
There are three basic steps • A set of information, called the input data, is entered into the computer and stored. • The input data is then processed to produce certain desired results, known as the output data. • The output data are printed or displayed or saved on the computer.
Example: write the required steps to calculate the area of a circle using πr2, given a numerical value for the radius r as input data • Read the numerical value for the radius of the circle • Calculate the value of the area using the given formula. • Print (display) the values of the radius and the corresponding area. • Stop
What you need to know is: • Reading input……. • Displaying output ….. • Processing data (Branching, looping, functions, subroutines….) • Processing a program
Operators + Addition - Subtraction * Multiplication / Division
' {$STAMP BS2} ' {$PBASIC 2.5} x VAR Byte x=65 DEBUG "hello",CR 'DEBUG ? x 'DEBUG DEC x, CR 'DEBUG IBIN x,CR 'DEBUG IHEX x, CR END Run
Write a simple program to calculate the area and the perimeter of a 8x 4 rectangle • Read the numerical value for the length and the width of the rectangle • Calculate the value of the area using the given formula. • Print (display) the values of the radius and the corresponding area. • Stop
Start Read l & W Calculate Area and Perimeter Print area and perimeter Stop
length CON 8 width CON 4 area CON length*width perimeter CON length+width* 2 DEBUG CR, “area= “ BEBUG DEC area DEBUG CR, “perimeter= “ Debug DEC perimeter END
Looping • Looping involves repeating some portion of the program either a specified number of times or until some particular condition has been satisfied • Do loop • For Next • Goto
DO- LOOP: let a program execute a series of instructions indefinitely or until a specific condition terminates the loop. DO DEBUG “Hello”, CR Loop
DO-WHILE The program will not run if the condition is not satisfied. Length=15 Height=4 DO WHILE (height<10) area=length*height height= height+1 loop
' {$STAMP BS2} ' {$PBASIC 2.5} length CON 15 height VAR Byte height=4 area VAR Word DO WHILE (height<10) area=length*height height=hight+1 LOOP DEBUG DEC area
DO-UNTIL The program will run until the condition is satisfied. Length=15 Height=4 DO area=length*height height= height+1 Loop until (height=10)
' {$STAMP BS2} ' {$PBASIC 2.5} length CON 15 height VAR Byte height=4 area VAR Word DO area=length*height height=hight+1 DEBUG DEC area Loop Until (height=10)
FOR-NEXT Execute the statements between FOR and NEXT until the value of the counter passes the end value For height=6 to 10 Length=10 area=length*height Next
' {$STAMP BS2} ' {$PBASIC 2.5} length CON 15 height VAR Byte area VAR Word FOR height =6 TO 10 area=length*height DEBUG DEC area, CR NEXT
Start DO Read r Calculate a Print a and r loop Yes NO Stop
IF-THEN • IF condition is trueTHEN(execute the following statement (s)) IF x<20 Then area =x*y • IFcondition is trueTHEN (execute the following statement (s)) ELSE (execute the following statement (s))
IFcondition is trueTHEN (execute the following statement (s)) ELSEIF condition is trueTHEN execute the following statement (s)) ENDIF
Subroutines • To avoid repeated programming of the same calculations • Subroutines can be referenced (called) from other places in the program • GOSUB is used to redirect the program to the subroutine • RETURN causes the program to jump back to the line of code that follows the calling GOSUB
Example Main: GOSUB Hello GOSUB Goodbye Hello: DEBUG ”Hello There”, CR RETURN Goodbye: DEBUG “Bye now”, CR RETURN
ASCII Code • ASCII is acronym for the for American Standard Code for Information Interchange (Pronounced ask-ee ). • ASCII is a code for representing English characters as numbers, with each letter assigned a number from 0 to 127. • For example, the ASCII code for uppercaseM is 77. • Most computers use ASCII codes to represent text, which makes it possible to transfer data from one computer to another.