Skip to content · ⁨ข้ามไปยังเนื้อหา⁩

Selection and Iteration · ⁨การเลือกและการทำซ้ำ⁩

AP Computer Science A · Topic 2 · ⁨หัวข้อ 2⁩

Video lesson for this topic · ⁨บทเรียนวิดีโอสำหรับหัวข้อนี้⁩ Open the video page · ⁨เปิดหน้าวิดีโอ⁩
7:59

การเลือกและการทำซ้ำ

นี่คือลูปสามตัว. ต่างกันเพียงหนึ่ง字符 — น้อยกว่าแทนที่จะใช้น้อยกว่าหรือเท่ากับ, มากกว่าแทนที่จะใช้น้อยกว่า. ตัวแรกจะทำงาน…

English narration · English + 中文 subtitles burned in · ⁨การบรรยายภาษาอังกฤษ · คำบรรยายภาษาอังกฤษ + 中文 ลอยตัวบนภาพ⁩

2.1

Selection and Repetition in Algorithms · ⁨การเลือกและการซ้ำในอัลกอริทึม⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 2.1.A: Represent patterns and algorithms that involve selection and repetition found in everyday life using written language or diagrams.

  • 2.1.A.1 The building blocks of algorithms include sequencing, selection, and repetition.
  • 2.1.A.2 Algorithms can contain selection, through decision making, and repetition, via looping.
  • 2.1.A.3 Selection occurs when a choice of how the execution of an algorithm will proceed is based on a true or false decision.
  • 2.1.A.4 Repetition is when a process repeats itself until a desired outcome is reached.
  • 2.1.A.5 The order in which sequencing, selection, and repetition are used contributes to the outcome of the algorithm.
ไทย

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

  • 2.1.A.1 ส่วนประกอบพื้นฐานของอัลกอริทึมรวมถึงการเรียงลำดับ การเลือก และการซ้ำซ้อน
  • 2.1.A.2 อัลกอริทึมอาจมีการเลือก ผ่านการตัดสินใจ และการซ้ำซ้อน ผ่านการวนลูป
  • 2.1.A.3 การเลือก เกิดขึ้นเมื่อการเลือกวิธีการดำเนินการของอัลกอริทึมขึ้นอยู่กับคำตัดสินที่เป็นจริงหรือเท็จ
  • 2.1.A.4 การซ้ำซ้อน คือกระบวนการที่ทำซ้ำตัวเองจนกว่าจะได้ผลลัพธ์ที่ต้องการ
  • 2.1.A.5 ลำดับที่ใช้ในการเรียงลำดับ การเลือก และการซ้ำซ้อนส่งผลต่อผลลัพธ์ของอัลกอริทึม

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

Algorithms are built from three control structures 控制结构: sequence (steps in order), selection 选择 (choosing a path), and iteration 迭代 (repeating steps). This topic covers selection and iteration – the tools that let a program make decisions and loop.

ไทย
แผนภาพการไหลที่มีเพชรตัดสินใจ: การเลือกช่วยกำหนดเส้นทางที่อัลกอริทึมจะผ่าน
แผนภาพการไหลที่มีเพชรตัดสินใจ: การเลือกช่วยกำหนดเส้นทางที่อัลกอริทึมจะผ่าน

อัลกอริทึมสร้างจาก โครงสร้างควบคุม สามชนิดคือ sequence (ขั้นตอนตามลำดับ), selection (การเลือกเส้นทาง), และ iteration (การทำซ้ำขั้นตอน) หัวข้อนี้อธิบายถึง selection และ iteration – ซึ่งเป็นเครื่องมือที่ทำให้โปรแกรมสามารถตัดสินใจและวนลูปได้

โครงสร้างควบคุมสามชนิด: sequence, selection, และ iteration
โครงสร้างควบคุมสามชนิด: sequence, selection, และ iteration
Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
control structures/kənˈtrəʊl ˈstrʌktʃəz/ โครงสร้างการควบคุม
selection/sɪˈlekʃn/ การคัดเลือก
iteration/ˌɪtəˈreɪʃn/ การวนซ้ำ
boolean expression/ˈbuːlɪən ekˈspreʃn/ นิพจน์บูลีน
2.2

Boolean Expressions · ⁨นิพจน์ boolean⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 2.2.A: Develop code to create Boolean expressions with relational operators and determine the result of these expressions.

  • 2.2.A.1 Values can be compared using the relational operators == and != to determine whether the values are the same. With primitive types, this compares the actual primitive values. With reference types, this compares the object references.
  • 2.2.A.2 Numeric values can be compared using the relational operators <, >, <=, and >= to determine the relationship between the values.
  • 2.2.A.3 An expression involving relational operators evaluates to a Boolean value.
ไทย

วัตถุประสงค์การเรียนรู้ 2.2.A: เขียนโค้ดเพื่อสร้าง布尔 expressions พร้อมตัวดำเนินการเปรียบเทียบ และกำหนดผลลัพธ์ของ布尔 expressions เหล่านี้

  • 2.2.A.1 ค่าสามารถเปรียบเทียบกันได้โดยใช้ ตัวดำเนินการเปรียบเทียบ (relational operators) == และ != เพื่อดำเนินการว่าค่าเหล่านั้นเท่ากันหรือไม่对于 primitive types การเปรียบเทียบนี้จะตรวจสอบค่า primitive จริง สำหรับ reference types การเปรียบเทียบนี้จะตรวจสอบการอ้างอิงไปยัง object
  • 2.2.A.2 ค่าทางคณิตศาสตร์สามารถเปรียบเทียบกันได้โดยใช้ตัวดำเนินการเปรียบเทียบ <, >, <= และ >= เพื่อดำเนินการความสัมพันธ์ระหว่างค่าเหล่านี้
  • 2.2.A.3布尔 expression ที่เกี่ยวข้องกับตัวดำเนินการเปรียบเทียบจะมีผลลัพธ์เป็น布尔 value

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English
Logic gates & the half-adder

A boolean expression 布尔表达式 evaluates to true or false, using relational operators 关系运算符: == (equal), != (not equal), <, >, <=, >=. Note == compares primitive values but object references for objects, so use .equals for Strings.

ไทย
Logic gates & the half-adder

นิพจน์ boolean ให้ผลลัพธ์เป็น true หรือ false โดยใช้ relational operators: == (เท่ากับ), != (ไม่เท่ากัน), <, >, <=, >=. หมายเหตุว่า == เปรียบเทียบ primitive values แต่ใช้สำหรับ object references ของ objects ดังนั้นให้ใช้ .equals สำหรับ Strings.

ครอบครัวของ operator สามกลุ่ม: arithmetic, relational, และ logical
ครอบครัวของ operator สามกลุ่ม: arithmetic, relational, และ logical
Explore · ⁨สำรวจ⁩

Explore the AND truth table · ⁨สำรวจตารางความจริงของ AND⁩

A Boolean expression evaluates to true or false. AND is true only when both operands are true; toggle the inputs to see all four cases. · ⁨นิพจน์บูลีน ให้ผลลัพธ์เป็น true หรือ false AND จะเป็นจริงก็ต่อเมื่อ ทั้งสอง ตัว operand เป็นจริง; สลับอินพุตเพื่อดูกรณีทั้งสี่⁩

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
relational operators/rɪˈleɪʃənl ˈɒpəreɪtəz/ ตัวเปรียบเทียบเชิงสัมพันธ์
if statement/ɪf ˈsteɪtmənt/ คำสั่ง if
2.3

The if Statement · ⁨คำสั่ง if⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 2.3.A: Develop code to represent branching logical processes by using selection statements and determine the result of these processes.

  • 2.3.A.1 Selection statements change the sequential execution of statements.
  • 2.3.A.2 An if statement is a type of selection statement that affects the flow of control by executing different segments of code based on the value of a Boolean expression.
  • 2.3.A.3 A one-way selection (if statement) is used when there is a segment of code to execute under a certain condition. In this case, the body is executed only when the Boolean expression is true.
  • 2.3.A.4 A two-way selection (if-else statement) is used when there are two segments of code—one to be executed when the Boolean expression is true and another segment for when the Boolean expression is false. In this case, the body of the if is executed when the Boolean expression is true, and the body of the else is executed when the Boolean expression is false.
ไทย

วัตถุประสงค์การเรียนรู้ 2.3.A: เขียนโค้ดเพื่อแสดงกระบวนการตัดสินใจเชิงตรรกะด้วยการใช้ selection statements และกำหนดผลลัพธ์ของกระบวนการเหล่านี้

  • 2.3.A.1 Selection statements เปลี่ยนแปลงลำดับการดำเนินการของคำสั่ง
  • 2.3.A.2 คำสั่ง if เป็นประเภทหนึ่งของ selection statements ที่มีผลต่อการควบคุมการทำงานโดยดำเนินการส่วนต่าง ๆ ของโค้ดตามค่าของ布尔 expression
  • 2.3.A.3 การเลือกแบบทางเดียว (one-way selection) หรือคำสั่ง if ใช้เมื่อมีส่วนหนึ่งของโค้ดที่ต้องการดำเนินการภายใต้เงื่อนไข tertentu ในกรณีนี้ ตัวจะดำเนินการก็ต่อเมื่อ布尔 expression มีค่าเป็น true เท่านั้น
  • 2.3.A.4 การเลือกแบบสองทาง (two-way selection) หรือคำสั่ง if-else ใช้เมื่อมีสองส่วนของโค้ด—one ส่วนสำหรับการดำเนินการเมื่อ布尔 expression มีค่าเป็น true และอีกส่วนหนึ่งสำหรับการดำเนินการเมื่อ布尔 expression มีค่าเป็น false ในกรณีนี้ ตัวของ if จะดำเนินการเมื่อ布尔 expression มีค่าเป็น true และตัวของ else จะดำเนินการเมื่อ布尔 expression มีค่าเป็น false

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

An if statement 条件语句 runs a block only when its condition is true; an optional else gives an alternative:

ไทย

คำสั่ง if จะรันบล็อกเมื่อเงื่อนไขเป็นจริงเท่านั้น; ส่วนเสริม else ให้ทางเลือกอื่น:

if (score >= 60) {
    System.out.println("Pass");
} else {
    System.out.println("Fail");
}
ไฟจราจร: การเลือกช่วยกำหนดว่า branch nàoจะถูก اجرا เหมือนกับคำสั่ง if ที่เลือกเส้นทางโค้ด
ไฟจราจร: การเลือกช่วยกำหนดว่า branch nàoจะถูก اجرا เหมือนกับคำสั่ง if ที่เลือกเส้นทางโค้ด
Explore · ⁨สำรวจ⁩

See which branch an if chooses · ⁨ดูว่า if เลือกทางไหน⁩

An if statement runs its body only when the condition is true, otherwise it skips to else. Slide the score across the boundaries and watch the grade change. · ⁨if statement จะรันโค้ดภายในก็ต่อเมื่อเงื่อนไขเป็นจริง มิฉะนั้นจะข้ามไป else เลื่อนคะแนนข้ามขอบเขตและดูเกรดเปลี่ยนไป⁩

2.4

Nested if Statements

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 2.4.A: Develop code to represent nested branching logical processes and determine the result of these processes.

  • 2.4.A.1 Nested if statements consist of if, if-else, or if-else-if statements within if, if-else, or if-else-if statements.
  • 2.4.A.2 The Boolean expression of the inner nested if statement is evaluated only if the Boolean expression of the outer if statement evaluates to true.
  • 2.4.A.3 A multiway selection (if-else-if) is used when there are a series of expressions with different segments of code for each condition. Multiway selection is performed such that no more than one segment of code is executed based on the first expression that evaluates to true. If no expression evaluates to true and there is a trailing else statement, then the body of the else is executed.
ไทย

วัตถุประสงค์การเรียนรู้ 2.4.A: เขียนโค้ดเพื่อแสดงกระบวนการตัดสินใจเชิงตรรกะแบบซ้อนและกำหนดผลลัพธ์ของกระบวนการเหล่านี้

  • 2.4.A.1 คำสั่ง if แบบซ้อน (nested statements) ประกอบด้วย if, if-else หรือ if-else-if语句ภายใน if, if-else หรือ if-else-if语句
  • 2.4.A.2布尔 expression ของ命令ifแบบซ้อนด้านในจะถูกประเมินก็ต่อเมื่อ布尔 expression ของ命令ifภายนอกมีค่าเป็นtrueเท่านั้น
  • 2.4.A.3 การเลือกหลายทาง (multiway selection) หรือif-else-ifใช้เมื่อมี سلسلةของ布尔 expressionsพร้อมส่วนหนึ่งของโค้ดสำหรับแต่ละเงื่อนไข การเลือกหลายทางดำเนินการโดยให้ดำเนินการส่วนหนึ่งของโค้ดไม่เกินหนึ่งส่วนตาม布尔 expression แรกที่มีค่าเป็นtrue หากไม่มี布尔 expressionใดมีค่าเป็นtrueและมีcommand else ตามท้าย แล้วตัวของelseจะถูกดำเนินการ

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

Placing an if inside another, or chaining with else if, tests several cases in order. Only the first matching branch runs:

ไทย

การวาง if ไว้ภายในอีกอันหนึ่ง หรือเชื่อมต่อกันด้วย else if เพื่อทดสอบหลายกรณีตามลำดับ จะมีการรันเฉพาะ branch แรก ที่ตรงกันเท่านั้น:

if (g >= 90) grade = 'A';
else if (g >= 80) grade = 'B';
else grade = 'C';
2.5

Compound Boolean Expressions

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 2.5.A: Develop code to represent compound Boolean expressions and determine the result of these expressions.

  • 2.5.A.1 Logical operators ! (not), && (and), and || (or) are used with Boolean expressions. The expression !a evaluates to true if a is false and evaluates to false otherwise. The expression a && b evaluates to true if both a and b are true and evaluates to false otherwise. The expression a || b evaluates to true if a is true, b is true, or both, and evaluates to false otherwise. The order of precedence for evaluating logical operators is ! (not), && (and), then || (or). An expression involving logical operators evaluates to a Boolean value.
  • 2.5.A.2 Short-circuit evaluation occurs when the result of a logical operation using && or || can be determined by evaluating only the first Boolean expression. In this case, the second Boolean expression is not evaluated.
ไทย

วัตถุประสงค์การเรียนรู้ 2.5.A: เขียนโค้ดเพื่อแสดงสมการ布尔แบบผสมและหาค่าผลลัพธ์ของสมการเหล่านี้

  • 2.5.A.1 ตัวดำเนินการตรรกะ ! (not), && (and), และ || (or) ใช้ร่วมกับสมการ布尔 สมการ !a จะให้ค่าเป็น true หาก a มีค่าเป็น false และจะให้ค่าเป็น false ในกรณีอื่น ๆ สมการ a && b จะให้ค่าเป็น true หากทั้ง a และ b มีค่าเป็น true และจะให้ค่าเป็น false ในกรณีอื่น ๆ สมการ a || b จะให้ค่าเป็น true หาก a มีค่าเป็น true, b มีค่าเป็น true, หรือทั้งสองอย่าง และมีค่าเป็น false ในกรณีอื่น ๆ ลำดับความสำคัญในการประเมินค่าตัวดำเนินการตรรกะคือ ! (not), && (and), แล้วตามด้วย || (or) สมการที่เกี่ยวข้องกับตัวดำเนินการตรรกะจะให้ค่าเป็น布尔值 (boolean value)
  • 2.5.A.2 การประเมินแบบลัด (short-circuit evaluation) เกิดขึ้นเมื่อผลลัพธ์ของการดำเนินการตรรกะที่ใช้ && หรือ || สามารถระบุได้จากการประเมินเพียงสมการ布尔แรกเท่านั้น ในกรณีนี้ สมการ布尔ที่สองจะไม่ถูกประเมิน

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English
Short-circuit evaluation

Logical operators 逻辑运算符 combine conditions: && (and – both true), || (or – at least one true), ! (not – reverse). Java uses short-circuit evaluation 短路求值: && stops if the left side is false, and || stops if the left side is true – useful to guard against errors, e.g. if (n != 0 && total / n > 5).

ไทย
Short-circuit evaluation

Logical operators รวมเงื่อนไขเข้าด้วยกัน: && (and – ทั้งคู่เป็นจริง), || (or – อย่างน้อยหนึ่งเป็นจริง), ! (not – กลับด้าน) Java ใช้ short-circuit evaluation: && จะหยุดหากฝั่งซ้ายเป็น false, และ || จะหยุดหากฝั่งซ้ายเป็นจริง – มีประโยชน์ในการป้องกันข้อผิดพลาด เช่น if (n != 0 && total / n > 5)

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
Logical operators/ˈlɒdʒɪkl ˈɒpəreɪtəz/ ตัวดำเนินการทางตรรกะ
short-circuit evaluation/ʃɔːt ˈsɜːkɪt ɪˌvæljuːˈeɪʃn/ การประเมินแบบวงจรสั้น
2.6

Comparing Boolean Expressions · ⁨เปรียบเทียบนิพจน์ boolean⁩

Syllabus · ⁨หลักสูตร⁩
Learning ObjectiveEssential Knowledge

2.6.A
Compare equivalent Boolean expressions.

  • 2.6.A.1 Two Boolean expressions are equivalent if they evaluate to the same value in all cases. Truth tables can be used to prove Boolean expressions are equivalent.
  • 2.6.A.2 De Morgan's law can be applied to Boolean expressions to create equivalent Boolean expressions. Under De Morgan's law, the Boolean expression !(a && b) is equivalent to !a || !b and the Boolean expression !(a || b) is equivalent to !a && !b.

2.6.B
Develop code to compare object references using Boolean expressions and determine the result of these expressions.

  • 2.6.B.1 Two different variables can hold references to the same object. Object references can be compared using == and !=.
  • 2.6.B.2 An object reference can be compared with null, using == or !=, to determine if the reference actually references an object.
  • 2.6.B.3 Classes often define their own equals method, which can be used to specify the criteria for equivalency for two objects of the class. The equivalency of two objects is most often determined using attributes from the two objects.
    • Exclusion statement: Overriding the equals method is outside the scope of the AP Computer Science A course and exam.

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

De Morgan's laws 德摩根定律 rewrite negations: !(a && b) equals !a || !b, and !(a || b) equals !a && !b. Two boolean expressions are equivalent if they give the same result for every input – a truth table proves it. Simplifying conditions this way is a common exam task.

ไทย

De Morgan's laws เขียนใหม่ของการปฏิเสธ: !(a && b) เท่ากับ !a || !b, และ !(a || b) เท่ากับ !a && !b. นิพจน์ boolean สองนิพจน์มีค่า เทียบเท่ากัน หากให้ผลลัพธ์เดียวกันสำหรับอินพุตทั้งหมด – ตารางความจริงพิสูจน์สิ่งนี้ การลดรูปเงื่อนไขด้วยวิธีนี้เป็นโจทย์สอบที่พบบ่อย

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
De Morgan's laws/də ˈmɔːɡənz lɔːz/ กฎของเด摩根
2.7

while Loops

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 2.7.A: Identify when an iterative process is required to achieve a desired result.

  • 2.7.A.1 Iteration is a form of repetition. Iteration statements change the flow of control by repeating a segment of code zero or more times as long as the Boolean expression controlling the loop evaluates to true.
  • 2.7.A.2 An infinite loop occurs when the Boolean expression in an iterative statement always evaluates to true.
  • 2.7.A.3 The loop body of an iterative statement will not execute if the Boolean expression initially evaluates to false.
  • 2.7.A.4 Off by one errors occur when the iteration statement loops one time too many or one time too few.

Learning Objective 2.7.B: Develop code to represent iterative processes using while loops and determine the result of these processes.

  • 2.7.B.1 A while loop is a type of iterative statement. In while loops, the Boolean expression is evaluated before each iteration of the loop body, including the first. When the expression evaluates to true, the loop body is executed. This continues until the Boolean expression evaluates to false, whereupon the iteration terminates.
ไทย

วัตถุประสงค์การเรียนรู้ 2.7.A: ระบุเมื่อใดที่ต้องการกระบวนการแบบวนซ้ำ (iterative process) เพื่อให้ได้ผลลัพธ์ที่ต้องการ

  • 2.7.A.1 การวนซ้ำ (Iteration) เป็นรูปแบบหนึ่งของการทำซ้ำ คำสั่งการวนซ้ำจะเปลี่ยนการไหลของควบคุมโดยการทำซ้ำส่วนหนึ่งของโค้ดศูนย์หรือมากกว่าครั้ง ตราบเท่าที่นิพจน์ Boolean ที่ควบคุมลูปมีค่าเป็น true
  • 2.7.A.2 ลูปอนันต์ (infinite loop) เกิดขึ้นเมื่อนิพจน์ Boolean ในคำสั่งการวนซ้ำมีค่าเป็น true เสมอ
  • 2.7.A.3 ส่วนลูปของคำสั่งการวนซ้ำจะไม่ถูกดำเนินการหากนิพจน์ Boolean มีค่าเริ่มต้นเป็น false
  • 2.7.A.4 ความผิดพลาดแบบ Off by one เกิดขึ้นเมื่อคำสั่งการวนซ้ำวนซ้ำมากไปหนึ่งรอบหรือน้อยไปหนึ่งรอบ

วัตถุประสงค์การเรียนรู้ 2.7.B: เขียนโค้ดเพื่อแสดงกระบวนการแบบวนซ้ำโดยใช้ while loops และหาผลลัพธ์ของกระบวนการเหล่านี้

  • 2.7.B.1 while loop เป็นประเภทหนึ่งของคำสั่งการวนซ้ำ ใน while loops นิพจน์ Boolean จะถูกประเมินก่อนแต่ละรอบของการวนซ้ำ เช่นเดียวกับรอบแรก เมื่อนิพจน์มีค่าเป็น true ส่วนลูปจะถูกดำเนินการ สิ่งนี้จะดำเนินต่อไปจนกว่านิพจน์ Boolean จะมีค่าเป็น false ซึ่งการวนซ้ำจะหยุดลง

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

A while loop 循环 repeats while its condition stays true, testing before each pass. You must change something inside so the loop eventually stops, or it becomes an infinite loop 无限循环:

ไทย

while loop ทำซ้ำ while เงื่อนไขยังคงเป็นจริง โดยทดสอบ ก่อน การวนแต่ละครั้ง คุณต้องเปลี่ยนแปลงบางอย่างภายในเพื่อให้ลูปหยุดลงในที่สุด มิฉะนั้นจะเป็น infinite loop:

ประเภทของลูปทั้งสามแตกต่างกันที่ตำแหน่งที่ทดสอบเงื่อนไข
ประเภทของลูปทั้งสามแตกต่างกันที่ตำแหน่งที่ทดสอบเงื่อนไข
int i = 0;
while (i < 5) {
    System.out.println(i);
    i++;
}
Explore · ⁨สำรวจ⁩

Trace a while loop · ⁨ติดตามการทำงานของ while loop⁩

A while loop repeats as long as its condition stays true, updating its variables each pass. Step through to see the sum of squares build up. · ⁨while loop จะซ้ำจนกว่าเงื่อนไขจะเป็นจริง และอัปเดตตัวแปรทุกครั้งในการวนลูป ก้าวผ่านเพื่อดูผลรวมกำลังสองค่อยๆ สร้างขึ้น⁩

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
while loop/waɪl luːp/ ลูป while
infinite loop/ˈɪnfɪnət luːp/ ลูปไม่สิ้นสุด
2.8

for Loops

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 2.8.A: Develop code to represent iterative processes using for loops and determine the result of these processes.

  • 2.8.A.1 A for loop is a type of iterative statement. There are three parts in a for loop header: the initialization, the Boolean expression, and the update.
  • 2.8.A.2 In a for loop, the initialization statement is only executed once before the first Boolean expression evaluation. The variable being initialized is referred to as a loop control variable. The Boolean expression is evaluated immediately after the loop control variable is initialized and then following each execution of the increment statement until it is false. In each iteration, the update is executed after the entire loop body is executed and before the Boolean expression is evaluated again.
  • 2.8.A.3 A for loop can be rewritten into an equivalent while loop (and vice versa).
ไทย

วัตถุประสงค์การเรียนรู้ 2.8.A: เขียนโค้ดเพื่อแสดงกระบวนการแบบวนซ้ำโดยใช้ for loops และหาผลลัพธ์ของกระบวนการเหล่านี้

  • 2.8.A.1 for loop เป็นประเภทหนึ่งของคำสั่งการวนซ้ำ หัวของ for loop ประกอบด้วยสามส่วน: การเริ่มต้น, นิพจน์ Boolean และการอัปเดต
  • 2.8.A.2 ใน for loop คำสั่งเริ่มต้นจะถูกดำเนินการเพียงครั้งเดียวก่อนการประเมินนิพจน์ Boolean ครั้งแรก ตัวแปรที่ถูกเริ่มต้นเรียกว่า ตัวแปรควบคุมลูป (loop control variable) นิพจน์ Boolean จะถูกประเมินทันทีหลังจากตัวแปรควบคุมลูปได้รับการเริ่มต้น และจากนั้นตามหลังการดำเนินการของแต่ละคำสั่งเพิ่มค่า จนกว่าจะมีค่าเป็น false ในแต่ละรอบการวนซ้ำ คำสั่งอัปเดตจะถูกดำเนินการหลังจากส่วนลูปทั้งหมดถูกดำเนินการและก่อนที่นิพจน์ Boolean จะถูกประเมินอีกครั้ง
  • 2.8.A.3 for loop สามารถเขียนใหม่ให้เป็น while loop ที่เทียบเท่ากันได้ (และในทางกลับกัน)

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

A for loop packs initialization, condition, and update into one line – best when you know the count:

A for and an equivalent while do the same work; be able to convert between them.

ไทย

for loop รวบรวมการเริ่มต้น เงื่อนไข และการอัปเดตไว้ในบรรทัดเดียว – เหมาะที่สุดเมื่อคุณทราบจำนวนรอบ:

for (int i = 0; i < n; i++) {
    // runs n times, i = 0..n-1
}

A for และ A equivalent while ทำงานเหมือนกัน; ต้องสามารถแปลงระหว่างกันได้

สายพานลำเลียง: ลูปทำซ้ำกระบวนการสำหรับทุกไอเทม เหมือนกับ for และ while
สายพานลำเลียง: ลูปทำซ้ำกระบวนการสำหรับทุกไอเทม เหมือนกับ for และ while
Explore · ⁨สำรวจ⁩

Trace a for loop · ⁨ติดตามการทำงานของ for loop⁩

A for loop runs a fixed number of times, its counter stepping through a range. Watch the counter and running total advance one pass at a time. · ⁨for loop จะรันจำนวนครั้งคงที่ ตัวนับจะก้าวผ่านช่วงที่กำหนด สังเกตตัวนับและผลรวมสะสมเคลื่อนที่ทีละรอบ⁩

2.9

Building Complete Selection and Iteration Algorithms · ⁨สร้างอัลกอริทึม Selection และ Iteration แบบสมบูรณ์⁩

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 2.9.A: Develop code for standard and original algorithms (without data structures) and determine the result of these algorithms.

  • 2.9.A.1 There are standard algorithms to:
    • identify if an integer is or is not evenly divisible by another integer
    • identify the individual digits in an integer
    • determine the frequency with which a specific criterion is met
    • determine a minimum or maximum value
    • compute a sum or average
ไทย

วัตถุประสงค์การเรียนรู้ 2.9.A: เขียนโค้ดสำหรับอัลกอริทึมมาตรฐานและอัลกอริทึมต้นฉบับ (โดยไม่ใช้โครงสร้างข้อมูล) และหาผลลัพธ์ของอัลกอริทึมเหล่านี้

  • 2.9.A.1 มีอัลกอริทึมมาตรฐานสำหรับ:
    • ระบุว่ามีจำนวนเต็มเป็นหรือไม่เป็นผลหารที่ลงตัวกับจำนวนเต็มอื่นหรือไม่
    • ระบุหลักเลขแต่ละหลักของจำนวนเต็ม
    • หาความถี่ที่เกณฑ์เฉพาะถูกบรรลุ
    • หาค่าต่ำสุดหรือสูงสุด
    • คำนวณผลรวมหรือค่าเฉลี่ย

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

Combine loops and conditions to solve real problems – count, sum, find a maximum, or test a property:

Two integer patterns the exam tests directly use % and /. To read the digits of an integer one at a time, repeatedly take n % 10 (the last digit) and then n = n / 10 (drop it). To test divisibility, n % d == 0 means n is evenly divisible by d. Combine them with a counter to find the frequency with which some criterion is met.

Standard patterns like a running total, a counter, or a flag 标志 (a boolean that records whether something happened) recur throughout the course.

ไทย

รวมลูปและเงื่อนไขเพื่อแก้ปัญหาจริง – นับผล, ผลรวม, หาค่าสูงสุด, หรือทดสอบคุณสมบัติ:

int max = arr[0];
for (int k = 1; k < arr.length; k++) {
    if (arr[k] > max) max = arr[k];
}

รูปแบบจำนวนเต็มสองแบบที่ข้อสอบทดสอบโดยตรงใช้ % และ /. ในการ อ่านตัวเลขของจำนวนเต็ม ตัวต่อตัว ทำซ้ำการนำ n % 10 (เลขตัวสุดท้าย) และตามด้วยการ n = n / 10 (ตัดทิ้ง) เพื่อทดสอบ หารลงตัว, n % d == 0 หมายความว่า n หารด้วย d ลงตัว การรวมเข้ากับ counter เพื่อ หาความถี่ ที่เกณฑ์ใดเกณฑ์หนึ่งเกิดขึ้น

รูปแบบมาตรฐานเช่น running total, counter, หรือ flag (boolean ที่บันทึกว่าเกิดเหตุการณ์ขึ้นหรือไม่) ปรากฏซ้ำ throughout หลักสูตร

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
flag/flæɡ/ flag
2.10

String Algorithms

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 2.10.A: Develop code for standard and original algorithms that involve strings and determine the result of these algorithms.

  • 2.10.A.1 There are standard string algorithms to:
    • find if one or more substrings have a particular property
    • determine the number of substrings that meet specific criteria
    • create a new string with the characters reversed
ไทย

วัตถุประสงค์การเรียนรู้ 2.10.A: พัฒนาโค้ดสำหรับอัลกอริทึมมาตรฐานและอัลกอริทึมดั้งเดิมที่เกี่ยวข้องกับสตริงและ xác địnhผลลัพธ์ของอัลกอริทึมเหล่านี้

  • 2.10.A.1 มีอัลกอริทึมสตริงมาตรฐานเพื่อ:
    • ค้นหาว่ามีสับสตริงหนึ่งหรือมากกว่ามีคุณสมบัติเฉพาะหรือไม่
    • จำนวนสับสตริงที่ตรงตามเกณฑ์เฉพาะ
    • สร้างสตริงใหม่โดยกลับลำดับตัวอักษร

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

Loop through a string by index to process each character:

Typical tasks: count occurrences, build a reversed or filtered copy, or test whether one string contains another.

ไทย

วนลูปผ่าน string ด้วย index เพื่อประมวลผลแต่ละอักขระ:

for (int i = 0; i < s.length(); i++) {
    char c = s.charAt(i);
    // count vowels, reverse, check for a substring, ...
}

งานทั่วไป: นับจำนวนครั้งที่ปรากฏ, สร้างสำเนาที่กลับหัวหรือกรอง, หรือทดสอบว่า string หนึ่งมีอีก string หนึ่งอยู่หรือไม่

2.11

Nested Iteration

Syllabus · ⁨หลักสูตร⁩
English
Learning ObjectiveEssential Knowledge

2.11.A
Develop code to represent nested iterative processes and determine the result of these processes.

  • 2.11.A.1 Nested iteration statements are iteration statements that appear in the body of another iteration statement. When a loop is nested inside another loop, the inner loop must complete all its iterations before the outer loop can continue to its next iteration.
ไทย
Learning ObjectiveEssential Knowledge

2.11.A
พัฒนาโค้ดเพื่อแทนกระบวนการ iterative แบบซ้อนและกำหนดผลลัพธ์ของกระบวนการเหล่านี้

  • 2.11.A.1 คำสั่งทำซ้ำแบบซ้อน (Nested iteration statements) คือคำสั่งทำซ้ำที่ปรากฏอยู่ในตัวของคำสั่งทำซ้ำอื่น เมื่อมีลูปถูกซ้อนอยู่ภายในอีกลูปหนึ่ง ลูปด้านในจะต้องดำเนินการจนครบทุกรอบก่อนที่ลูปภายนอกจะสามารถดำเนินต่อไปยังรอบถัดไปได้

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English

A nested loop 嵌套循环 puts one loop inside another; the inner loop completes fully for each pass of the outer. If the outer runs $n$ times and the inner $m$ times, the body runs $n\times m$ times – the basis for processing grids and comparing all pairs.

ไทย

nested loop วางลูปหนึ่งไว้ภายในอีกลูปหนึ่ง; ลูปภายในจะทำงานจนเสร็จสำหรับ แต่ละ รอบของลูปภายนอก หากลูปภายนอกวน $n$ ครั้งและลูปภายใน $m$ ครั้ง, body จะวน $n\times m$ ครั้ง – เป็นพื้นฐานสำหรับการประมวลผลตารางและเปรียบเทียบทุกคู่

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
nested loop/ˈnestɪd luːp/ ลูปซ้อน
2.12

Informal Run-Time Analysis

Syllabus · ⁨หลักสูตร⁩
English

Learning Objective 2.12.A: Calculate statement execution counts and informal run-time comparison of iterative statements.

  • 2.12.A.1 A statement execution count indicates the number of times a statement is executed by the program. Statement execution counts are often calculated informally through tracing and analysis of the iterative statements.
ไทย

วัตถุประสงค์การเรียนรู้ 2.12.A: คำนวณจำนวนครั้งของการดำเนินการคำสั่งและเปรียบเทียบเวลาในการรันแบบไม่เป็นทางการของคำสั่งทำซ้ำ

  • 2.12.A.1 จำนวนครั้งของการดำเนินการคำสั่ง (Statement execution count) บ่งบอกถึงจำนวนครั้งที่โปรแกรมได้ดำเนินการคำสั่งนั้น จำนวนครั้งของการดำเนินการคำสั่งมักถูกคำนวณโดยวิธีไม่เป็นทางการผ่านการติดตามและวิเคราะห์คำสั่งทำซ้ำ

Source: College Board AP Course and Exam Description · ⁨แหล่งที่มา: คำอธิบายหลักสูตรและข้อสอบ College Board AP⁩

English
Big-O growth rates

Run-time analysis 运行时间分析 counts how many basic steps an algorithm takes as the input size $n$ grows. Count the executions of the innermost statement: a single loop over $n$ items is linear ($n$ steps); two nested loops over $n$ are quadratic ($n^2$). This informal counting lets you compare two algorithms' efficiency.

Exam skill: for a nested loop, be able to state how many times the inner statement runs in terms of the loop bounds – a frequent multiple-choice question.

Worked example. How many stars does this print?

The inner loop runs i times for each outer i: 0 + 1 + 2 + 3 = 6 stars. When the inner bound is the outer variable, the total is the triangular sum $0+1+\dots+(n-1)=\dfrac{n(n-1)}{2}$ – here $\dfrac{4\times3}{2}=6$ – not the full $n^2=16$ of a rectangular nested loop.

ไทย
Big-O growth rates

Run-time analysis นับว่ามีขั้นตอนพื้นฐานกี่ขั้นตอนที่อัลกอริทึมใช้ เมื่อขนาดอินพุต $n$ เพิ่มขึ้น นับจำนวนครั้งที่被执行ของ statement ภายในสุด: ลูปเดี่ยวบน $n$ ไอเทมคือ linear ($n$ ขั้นตอน); ลูปซ้อนกันสองชั้นบน $n$ คือ quadratic ($n^2$). การนับแบบไม่ทางการนี้ช่วยให้เปรียบเทียบประสิทธิภาพของอัลกอริทึมสองตัวได้

เวลาการทำงานเพิ่มขึ้นตามจำนวนองค์ประกอบ n อย่างไร
เวลาการทำงานเพิ่มขึ้นตามจำนวนองค์ประกอบ n อย่างไร

ทักษะข้อสอบ: สำหรับ nested loop ต้องระบุได้ว่า statement ภายในวนกี่ครั้งโดยอ้างอิงจากขอบเขตของลูป – ข้อสอบ multiple-choice ที่พบบ่อย

ตัวอย่างที่ทำแล้ว. ตัวอย่างนี้จะพิมพ์ดาวกี่ดวง?

for (int i = 0; i < 4; i++)
    for (int j = 0; j < i; j++)
        System.out.print("*");

ลูปภายในทำงาน i ครั้งสำหรับแต่ละลูปภายนอก i: มีดาว 0 + 1 + 2 + 3 = 6 เมื่อขอบเขตของลูปภายในเป็นตัวแปร ภายนอก ผลรวมจะเป็นผลบวกสามเหลี่ยม $0+1+\dots+(n-1)=\dfrac{n(n-1)}{2}$ – ที่นี่คือ $\dfrac{4\times3}{2}=6$ – ไม่ใช่ผลเต็ม $n^2=16$ ของลูปซ้อนแบบสี่เหลี่ยมผืนผ้า

Explore · ⁨สำรวจ⁩

Compare how algorithms scale · ⁨เปรียบเทียบการขยายตัวของอัลกอริทึม⁩

Run-time describes how the number of steps grows with the input size $n$. Increase $n$ and watch a linear $O(n)$ pull far ahead of a quadratic $O(n^2)$. · ⁨Run-time อธิบายว่าจำนวนขั้นตอนเพิ่มขึ้นตามขนาดอินพุต $n$ อย่างไร เพิ่ม $n$ แล้วดูว่า linear $O(n)$ ทิ้งห่าง quadratic $O(n^2)$ ได้มากแค่ไหน⁩

Vocabulary · ⁨คำศัพท์⁩ Train · ⁨ฝึกฝน⁩
English ไทย
Run-time analysis/rʌn taɪm əˈnæləsɪs/ การวิเคราะห์ขณะรันไทม์
2.12

Exam tips · ⁨ข้อแนะนำสำหรับการสอบ⁩

English
  • Get boundary conditions right: use < vs <= deliberately, and watch the first and last iteration of every loop (off-by-one is the classic bug).
  • Build compound conditions with &&, ||, ! and remember short-circuit evaluation (put the null check first).
  • Trace nested loops by counting how many times the inner body runs in total.
  • Choose the right structure — if/else if for ranges, a loop for repetition — and avoid an infinite loop by updating the loop variable.
  • Apply De Morgan's laws when you simplify or negate a boolean condition.
ไทย
  • ตั้งค่าเงื่อนไขขอบเขตให้ถูกต้อง: ใช้ < เทียบกับ <= อย่างตั้งใจ และสังเกตการวนซ้ำครั้งแรกและครั้งสุดท้ายของแต่ละลูป (ข้อผิดพลาด off-by-one เป็นปัญหาคลาสสิก)
  • สร้างเงื่อนไขที่ซับซ้อนด้วย &&, || และ ! และจำไว้ว่ามีการประเมินแบบ short-circuit (วางการตรวจสอบ null ไว้ก่อน)
  • ติดตามลูปซ้อนด้วยการนับว่า ตัวลูปภายใน ทำงานทั้งหมดกี่ครั้ง
  • เลือกโครงสร้างที่เหมาะสม — ใช้ if/else if สำหรับช่วงจำนวน และใช้ลูปสำหรับการทำซ้ำ — และหลีกเลี่ยงลูปไม่สิ้นสุดโดยอัปเดตตัวแปรลูป
  • ใช้ กฎของเด摩根 เมื่อคุณลดรูปหรือสร้างค่าตรงข้ามของเงื่อนไข boolean

Interactive lessons on this topic · ⁨บทเรียนเชิงโต้ตอบสำหรับหัวข้อนี้⁩

Work through it step by step, with instant-check exercises. · ⁨ทำทีละขั้นตอน พร้อมแบบฝึกหัดตรวจสอบผลทันที⁩

Past Papers · ⁨ข้อสอบย้อนหลัง⁩

More topics in AP Computer Science A · ⁨หัวข้อเพิ่มเติมใน AP Computer Science A⁩

Log in or create account · ⁨เข้าสู่ระบบหรือสร้างบัญชี⁩

IGCSE, A-Level & AP