SlideShare a Scribd company logo
1 of 33
สร้างปัญญา ศรัทธาความดี มหาวิทยาลัยแห่งเมืองคนดี BIZ Com รักษ์สิ่งแวดล้อม CHAPTER 5 Variable Declaration  & Constant อาจารย์ปรเมษฐ์  ดำชู คณะวิทยาการจัดการ โปรแกรมคอมพิวเตอร์ธุรกิจ 1/16
Variable Declaration การที่เราจะใช้งานตัวแปรในการเขียนโปรแกรม จะเริ่มจากการประกาศตัวแปร (Variable Declaration) ซึ่งก็คือ การตั้งชื่อตัวแปร และกำหนดชนิดข้อมูลให้กับตัวแปรแต่ละตัว 2 รูปแบบ DIMVariable-nameASData Type DIM ย่อมาจากคำว่าDimension
DIMเป็นคำสงวนที่หมายถึง การเริ่มต้นของตัวแปร ซึ่ง DIM  ย่อมาจากคำว่า Dimension  Variable-nameคือตัวแปรที่กำหนดไว้ในส่วนของ Input และ Output การตั้งชื่อตัวแปรจะมีกฏเกณฑ์ตามที่กำหนด ASเป็นตัวที่ใช้แยก Variable –name กับ Data Type Data Type คือชนิดของข้อมูลเราจะต้องกำหนดให้เหมาะสมกับข้อมูลชนิดนั้นๆ เช่น Byte Integer Long String Boolean เป็นต้น 3
Variable Name กฏเกณฑ์ในการตั้งชื่อให้กับตัวแปรใน Visual Basic 6.0 ต้องขึ้นต้นด้วยตัวอักษรภาษาอังกฤษ A-Z ตัวอักษรตัวถัดไปอาจจะเป็นA-Z หรือตัวเลข 0-9 หรือ _ ตัวอักษรตัวสุดท้ายอาจจะเป็นเครื่องหมายที่ใช้แสดงถึงประเภทของตัวแปร ได้แก่ Integer(%) Long (&) single (!) Double (#) Currency (@) หรืออาจจะไม่มีก็ได้ ชื่อของตัวแปรจะต้องไม่ซ้ำกับคำสงวน(Reserved Word) ตัวอักษรตัวพิมพ์ใหญ่และพิมพ์เล็กถือว่าเป็นตัวเดียวกัน 4
การตั้งชื่อแบบมาตรฐานจะมี  3 ชนิดด้วยกันคือ CamelCase Notation หรือ Medial Capitals เป็นการตั้งชื่อตัวแปรจะขึ้นต้นด้วยตัวเล็ก และตัวอักษรของคำถัดไปจะขึ้นต้นด้วยตัวใหญ่ backColor , iName , firstName Hungarian Notation  เป็นการตั้งชื่อโดยกำหนดตัวอักษรย่อชนิดของตัวแปรที่ใช้ โดยตัวอักษรย่อจะเป็นตัวเล็กและนำหน้าตัวแปร lName , bCheck 5
การตั้งชื่อแบบมาตรฐานจะมี  3 ชนิดด้วยกันคือ (ต่อ) Pascal Notation ตัวอักษรแรกของแต่ละคำจะเป็นตัวใหญ่ส่วนตัวอักษรอื่น ๆ ที่ได้ขึ้นต้นคำนั้นก็เป็นตัวเล็ก  เช่น BackColor , NumberValue 6
บทกลอน อย่าเอาตัวเลขนำหน้า		    อย่าให้มาตรงกับคำสงวน อย่าให้มีเครื่องหมายใดๆลอยนวล	    ไม่สมควรมีช่องว่างใดๆๆ ตัวพิมพ์เล็กพิมพ์ใหญ่มีค่าเท่ากัน	    ท้ายชื่อตัวแปรนั้นเป็นสัญลักษณ์ชนิดข้อมูล		 7 ดัดแปลง จากนิรุธ อำนวยศิลป์.เขียนโปรแกรมภาษาซี.กรุงเทพ: ดวงกมลสมัย.2548.หน้า49
ให้กาเครื่องหมาย หน้าข้อที่ตั้งชื่อได้ถูก และกาหน้าข้อที่ตั้งชื่อตัวแปรผิด พร้อมบอกด้วยว่าผิดตรงไหน Age		  ถูก		 ผิด ................................ Grade2		  ถูก ผิด ................................ 1ch		  ถูก		 ผิด ................................ SRU		  ถูก		 ผิด ................................ IF			  ถูก		 ผิด ................................ AS	    		  ถูก		 ผิด ................................ 8  ขึ้นต้นด้วยตัวเลข เป็นคำสงวน
ระดับคะแนน ถ้าตอบได้ถูกหมดเลยแสดงว่า ของเขาดีจริง ๆ ถ้าตอบผิด 2   ข้อ  Work มาก ๆ ครับ ถ้าถูกครึ่งหนึ่ง  อืม 3  ดาว  ถ้าไม่ถูกเลย   โอ้โห  E  ลอยมาแล้ว 9
7. 	Net price		  ถูก		 ผิด ........................ 8. 	Grade-2		  ถูก ผิด ........................ 9.	100-E		  ถูก		 ผิด ........................ Biz-com		  ถูก		 ผิด ........................ Black Berry	  ถูก		 ผิด ........................ 12.  BOOM^		  ถูก		 ผิด ........................ 10
ตัวอย่าง 11 ให้นักศึกษาประกาศตัวแปรของเงินเดือนเก็บข้อมูลชนิดเลขจำนวนเต็ม  DIM Salary As Integer ------------------------------------------------------------------ ให้นักศึกษาประกาศตัวแปรของเกรดเฉลี่ยโดยเก็บข้อมูลชนิดเลข จำนวนจริง  DIM Grade As Single ------------------------------------------------------------------ ให้นักศึกษาประกาศตัวแปรชื่อของนักศึกษา  DIM Name As String ------------------------------------------------------------------
ตัวอย่าง 12 ให้นักศึกษาประกาศตัวแปรของอัตราส่วนลด  DIM Discount_Rate As Single ------------------------------------------------------------------ ให้นักศึกษาประกาศตัวแปรของบ้านเลขที่  DIM Grade As String ------------------------------------------------------------------ ให้นักศึกษาประกาศตัวแปรผลการเรียน เช่น A ,B+   DIM Grade As String ------------------------------------------------------------------
จาก Flowchart ต่อไปนี้ให้ประกาศตัวแปร 13 1 START ตัวแปรอยู่นี้ Net Price = Total Price - Discount   Price , Amount day   Discount ,Total Price Total Price = Price * Amount day Discount = Total Price * (10/100)  Net Price 1 ตัวแปรอยู่นี้ STOP
14 จาก Flowchart ต่อไปนี้ให้ประกาศตัวแปร Price , Amount day   นำมาจากสัญลักษณ์ของการรับค่า DIM Price As Integer  DIMAmount_dayAs Integer
15 Memory (พื้นที่หน่วยความจำ) Integer Integer Null Null Price Amount_day
16 Net Price Output Discount ,Total Price DIM Discount As     Single จะกำหนดข้อมูลชนิดใดให้ดูที่ Process DIMTotal_priceAs Single DIMNet_priceAs Single
17 Memory (พื้นที่หน่วยความจำ) Single Single Single Null Null Discount Total_price Net_price
Constant สำหรับค่าคงที่ที่ใช้งานใน Visual Basic นั้นแบ่งได้เป็น 2 ประเภทดังนี้ ค่าคงที่แบบ User Defined ค่าคงที่แบบ Pre –Defined  18
ค่าคงที่แบบ User Defined เป็นค่าคงที่ที่เราเป็นผู้กำหนดเอง เป็นค่าคงที่ที่ใช้เฉพาะในApplication ของเรา โดยใช้คำสั่ง Const ประกาศค่าคงที่ 19 CONST ชื่อค่าคงที่ As ชนิดข้อมูล =ค่าคงที่นั้น
ค่าคงที่แบบ Pre –Defined  เป็นค่าคงที่ที่ Visual Basic ได้กำหนดค่าไว้แล้ว เราสามารถนำมาใช้ได้ทันที (เราไม่ต้องประกาศค่า) 20
การ assignment   (ส่วนหนึ่งของ Process) Assignment เป็นการกำหนดค่าให้กับตัวแปร  ซึ่ง ค่าที่กำหนดนั้นอาจจะเป็นได้ทั้งค่าหรือตัวแปรก็ได้ ดังรูปแบบต่อไปนี้  21 Variable-Name = ค่าต่างๆ หรือ ตัวแปร ความหมาย คือ นำข้อมูลด้านขวามือไปไว้ในตัวแปรทางซ้ายมือ อย่าลืมประการตัวแปรตัวนี้ด้วย
ตัวอย่าง  22 ตัวอย่าง ให้ประกาศตัวแปร เพื่อเก็บค่าของคะแนนนักศึกษา DIM Score As Single Score 2. 0 Score = 2.0 4. 0 Score Score = 4.0 10 Score = 10
ปกติเรามักจะเลือกชนิดข้อมูลให้เหมาะสมกับงาน แต่ถ้าเราไม่ได้เลือกไว้ล่วงหน้า Visual Basic เองจะตัดสินใจเองว่าจะเก็บข้อมูลที่เป็น Variant แบบใด โดยพิจารณาจากข้อมูลที่จะเก็บ หรือยึดสภาวะรอบข้างในการตัดสินใจ (Context Decision) ว่าเป็นแบบใด 23
Exp1 ให้นักศึกษาเขียนผังงานในการคำนวณหาค่าเช่าโรงแรมแห่งหนึ่งในจังหวัดสุราษฏร์ธานี ลูกค้าทุกคนได้รับส่วนลด 10 % จากค่าเช่าทั้งหมด โดยให้รับข้อมูลทางแป้นพิมพ์ และแสดงผลส่วนลดที่ได้รับออกทางจอภาพ และเงินค่าเช่าที่ต้องชำระออกทางเครื่องพิมพ์ ให้วิเคราะห์ข้อมูลประกอบด้วย  INPUT PROCESS OUTPUT ให้สร้างผังงาน (Flowchart)  ให้เขียนAlgorithm  ให้เขียน Pseudo Code 24
เฉลยการสร้างผังงาน  25 1 START Net Price = Total Price - Discount   Price , Amount day   Discount Total Price = Price * Amount day Discount = Total Price * (10/100)  Net Price 1 STOP
เฉลยการวิเคราะห์ข้อมูล  26 INPUT ราคาห้องพัก (Price)   ข้อมูลชนิด    Single จำนวนคืนที่พัก (Amount Day) ข้อมูลชนิด   Integer PROCESS ค่าห้องพัก = Price * Amount Day ส่วนลด = ค่าห้องพัก *(10/100) ราคาสุทธิ = ค่าห้องพัก – ส่วนลด OUTPUT ส่วนลด  (Discount)   ข้อมูลชนิด  single ราคาสุทธิ (Net Price)  ข้อมูลชจิด   Single
Algorithm เริ่มต้น รับค่า ราคาห้องพัก  รับค่า จำนวนคืนที่พัก ค่าห้องพัก = จำนวนคืนที่พัก * จำนวนคืนที่พัก ส่วนลด = ค่าห้องพัก *(10/100) ราคาสุทธิ = ค่าห้องพัก – ส่วนลด  แสดงค่า ของส่วนลด แสดงค่า ราคาสุทธิ จบการทำงาน 27
Pseudo Code Begin  	Input  Price   	Input  Amount day 	Total Price= Price * Amount Day  	Discount = Total Price *(10/100) 	Net price = Total Price – Discount  	Display Discount  	print Net price End 28
EXP2 ให้นักศึกษาเขียนโปแกรมเพื่อคำนวณหาอัตรากำไรโดยข้อมูลที่รับเข้ามาประมวลผลทางแป้นพิมพ์ ได้แก่ ราคาขายต่อหน่วย และต้นทุนผันแปร สำหรับสูตรที่ใช้หาอัตรากำไรส่วนเกิน มีดังนี้   อัตรากำไรส่วนเกิน =((ราคาขายต่อหน่วย – ต้นทุนผันแปรต่อหน่วย)-ราคาขายต่อหน่วย)*100 กำหนดให้แสดงผลอัตรากำไรส่วนเกินออกทางจอภาพ  ให้วิเคราะห์  Input   Process Output ให้สร้างผังงาน ให้เขียนAlgorithm  ให้เขียน Pseudo Code 29
EXP3 กำหนดให้ x = 10 ,y=20 ,z=2 จงเขียนโปรแกรมเพื่อหาค่าเฉลี่ยของX , y , z  ให้วิเคราะห์ข้อมูล Input  Process Output ให้สร้างผังงาน ให้เขียนAlgorithm  ให้เขียน Pseudo Code 30
EXP4 ให้นำเงินเดือนของพนักงาน เข้าเครื่องคอมพิวเตอร์แล้วให้เพิ่มเงินเดือนของพนักงานอีก 10% ของเงินเดือนเดิม แล้วให้แสดงผลของเงินเดือนที่เพิ่มขึ้น(10%)และเงินเดือนรวมทั้งหมด ให้วิเคราะห์ข้อมูล Input  Process Output ให้สร้างผังงาน ให้เขียนAlgorithm  ให้เขียน Pseudo Code 31
คุณธรรม จริยธรรม 32 มหาวิทยาลัยแห่งเมืองคนดี สร้างปัญญา สร้างปัญญา
สร้างปัญญา ศรัทธาความดี 33 ถาม ตอบ

More Related Content

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by HubspotMarius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTExpeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Variable Declaration & Constant