INC 161 , CPE 100 Computer Programming

Slides:



Advertisements
งานนำเสนอที่คล้ายกัน
Application Development Overview Nithi Thanon Computer Science Prince of Songkla University.
Advertisements

อัลกอริทึ่มทำงานวนซ้ำ
Control Statement if..else switch..case for while do-while.
การควบคุมทิศทางการทำงาน
Repetitive Statements (Looping)
Repetitive Or Iterative
หน่วยที่ 4: คำสั่งควบคุมโปรแกรม (Control Flow Command)
การเขียนโปรแกรมภาษาซี
โครงสร้างควบคุมการทำงาน
Week 5 การทำซ้ำโดย while loop และ do while loop
Computer Programming for Engineers
Chapter 5 การทำซ้ำโดย while loop และ do while loop
Week 5 การทำซ้ำโดย while loop และ do while loop
หลักสูตรอบรมครู คอมพิวเตอร์ หลักสูตรอบรมครู คอมพิวเตอร์ หลักสูตรที่ ๑ ทักษะการโปรแกรม เบื้องต้น วันที่สาม.
สาขาวิชาเทคโนโลยี สารสนเทศ คณะเทคโนโลยีสารสนเทศ และการสื่อสาร.
Computer Programming Asst. Prof. Dr. Choopan Rattanapoka
คำสั่งวนซ้ำ.
Programming & Algorithm
Function ธนวัฒน์ แซ่ เอียบ. What is a function ฟังก์ชันในภาษา C เป็นโปรแกรมที่ถูกออกแบบมาเพื่อ ใช้แก้ปัญหางานใดงานหนึ่งโดยเฉพาะ ฟังก์ชันจะเปลี่ยน input.
Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
Computer Programming การเขียนโปรแกรม คอมพิวเตอร์ สัปดาห์ที่ 6 คำสั่งควบคุมการทำงานแบบ เงื่อนไขและคำสั่งควบคุมการ ทำงานแบบวนซ้ำ.
หลักการโปรแกรม 1 Lecture 8: การทำซ้ำ (while, do-while)
1 exit() and break C++ provides a way to leave a program early (before its natural finish) with the exit() function. The format of exit() is as follows:
โครงสร้างภาษาซี C ++ structure
บทที่ 1 บทนำ 1.1 ฮาร์ดแวร์คอมพิวเตอร์ 1.2 ซอฟต์แวร์คอมพิวเตอร์
Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
Computer Programming I โดย อ.วิมลศรี เกตุโสภณ สาขาวิชาระบบสารสนเทศ
การประมวลผลแบบวน ( LOOP )
การเขียนโปรแกรมด้วยภาษา C
C Programming By Mr. Sanae Sukprung.
การควบคุมทิศทางการทำงาน
Chapter 4 ข้อความสั่ง เลือกทำ.
คำสั่งวนซ้ำ (Looping)
Concept of Programing.
Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
บทที่ 6 การเขียนโปรแกรมแบบมีเงื่อนไข
Chapter 9 ตัวชี้ pointer.
Control Statements.
ใช้สำหรับ Turbo C++ Version 3.0
Data Structure & Algorithm Concept
การเขียนผังงาน (Flowchart)
ภาษา C เบื้องต้น.
การแสดงขั้นตอนวิธีด้วยรหัสเทียม (Pseudo-Code)
การควบคุมการทำงานด้วยภาษา C
Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
โครงสร้างโปรแกรมภาษา C
Computer Programming การเขียนโปรแกรมคอมพิวเตอร์
คำอธิบายรายวิชา การเขียนผังงาน รหัสเทียม ตรรกศาสตร์เบื้องต้น การเขียนโปรแกรมคอมพิวเตอร์แบบโครงสร้าง ชนิดตัวแปร ตัวดำเนินการทางตรรกะ ตัวดำเนินการเปรียบเทียบ.
ความรู้พื้นฐานการเขียนโปรแกรม
บทที่ 4 ตัวแปร (Variables)
บทที่ 2 ขั้นตอนการทำงาน (Algorithm)
การออกแบบระบบ System Design.
Principles of Problem Solving and Basic Programming หลักการแก้ปัญหาและการเขียนโปรแกรมเบื้องต้น2(1-2-3) สัปดาห์ที่ 13 การเขียนรหัสเทียม (Pseudo Code)
บทที่ 7 การเขียนโปรแกรม แบบวนรอบทำซ้ำ (Loop) Part1
Problem Solving ขั้นตอนวิธีและการแก้ปัญหาสำหรับวิทยาการคอมพิวเตอร์
การเขียนผังงาน (Flow Chart)
Lecture no. 1: Introduction to Computer and Programming
Chapter 7 ฟังก์ชัน Function.
บทที่ 7 การเขียนโปรแกรม แบบวนรอบทำซ้ำ (Loop) Part2
บทที่ 3 โครงสร้างควบคุม Control Structures
ขั้นตอนการเขียนโปรแกรมคอมพิวเตอร์
บทที่ 6 การเขียนผังงาน (Flowchart)
Introduction to Computer Programming
การเขียนโปรแกรมภาษา Java (ต่อ)
การสร้างผังงานโปรแกรม
การวิเคราะห์โจทย์ปัญหา (Problem Analysis)
Lecture 7: ทางเลือกแบบมีโครงสร้างซ้อนใน
(Flowchart) ผังงาน.
อัลกอริทึม (Algorithm) ???
ใบสำเนางานนำเสนอ:

INC 161 , CPE 100 Computer Programming Lecture 4 Flow Control

Flow Control Commands Normally, commands are run top-down every command in the list is executed. With flow control, some commands can be skipped or rerun according to the condition.

การเขียนผังงาน(Flowcharting) ผังงาน หมายถึง เครื่องมือที่ช่วยในการเขียนโปรแกรม โดยเขียนเป็นเครื่องหมายภาพสัญลักษณ์แสดงลำดับขั้นตอนการทำงาน ผู้เขียนโปรแกรมจะสามารถเข้าใจลำดับขั้นตอนการเขียนโปรแกรมได้อย่างรวดเร็วและง่ายขึ้น และง่ายต่อการตรวจสอบความถูกต้องของลำดับขั้นตอนในวิธีการประมวลผล

ตัวอย่างผังงานทั่วไป(1/3) เริ่มต้น โยนเหรียญ ผลการโยน ได้เงิน 10 บาท เสียเงิน 10 บาท ครบ 3 ครั้ง หรือยัง? หยุด หัว ก้อย ยัง ครบ แสดงการโยนเหรียญ 3 ครั้ง ถ้าออกหัว ผู้โยนจะได้เงิน 10 บาท ถ้าออกก้อย ผู้โยนจะเสียเงิน 10 บาท

ตัวอย่างผังงานทั่วไป(2/3) ผังงานแสดงการเดินข้ามถนนที่มีสัญญาณไฟจราจร เริ่มต้น รอสัญญาณไฟ ไฟแดง หรือไม่ เดินข้ามถนน หยุด ใช่ ไม่ใช่

ตัวอย่างผังงานทั่วไป(3/3) เริ่มต้น อายุต่ำกว่า 5 ปี ห้ามรับประทาน อายุ 5 ปีขั้นไป แต่ไม่ถึง 8 ปี ครั้งละ 1 เม็ด อายุ 8 ปีขั้นไป แต่ไม่ถึง 15 ปี ครั้งละ 2 เม็ด ครั้งละ 3 เม็ด หยุด ใช่ ไม่ใช่ ผังงานพิจารณาการใช้ยาตามฉลากยาที่ปิดข้างขวด แยกตามขนาดการใช้ดังนี้ อายุต่ำกว่า 5 ปี ห้ามรับประทาน อายุ 5 - 7 ปี ครั้งละ 1 เม็ด อายุ 8 - 14 ปี ครั้งละ 2 เม็ด อายุ 15 ปีขึ้นไป ครั้งละ 3 เม็ด

ผังงานทางคอมพิวเตอร์ ผังงานทางคอมพิวเตอร์แบ่งออกเป็น 2 ประเภท ได้แก่ ผังงานระบบ (System flowchart) ผังงานโปรแกรม (Program flowchart)

ผังงานระบบ(System flowchart) เป็นผังงานที่ใช้แสดงขั้นตอนการทำงานของระบบงานอย่างกว้าง ๆ ทั้งระบบ โดยแสดงให้เห็นถึงความสัมพันธ์ของส่วนสำคัญต่าง ๆ ในระบบนั้น เช่น เอกสารเบื้องต้น หรือสื่อบันทึกที่ใช้อยู่เป็นอะไร และผ่านไปยังหน่วยงานใด มีกิจกรรมอะไรในหน่วยงานนั้น ๆ แล้วส่งต่อไปยังหน่วยงานใด เป็นต้น ดังนั้น ผังงานระบบ จะเกี่ยวข้องกับ คน วัสดุ และเครื่องจักร โดยแสดงการนำข้อมูลเข้า ประมวลผล แสดงผลลัพธ์ ว่ามาจากที่ใดกว้าง ๆ

ตัวอย่างผังงานระบบ ผังงานแสดงการคำนวณพื้นที่สามเหลี่ยมจำนวน 100 รูป เริ่มต้น บันทึกความสูงและความยาวฐาน ลงในดิสก์ รับความสูง และความยาวฐาน ของสามเหลี่ยม 100 รูป ดิสก์ที่ บันทึกแล้ว คำนวณพื้นที่ของสามเหลี่ยม พิมพ์รายงานของ พื้นที่สามเหลี่ยม จบ ผังงานแสดงการคำนวณพื้นที่สามเหลี่ยมจำนวน 100 รูป

ผังงานโปรแกรม(Program flowchart) เป็นผังงานที่แสดงขั้นตอนของคำสั่งที่ใช้ในโปรแกรม ผังงานนี้แยกย่อยมาจากผังงานระบบ โดยผู้เขียนโปรแกรมจะดึงเอาแต่ละจุดที่เกี่ยวข้องกับการประมวลผลในคอมพิวเตอร์ที่ปรากฏในผังงานระบบมาเขียนแสดงรายละเอียดในการประมวลผลอย่างเป็นลำดับขั้นตอน เพื่อนำไปประกอบการเขียนโปรแกรมต่อไป

ตัวอย่างผังงานโปรแกรม เริ่มต้น รับความสูง และ ความยาวฐานทีละรูป ครบ 100 รูปแล้ว ใช่หรือไม่ คำนวณพื้นที่= 0.5 * ฐาน * สูง พิมพ์พื้นที่สามเหลี่ยม ทีละรูป จบ ไม่ใช่ ใช่ ผังงานแสดงการคำนวณพื้นที่สามเหลี่ยมจำนวน 100 รูป

เปรียบเทียบผังงานระบบและผังงานโปรแกรม เริ่มต้น บันทึกความสูงและความยาวฐาน ลงในดิสก์ รับความสูง และความยาวฐาน ของสามเหลี่ยม 100 รูป ดิสก์ที่ บันทึกแล้ว คำนวณพื้นที่ของสามเหลี่ยม พิมพ์รายงานของ พื้นที่สามเหลี่ยม จบ เริ่มต้น รับความสูง และ ความยาวฐานทีละรูป ครบ 100 รูปแล้ว ใช่หรือไม่ คำนวณพื้นที่= 0.5 * ฐาน * สูง พิมพ์พื้นที่สามเหลี่ยม ทีละรูป จบ ไม่ใช่ ใช่ ผังงานระบบ ผังงานโปรแกรม

ประโยชน์ของการเขียนผังงาน เราสามารถเรียนรู้และเข้าใจผังงานได้ง่าย เพราะผังงานไม่ขึ้นอยู่กับภาษาคอมพิวเตอร์ภาษาใดภาษาหนึ่ง ผังงานเป็นการสื่อความหมายด้วยภาพ ทำให้ง่ายและสะดวกต่อการพิจารณาลำดับขั้นตอนในการทำงานดีกว่าการบรรยายเป็นตัวอักษร ช่วยให้การค้นหาความผิดพลาดของลำดับการทำงานได้รวดเร็ว และสามารถแก้ไขได้ง่าย การเขียนโปรแกรมโดยพิจารณาจากผังงาน สามารถทำได้รวดเร็วและง่ายขึ้น ในการบำรุงรักษาโปรแกรม หรือมีการเปลี่ยนแปลงแก้ไขโปรแกรมให้มีประสิทธิภาพ ถ้าดูจากผังงานจะช่วยให้สามารถทบทวนการทำงานในโปรแกรมได้ง่ายขึ้น

สัญลักษณ์ในการเขียนผังงาน การเขียนผังงานเป็นการนำเอาภาพสัญลักษณ์ต่าง ๆ มาเรียงต่อกัน เพื่อแสดงลำดับขั้นตอนการทำงาน โดยมีเส้นลูกศรเชื่อมระหว่างภาพต่างๆ สัญลักษณ์ในการเขียนผังงานที่นิยมใช้กันนั้น เป็นสัญลักษณ์ของหน่วยงานที่ชื่อว่า American National Standard Institute (ANSI) และ International Standard Organization (ISO) เป็นผู้กำหนดและรวบรวมให้เป็นสัญลักษณ์มาตรฐานในการเขียนผังงาน

สัญลักษณ์ในการเขียนผังงาน ความหมาย ตัวอย่างการใช้ คำอธิบาย การเริ่มต้นหรือสิ้นสุดการเขียนผังงาน (Terminal) เริ่มต้นผังงาน จบผังงาน รับข้อมูลหรือแสดงข้อมูลโดยไม่ระบุสื่อ (Input/output) รับค่าใส่ในตัวแปรชื่อ name แสดงค่าจากตัวแปร area START STOP read name display area

สัญลักษณ์ในการเขียนผังงาน ความหมาย ตัวอย่างการใช้ คำอธิบาย การประมวลผล (Process) คำนวณ A + B และเก็บไว้ใน C กำหนดค่า sum เท่ากับ 0 การเปรียบเทียบหรือตัวสินใจ (Compare / Decision) เปรียบเทียบถ้า i มีค่า น้อยกว่าหรือเท่ากับ 10 - เป็นจริง พิมพ์ค่า i เสร็จแล้วไปทำคำสั่งอื่น ๆ - เป็นเท็จ ไปทำคำสั่งอื่น ๆ C = A + B Sum = 0 i <= 10 true false แสดง i

สัญลักษณ์ในการเขียนผังงาน ความหมาย ตัวอย่างการใช้ คำอธิบาย จุดต่อเนื่องในหน้าเดียวกัน (In-Page connector) หลังจากพิมพ์ค่า A แล้วให้ทำตามที่จุด ต่อเนื่อง A ซึ่งอยู่ใน หน้าเดียวกัน จุดต่อเนื่องที่อยู่คนละหน้า (Off-Page Connector) หลังจากกำหนดค่า A เท่ากับ 3 ให้ทำตาม จุดต่อเนื่องชื่อ 1 ซึ่ง ไม่ได้อยู่ในหน้า เดียวกัน write A A A = 3 1

หลักการจัดภาพผังงาน(1/2) ทิศทางของผังงานจะเริ่มจากส่วนบนของหน้ากระดาษลงมายังส่วนล่าง บน START ทิศทางของเส้น read age age > 60 false true “You are old” ล่าง STOP

หลักการจัดภาพผังงาน(2/2) สัญลักษณ์หรือภาพที่ใช้ในการเขียนผังงานมีขนาดต่าง ๆ กันได้ แต่จะต้องมีรูปมาตรฐานตามความหมายที่กำหนด START sum = 0 ขนาดของสัญลักษณ์ ปรับให้เหมาะสมกับข้อความในสัญลักษณ์ i = 1 to 10 sum = sum + i i sum STOP

Selection Statements If Statements Example: The syntax for an if-statement is as follows: if(expression) statement The controlling expression of an if statement should have a scalar type. The statement is executed if the expression compared is unequal to 0. Example: int i = 1; if(i) { i = 0; }

Flowchart for an if-Statement The syntax for an if-statement is as follows: if(expression) statement

If-else Statements The syntax for an if-else statement is as follows: if(expression) statement1 else statement2 The controlling expression of an if-statement should have a scalar type. The statement1 is executed if the expression compared is unequal to 0, else statement2 is executed.

Flowchart of an if-else Statement The syntax for an if-else statement is as follows: if(expression) statement1 else statement2

- The syntax for the else-if statement is as follows: Else-if Statements - The syntax for the else-if statement is as follows: if(expression1) statement1 else if(expression2) statement2 else if(expression3) statement3 else statement4 - Semantically, the syntax of the else-if statement is an extension of the previous if-else statement.

Flowchart of an else-if Statement The syntax for an else-if statement is as follows: if(expression1) statement1 else if(expression2) statement2 else if(expression3) statement3 else statement4

Calculate the score based on a grade. /* File: ifgrade.c */ #include <stdio.h> main() { char grade; /* grade */ double score; /* score */ printf("Enter a grade [A, B, C, D, F]: "); scanf("%c", &grade); if(grade == 'A') /* entered A */ score = 4.0; else if(grade == 'B') /* entered B */ score = 3.0; else if(grade == 'C') /* entered C */ score = 2.0; else if(grade == 'D') /* entered D */ score = 1.0; else if(grade == 'F') /* entered F */ score = 0.0; /* entered any other character */ else { score = -1; printf("Invalid grade '%c'\n", grade); } if(score != -1) printf("The score for the grade '%c' is %.2f\n", grade, score); Calculate the score based on a grade.

Iteration Statements While Loop The syntax of a while statement is as follows: The evaluation of the controlling expression takes place before each execution of the loop body. The loop body is executed repeatedly until the return value of the controlling expression is equal to 0. while(expression) statement

Flowchart of a while Loop The syntax of a while loop is as follows: while(expression) statement

int i = 0; while(i < 5){ printf(“%d “, i); i++; } Example: Output: 0 1 2 3 4

Example 1 : Program to find factorial 1 ! = 1 2 ! = 1x2 3 ! = 1x2x3 4 ! = 1x2x3x4

Find factorial n= 4 f = 1 i = 2 f = 2 i = 3 f = 6 i = 4 f = 24 i = 5

/* processing */ Example: Calculating a factorial 5!. The factorial n! is defined as n*(n-1)! /* File: whilec.c */ #include <stdio.h> main() { /* declaration */ unsigned int i, f, n; /* initialization */ i = 1; f = 1; /* processing */ printf(“Please input a number\n”); scanf(“%d”, &n); while (i <= n) { f *= i; i++; } /* termination */ printf(“factorial %d! = %d\n", n, f); Execution and Output: > whilec.c 5 factorial 5! = 120

There are 2 popular ways to control a loop Control of Repetition There are 2 popular ways to control a loop Counter-controlled repetition Sentinel-controlled repetition

Counter-controlled repetition Loop repeated until counter reaches a certain value. Definite repetition: number of repetitions is known Example: A student takes four courses in a quarter. Each course will be assigned a grade with the score from 0 to 4. Develop a C program to calculate the grade point average (GPA) for the quarter.

Calculate GPA By finding an Average of 4 subjects

> gpa.c Preprocessing NUM ≡ 4 Execution and Output: /* File: gpa.c */ #include <stdio.h> #define NUM 4 main() { /* declaration */ int count; double grade, total, gpa; /* initialization */ count = 0; total = 0; /* processing */ while(count < NUM) { printf("Enter a grade: "); scanf("%lf", &grade); total += grade; count++; } /* termination */ gpa = total/NUM; printf("The GPA is: %f\n", gpa); Preprocessing NUM ≡ 4 Execution and Output: > gpa.c Enter a grade: 4 Enter a grade: 3.7 Enter a grade: 3.3 The GPA is: 3.750000

Sentinel-controlled repetition Loop repeated until the sentinel (signal) value is entered. Indefinite repetition: number of repetitions is unknown when the loop begins execution. Example: Develop a GPA calculation program that will process grades with scores in the range of [0, 4] for an arbitrary number of courses.

Flowchart for Calculate grade Sentinel number = -1 When -1 is entered, the program exit.

Do-While Loop The syntax of a do-while statement is as follows: The evaluation of the controlling expression takes place after each execution of the loop body. The loop body is executed repeatedly until the return value of the controlling expression is equal to 0. do statement while(expression);

Flowchart of a do-while loop The syntax of a do-while statement is as follows: do statement while(expression);

Example: int i = 0; do { printf(“%d ”, i); i++; } while(i < 5); Output: 0 1 2 3 4

10 What is the output of the following example? Example: int i = 10; do { printf(“%d ”, i); i++; } while(i < 5); Output: 10