SlideShare una empresa de Scribd logo
1 de 72
Descargar para leer sin conexión
BASIC Tutorial
Basic Programming Language
BASIC Intro.
• BASIC (Beginner's All-purpose Symbolic Instruction Code)
• Very simple computer language
• 1964 at Dartsmouth college
• QBasic = Microsoft
Print & Clear Screen
Print & Clear Screen
CLS
PRINT "Hello QBASIC"
END
Print & Clear Screen
CLS
PRINT "Hello QBASIC"
END
แสดง Hello QBASIC
Clear Screen
Print & Clear Screen
CLS
PRINT "Hello QBASIC"
END
PRINT “5+3”
VS
PRINT 5+3
แสดง Hello QBASIC
Clear Screen
Print & Clear Screen
CLS
PRINT "Hello QBASIC"
END
แสดง Hello QBASIC
Clear Screen
PRINT “5+3”
VS
PRINT 5+3
5+3
8
LET & REM
LET & REM
LET เป็นคำำสั่งที่ใช้สำำหรับกำรตั้งชื่อตัวแปร
REM ใช้ในกำรอธิบำย โปรแกรม (Remark)
LET X = 12
คอมพิวเตอร์จะสร้ำงตัวแปร X แล้วนำำค่ำ 12 เก็บไว้ที่ตัวแปร X
REM Declare var x is 12
LET X% = 12
Arithmetic Mathematics
Arithmetic Mathematics
• + บวก
• - ลบ
• * คูณ
• / หำร
• ^ ยกกำำลัง
PRINT 5+6
PRINT 5-6
PRINT 5*6
PRINT 5/2
PRINT 5^2
Arithmetic Mathematics Priority
operator meaning priority
() parentheses 1
^ power 2
- negation 3
* multiply 4
/ divide 4
+ addition 5
- subtraction 5
Example
• PRINT 1 + 2 * 3
• PRINT 1 + 6
• PRINT 7
• 7
Example
• PRINT 1 + 2 * 3
• PRINT 1 + 6
• PRINT 7
• 7
Example
• PRINT 1 + 2 * 3
• PRINT 1 + 6
• PRINT 7
• 7
Example
• PRINT 1 + 2 * 3
• PRINT 1 + 6
• PRINT 7
• 7
• PRINT 2 * 4 + 2 * 3
• PRINT 8 + 2 * 3
• PRINT 8 + 6
• PRINT 14
• 14
Example
• PRINT 1 + 2 * 3
• PRINT 1 + 6
• PRINT 7
• 7
• PRINT 2 * 4 + 2 * 3
• PRINT 8 + 2 * 3
• PRINT 8 + 6
• PRINT 14
• 14
Example
• PRINT 1 + 2 * 3
• PRINT 1 + 6
• PRINT 7
• 7
• PRINT 2 * 4 + 2 * 3
• PRINT 8 + 2 * 3
• PRINT 8 + 6
• PRINT 14
• 14
Example
• PRINT 1 + 2 * 3
• PRINT 1 + 6
• PRINT 7
• 7
• PRINT 2 * 4 + 2 * 3
• PRINT 8 + 2 * 3
• PRINT 8 + 6
• PRINT 14
• 14
Example
• PRINT 1 + 2 * 3
• PRINT 1 + 6
• PRINT 7
• 7
• PRINT 2 * 4 + 2 * 3
• PRINT 8 + 2 * 3
• PRINT 8 + 6
• PRINT 14
• 14
Example
• PRINT 1 + 2 * 3
• PRINT 1 + 6
• PRINT 7
• 7
การตั้งชื่อตัวแปร
การตั้งชื่อตัวแปร
• ชื่อของตัวแปรจะยาวได้สูงสุด 40 ตัวอักษร
• สามารถใช้ตัวอักษร A ถึง Z รวมทั้ง ตังเลข 0 ถึง 9 และ ขีดล่าง
(_) ด้วย โดยมีข้อแม้ว่าต้องไม่ใช้ตัวเลขนำาหน้าชื่อตัวแปร
– ตัวอย่างชื่อที่ใช้ได้ เช่น folder1, student_name, gra1de หรือ total
– ตัวอย่างชื่อที่ใช้ไม่ได้ เช่น street-address, 1origin, file+1, student  ID,
6room
• ชื่อตัวแปรและฟังก์ชัน ไม่อาจใช้คำาสงวน (Reserved Words)
BEEP ELSE FOR INPUT RETURN
CLEAR END GET LOG SCREEN
CLOSE EOF GOSUB NEXT STRING
COMMON ERR GOTO OFF WHILE
DO EXIT IF ON XOR
ชนิดของตัวแปร
และการประกาศค่าตัวแปร
ชนิดของตัวแปร
• STRING = "hello, this is a string"
• INTEGER = 5
• LONG = 92883
• SINGLE = 39.2932
• DOUBLE = 983288.18
การประกาศค่าตัวแปร
• String = $
• Integer = %
• Long = &
• Single = !
• Double = #
การประกาศค่าตัวแปร
• String = $
• Integer = %
• Long = &
• Single = !
• Double = #
a% = 50
b% = 100
PRINT "The value of a is ” ; a%; " and the value of b is ” , b%
END
การประกาศค่าตัวแปร
• String = $
• Integer = %
• Long = &
• Single = !
• Double = #
a% = 50
b% = 100
PRINT "The value of a is ” ; a%; " and the value of b is ” , b%
END
ประกาศตัวแปร a และ b เป็น Integer และให้ค่าเป็น 50 และ 100 ตามลำาดับ โดย
รูปแบบของตัวแปรให้ใส่ด้านหลังชื่อตัวแปร
การประกาศค่าตัวแปร
• String = $
• Integer = %
• Long = &
• Single = !
• Double = #
a% = 50
b% = 100
PRINT "The value of a is ” ; a%; " and the value of b is ” , b%
END
ประกาศตัวแปร a และ b เป็น Integer และให้ค่าเป็น 50 และ 100 ตามลำาดับ โดย
รูปแบบของตัวแปรให้ใส่ด้านหลังชื่อตัวแปร
The value of a is 50 and the value of b is 100
Color
Color
• 00 - black 08 - dark grey
• 01 - dark blue 09 - light blue
• 02 - dark green 10 - light green
• 03 - dark cyan 11 - light cyan
• 04 - dark red 12 - light red
• 05 - dark purple 13 - magenta
• 06 - orange brown 14 - yellow
• 07 – grey 15 - bright white
Color
• 00 - black 08 - dark grey
• 01 - dark blue 09 - light blue
• 02 - dark green 10 - light green
• 03 - dark cyan 11 - light cyan
• 04 - dark red 12 - light red
• 05 - dark purple 13 - magenta
• 06 - orange brown 14 - yellow
• 07 – grey 15 - bright white
COLOR 14
PRINT “Yellow!"
COLOR 7
PRINT "Grey!"
Color
• 00 - black 08 - dark grey
• 01 - dark blue 09 - light blue
• 02 - dark green 10 - light green
• 03 - dark cyan 11 - light cyan
• 04 - dark red 12 - light red
• 05 - dark purple 13 - magenta
• 06 - orange brown 14 - yellow
• 07 – grey 15 - bright white
COLOR 14
PRINT “Yellow!"
COLOR 7
PRINT "Grey!"
Yellow!
Grey!
Comparison
Comparison
• > มากกว่า
• >= มากว่าเท่ากับ
• < น้อยกว่า
• <= น้อยกว่าเท่ากับ
• = เท่ากับ
• <> ไม่เท่ากับ
IF Expression
IF Expression
IF Comparison THEN
………………..
ELSE
………………..
END IF
IF Expression
IF Comparison THEN
………………..
ELSE
………………..
END IF
IF Comparison THEN
………………..
ELSEIF Comparison THEN
………………..
ELSE
………………..
END IF
IF Expression
IF Comparison THEN
………………..
ELSE
………………..
END IF
IF Expression
IF Comparison THEN
………………..
ELSE
………………..
END IF
A = 5
IF A > 0 THEN
PRINT “MORE”
ELSE
PRINT “LESS”
END IF
IF Expression
IF Comparison THEN
………………..
ELSE
………………..
END IF
A = 5
IF A > 0 THEN
PRINT “MORE”
ELSE
PRINT “LESS”
END IF
ถ้า A มากกว่า 0
IF Expression
IF Comparison THEN
………………..
ELSE
………………..
END IF
A = 5
IF A > 0 THEN
PRINT “MORE”
ELSE
PRINT “LESS”
END IF
MOREMORE
IF Expression
IF Comparison THEN
………………..
ELSE
………………..
END IF
A = -5
IF A > 0 THEN
PRINT “MORE”
ELSE
PRINT “LESS”
END IF
IF Expression
IF Comparison THEN
………………..
ELSE
………………..
END IF
A = -5
IF A > 0 THEN
PRINT “MORE”
ELSE
PRINT “LESS”
END IF
ถ้า A มากกว่า 0
IF Expression
IF Comparison THEN
………………..
ELSE
………………..
END IF
A = -5
IF A > 0 THEN
PRINT “MORE”
ELSE
PRINT “LESS”
END IF
IF Expression
IF Comparison THEN
………………..
ELSE
………………..
END IF
A = -5
IF A > 0 THEN
PRINT “MORE”
ELSE
PRINT “LESS”
END IF
LESSLESS
INPUT
INPUT
• INPUT Var
– Var คือตัวแปรที่ใช้เก็บค่าที่เราได้พิมพ์ผ่านคีย์บอร์ด
– Exp: INPUT A
– Output : ?
INPUT
• INPUT Var
– Var คือตัวแปรที่ใช้เก็บค่าที่เราได้พิมพ์ผ่านคีย์บอร์ด
– Exp: INPUT A
– Output : ?
• INPUT “Prompt”, Var
– Var คือตัวแปรที่ใช้เก็บค่าที่เราได้พิมพ์ผ่านคีย์บอร์ด
– Prompt คือคำาถามที่บ่งบอกสิ่งที่เราจะให้ผู้ใช้ใส่ค่าลงไป
– Exp: INPUT “Q/A”,a
– Output : Q/A
INPUT
CLS
INPUT "What is your name? ", yourName$
INPUT "How old are you? ", age%
PRINT "So, "; yourName$; ", you are "; age%; " years
old. That's interesting."
END
INPUT
CLS
INPUT "What is your name? ", yourName$
INPUT "How old are you? ", age%
PRINT "So, "; yourName$; ", you are "; age%; " years
old. That's interesting."
END
What is your name? TEST
How old are you? 21
So, TEST, you are 21 years old. That's interesting.
IF Expression & INPUT
CLS
PRINT "1. Say hello"
PRINT "2. Say nice tie"
INPUT "Enter your selection ", selection%
IF selection% = 1 THEN
PRINT "hello"
ELSEIF selection% = 2 THEN
PRINT "nice tie“
ENDIF
END
IF Expression & INPUT
CLS
PRINT "1. Say hello"
PRINT "2. Say nice tie"
INPUT "Enter your selection ", selection%
IF selection% = 1 THEN
PRINT "hello"
ELSEIF selection% = 2 THEN
PRINT "nice tie“
ELSE
PRINT “error“
ENDIF
END
INPUT "Enter your value ", A%
IF A% > 0 THEN
PRINT “MORE”
ELSEIF A% < 0 THEN
PRINT “LESS”
ELSE
PRINT “ZERO”
END IF
Example (1)
CLS
LET pi! = 3.1415
INPUT "What is the radius of the circle? ", radius!
area! = pi! * radius! ^ 2
PRINT "The area of the circle is ", area!
END
Example (2)
CLS
INPUT "Press 1 if you want some pizza.", number%
IF number% = 1 THEN
PRINT "Here's your pizza"
ELSE
PRINT "You don't get pizza“
END IF
END
Program Looping
Programm Looping
• GOTO
• FOR
• DO WHILE
Programm Looping
• GOTO
• FOR
• DO WHILE
GOTO
LABEL:
…………
GOTO LABEL
GOTO LABEL
…………
LABEL:
LET COUNT = 1
MORE:
PRINT COUNT
COUNT = COUNT + 1
IF COUNT < 10 THEN
GOTO MORE
END IF
END
Example GOTO
NUMBER = 1
AGAIN:
DOUBLES = NUMBER * 2
SQUARE = NUMBER * NUMBER
PRINT NUMBER, DOUBLES, SQUARE
NUMBER = NUMBER + 1
IF NUMBER < 16 THEN
GOTO AGAIN
PRINT NUMBER
END IF
END
Programm Looping
• GOTO
• FOR
• DO WHILE
FOR
FOR VAR = BEGIN TO END [STEP NUM]
……
NEXT VAR
FOR NUMBER = 1 TO 6
PRINT NUMBER
NEXT NUMBER
END
1
2
3
4
5
6
FOR + STEP
FOR NUMBER = 1 TO 10 STEP 2
PRINT NUMBER
NEXT NUMBER
1
3
5
7
9
FOR VAR = BEGIN TO END [STEP NUM]
……
NEXT VAR
Example FOR(1)
PRINT "Number","Square"
FOR NUM = 1 TO 10
PRINT NUM, NUM * NUM
NEXT NUM
END
Example FOR(2)
INPUT “1 to ” , ENDNUM
PRINT "Number","Square"
FOR NUM = 1 TO ENDNUM
PRINT NUM, NUM * NUM
NEXT NUM
END
Overflow Problem @ Factorial
Overflow Problem @ Factorial
INPUT "1 to ", ENDNUM
PRINT “Factorial", “Answer"
prod = 1
FOR NUM = 1 TO ENDNUM
prod = prod * NUM
PRINT NUM, prod
NEXT NUM
END Overflow
INPUT "1 to ", ENDNUM
PRINT “Factorial", “Answer"
prod# = 1
FOR NUM = 1 TO ENDNUM
prod# = prod# * NUM
PRINT NUM, prod#
NEXT NUM
END
Slove Overflow Problem @ Factorial
STRING = "hello"
INTEGER = 5
LONG = 92883
SINGLE =
39.2932
DOUBLE =
String = $
Integer = %
Long = &
Single = !
Double = #
Programm Looping
• GOTO
• FOR
• DO WHILE
DO WHILE
DO WHILE Comparison
………
LOOP
count = 1
DO WHILE count <= 10
PRINT count
count = count + 1
LOOP
Example (1)
total = 0
number = 0
DO WHILE number <> -999
total = total + number
INPUT "Enter a number (-999 to quit): "; number
LOOP
PRINT “Total = ”; total

Más contenido relacionado

Qbasic

  • 2. BASIC Intro. • BASIC (Beginner's All-purpose Symbolic Instruction Code) • Very simple computer language • 1964 at Dartsmouth college • QBasic = Microsoft
  • 3. Print & Clear Screen
  • 4. Print & Clear Screen CLS PRINT "Hello QBASIC" END
  • 5. Print & Clear Screen CLS PRINT "Hello QBASIC" END แสดง Hello QBASIC Clear Screen
  • 6. Print & Clear Screen CLS PRINT "Hello QBASIC" END PRINT “5+3” VS PRINT 5+3 แสดง Hello QBASIC Clear Screen
  • 7. Print & Clear Screen CLS PRINT "Hello QBASIC" END แสดง Hello QBASIC Clear Screen PRINT “5+3” VS PRINT 5+3 5+3 8
  • 9. LET & REM LET เป็นคำำสั่งที่ใช้สำำหรับกำรตั้งชื่อตัวแปร REM ใช้ในกำรอธิบำย โปรแกรม (Remark) LET X = 12 คอมพิวเตอร์จะสร้ำงตัวแปร X แล้วนำำค่ำ 12 เก็บไว้ที่ตัวแปร X REM Declare var x is 12 LET X% = 12
  • 11. Arithmetic Mathematics • + บวก • - ลบ • * คูณ • / หำร • ^ ยกกำำลัง PRINT 5+6 PRINT 5-6 PRINT 5*6 PRINT 5/2 PRINT 5^2
  • 12. Arithmetic Mathematics Priority operator meaning priority () parentheses 1 ^ power 2 - negation 3 * multiply 4 / divide 4 + addition 5 - subtraction 5
  • 13. Example • PRINT 1 + 2 * 3 • PRINT 1 + 6 • PRINT 7 • 7
  • 14. Example • PRINT 1 + 2 * 3 • PRINT 1 + 6 • PRINT 7 • 7
  • 15. Example • PRINT 1 + 2 * 3 • PRINT 1 + 6 • PRINT 7 • 7
  • 16. Example • PRINT 1 + 2 * 3 • PRINT 1 + 6 • PRINT 7 • 7
  • 17. • PRINT 2 * 4 + 2 * 3 • PRINT 8 + 2 * 3 • PRINT 8 + 6 • PRINT 14 • 14 Example • PRINT 1 + 2 * 3 • PRINT 1 + 6 • PRINT 7 • 7
  • 18. • PRINT 2 * 4 + 2 * 3 • PRINT 8 + 2 * 3 • PRINT 8 + 6 • PRINT 14 • 14 Example • PRINT 1 + 2 * 3 • PRINT 1 + 6 • PRINT 7 • 7
  • 19. • PRINT 2 * 4 + 2 * 3 • PRINT 8 + 2 * 3 • PRINT 8 + 6 • PRINT 14 • 14 Example • PRINT 1 + 2 * 3 • PRINT 1 + 6 • PRINT 7 • 7
  • 20. • PRINT 2 * 4 + 2 * 3 • PRINT 8 + 2 * 3 • PRINT 8 + 6 • PRINT 14 • 14 Example • PRINT 1 + 2 * 3 • PRINT 1 + 6 • PRINT 7 • 7
  • 21. • PRINT 2 * 4 + 2 * 3 • PRINT 8 + 2 * 3 • PRINT 8 + 6 • PRINT 14 • 14 Example • PRINT 1 + 2 * 3 • PRINT 1 + 6 • PRINT 7 • 7
  • 23. การตั้งชื่อตัวแปร • ชื่อของตัวแปรจะยาวได้สูงสุด 40 ตัวอักษร • สามารถใช้ตัวอักษร A ถึง Z รวมทั้ง ตังเลข 0 ถึง 9 และ ขีดล่าง (_) ด้วย โดยมีข้อแม้ว่าต้องไม่ใช้ตัวเลขนำาหน้าชื่อตัวแปร – ตัวอย่างชื่อที่ใช้ได้ เช่น folder1, student_name, gra1de หรือ total – ตัวอย่างชื่อที่ใช้ไม่ได้ เช่น street-address, 1origin, file+1, student  ID, 6room • ชื่อตัวแปรและฟังก์ชัน ไม่อาจใช้คำาสงวน (Reserved Words) BEEP ELSE FOR INPUT RETURN CLEAR END GET LOG SCREEN CLOSE EOF GOSUB NEXT STRING COMMON ERR GOTO OFF WHILE DO EXIT IF ON XOR
  • 25. ชนิดของตัวแปร • STRING = "hello, this is a string" • INTEGER = 5 • LONG = 92883 • SINGLE = 39.2932 • DOUBLE = 983288.18
  • 26. การประกาศค่าตัวแปร • String = $ • Integer = % • Long = & • Single = ! • Double = #
  • 27. การประกาศค่าตัวแปร • String = $ • Integer = % • Long = & • Single = ! • Double = # a% = 50 b% = 100 PRINT "The value of a is ” ; a%; " and the value of b is ” , b% END
  • 28. การประกาศค่าตัวแปร • String = $ • Integer = % • Long = & • Single = ! • Double = # a% = 50 b% = 100 PRINT "The value of a is ” ; a%; " and the value of b is ” , b% END ประกาศตัวแปร a และ b เป็น Integer และให้ค่าเป็น 50 และ 100 ตามลำาดับ โดย รูปแบบของตัวแปรให้ใส่ด้านหลังชื่อตัวแปร
  • 29. การประกาศค่าตัวแปร • String = $ • Integer = % • Long = & • Single = ! • Double = # a% = 50 b% = 100 PRINT "The value of a is ” ; a%; " and the value of b is ” , b% END ประกาศตัวแปร a และ b เป็น Integer และให้ค่าเป็น 50 และ 100 ตามลำาดับ โดย รูปแบบของตัวแปรให้ใส่ด้านหลังชื่อตัวแปร The value of a is 50 and the value of b is 100
  • 30. Color
  • 31. Color • 00 - black 08 - dark grey • 01 - dark blue 09 - light blue • 02 - dark green 10 - light green • 03 - dark cyan 11 - light cyan • 04 - dark red 12 - light red • 05 - dark purple 13 - magenta • 06 - orange brown 14 - yellow • 07 – grey 15 - bright white
  • 32. Color • 00 - black 08 - dark grey • 01 - dark blue 09 - light blue • 02 - dark green 10 - light green • 03 - dark cyan 11 - light cyan • 04 - dark red 12 - light red • 05 - dark purple 13 - magenta • 06 - orange brown 14 - yellow • 07 – grey 15 - bright white COLOR 14 PRINT “Yellow!" COLOR 7 PRINT "Grey!"
  • 33. Color • 00 - black 08 - dark grey • 01 - dark blue 09 - light blue • 02 - dark green 10 - light green • 03 - dark cyan 11 - light cyan • 04 - dark red 12 - light red • 05 - dark purple 13 - magenta • 06 - orange brown 14 - yellow • 07 – grey 15 - bright white COLOR 14 PRINT “Yellow!" COLOR 7 PRINT "Grey!" Yellow! Grey!
  • 35. Comparison • > มากกว่า • >= มากว่าเท่ากับ • < น้อยกว่า • <= น้อยกว่าเท่ากับ • = เท่ากับ • <> ไม่เท่ากับ
  • 37. IF Expression IF Comparison THEN ……………….. ELSE ……………….. END IF
  • 38. IF Expression IF Comparison THEN ……………….. ELSE ……………….. END IF IF Comparison THEN ……………….. ELSEIF Comparison THEN ……………….. ELSE ……………….. END IF
  • 39. IF Expression IF Comparison THEN ……………….. ELSE ……………….. END IF
  • 40. IF Expression IF Comparison THEN ……………….. ELSE ……………….. END IF A = 5 IF A > 0 THEN PRINT “MORE” ELSE PRINT “LESS” END IF
  • 41. IF Expression IF Comparison THEN ……………….. ELSE ……………….. END IF A = 5 IF A > 0 THEN PRINT “MORE” ELSE PRINT “LESS” END IF ถ้า A มากกว่า 0
  • 42. IF Expression IF Comparison THEN ……………….. ELSE ……………….. END IF A = 5 IF A > 0 THEN PRINT “MORE” ELSE PRINT “LESS” END IF MOREMORE
  • 43. IF Expression IF Comparison THEN ……………….. ELSE ……………….. END IF A = -5 IF A > 0 THEN PRINT “MORE” ELSE PRINT “LESS” END IF
  • 44. IF Expression IF Comparison THEN ……………….. ELSE ……………….. END IF A = -5 IF A > 0 THEN PRINT “MORE” ELSE PRINT “LESS” END IF ถ้า A มากกว่า 0
  • 45. IF Expression IF Comparison THEN ……………….. ELSE ……………….. END IF A = -5 IF A > 0 THEN PRINT “MORE” ELSE PRINT “LESS” END IF
  • 46. IF Expression IF Comparison THEN ……………….. ELSE ……………….. END IF A = -5 IF A > 0 THEN PRINT “MORE” ELSE PRINT “LESS” END IF LESSLESS
  • 47. INPUT
  • 48. INPUT • INPUT Var – Var คือตัวแปรที่ใช้เก็บค่าที่เราได้พิมพ์ผ่านคีย์บอร์ด – Exp: INPUT A – Output : ?
  • 49. INPUT • INPUT Var – Var คือตัวแปรที่ใช้เก็บค่าที่เราได้พิมพ์ผ่านคีย์บอร์ด – Exp: INPUT A – Output : ? • INPUT “Prompt”, Var – Var คือตัวแปรที่ใช้เก็บค่าที่เราได้พิมพ์ผ่านคีย์บอร์ด – Prompt คือคำาถามที่บ่งบอกสิ่งที่เราจะให้ผู้ใช้ใส่ค่าลงไป – Exp: INPUT “Q/A”,a – Output : Q/A
  • 50. INPUT CLS INPUT "What is your name? ", yourName$ INPUT "How old are you? ", age% PRINT "So, "; yourName$; ", you are "; age%; " years old. That's interesting." END
  • 51. INPUT CLS INPUT "What is your name? ", yourName$ INPUT "How old are you? ", age% PRINT "So, "; yourName$; ", you are "; age%; " years old. That's interesting." END What is your name? TEST How old are you? 21 So, TEST, you are 21 years old. That's interesting.
  • 52. IF Expression & INPUT CLS PRINT "1. Say hello" PRINT "2. Say nice tie" INPUT "Enter your selection ", selection% IF selection% = 1 THEN PRINT "hello" ELSEIF selection% = 2 THEN PRINT "nice tie“ ENDIF END
  • 53. IF Expression & INPUT CLS PRINT "1. Say hello" PRINT "2. Say nice tie" INPUT "Enter your selection ", selection% IF selection% = 1 THEN PRINT "hello" ELSEIF selection% = 2 THEN PRINT "nice tie“ ELSE PRINT “error“ ENDIF END
  • 54. INPUT "Enter your value ", A% IF A% > 0 THEN PRINT “MORE” ELSEIF A% < 0 THEN PRINT “LESS” ELSE PRINT “ZERO” END IF
  • 55. Example (1) CLS LET pi! = 3.1415 INPUT "What is the radius of the circle? ", radius! area! = pi! * radius! ^ 2 PRINT "The area of the circle is ", area! END
  • 56. Example (2) CLS INPUT "Press 1 if you want some pizza.", number% IF number% = 1 THEN PRINT "Here's your pizza" ELSE PRINT "You don't get pizza“ END IF END
  • 58. Programm Looping • GOTO • FOR • DO WHILE
  • 59. Programm Looping • GOTO • FOR • DO WHILE
  • 60. GOTO LABEL: ………… GOTO LABEL GOTO LABEL ………… LABEL: LET COUNT = 1 MORE: PRINT COUNT COUNT = COUNT + 1 IF COUNT < 10 THEN GOTO MORE END IF END
  • 61. Example GOTO NUMBER = 1 AGAIN: DOUBLES = NUMBER * 2 SQUARE = NUMBER * NUMBER PRINT NUMBER, DOUBLES, SQUARE NUMBER = NUMBER + 1 IF NUMBER < 16 THEN GOTO AGAIN PRINT NUMBER END IF END
  • 62. Programm Looping • GOTO • FOR • DO WHILE
  • 63. FOR FOR VAR = BEGIN TO END [STEP NUM] …… NEXT VAR FOR NUMBER = 1 TO 6 PRINT NUMBER NEXT NUMBER END 1 2 3 4 5 6
  • 64. FOR + STEP FOR NUMBER = 1 TO 10 STEP 2 PRINT NUMBER NEXT NUMBER 1 3 5 7 9 FOR VAR = BEGIN TO END [STEP NUM] …… NEXT VAR
  • 65. Example FOR(1) PRINT "Number","Square" FOR NUM = 1 TO 10 PRINT NUM, NUM * NUM NEXT NUM END
  • 66. Example FOR(2) INPUT “1 to ” , ENDNUM PRINT "Number","Square" FOR NUM = 1 TO ENDNUM PRINT NUM, NUM * NUM NEXT NUM END
  • 67. Overflow Problem @ Factorial
  • 68. Overflow Problem @ Factorial INPUT "1 to ", ENDNUM PRINT “Factorial", “Answer" prod = 1 FOR NUM = 1 TO ENDNUM prod = prod * NUM PRINT NUM, prod NEXT NUM END Overflow
  • 69. INPUT "1 to ", ENDNUM PRINT “Factorial", “Answer" prod# = 1 FOR NUM = 1 TO ENDNUM prod# = prod# * NUM PRINT NUM, prod# NEXT NUM END Slove Overflow Problem @ Factorial STRING = "hello" INTEGER = 5 LONG = 92883 SINGLE = 39.2932 DOUBLE = String = $ Integer = % Long = & Single = ! Double = #
  • 70. Programm Looping • GOTO • FOR • DO WHILE
  • 71. DO WHILE DO WHILE Comparison ……… LOOP count = 1 DO WHILE count <= 10 PRINT count count = count + 1 LOOP
  • 72. Example (1) total = 0 number = 0 DO WHILE number <> -999 total = total + number INPUT "Enter a number (-999 to quit): "; number LOOP PRINT “Total = ”; total