SlideShare uma empresa Scribd logo
1 de 8
การวนซำ้า โดยใช้
โครงสร้า ง for
คำาสัง for
่
สะดวกสำาหรับการนับรอบของลูป



for (init_stmt; condition; update_stmt)
for (init_stmt; condition; update_stmt)
statement;
statement;



การดำาเนินการที่เกี่ยวข้อง

ทำา init_ stmt หนึงครั้ง
่
2. ตรวจสอบ co nditio n ; ถ้าเป็นจริงให้ทำางาน
ใน bo dy lo o p
3. ทำา update _ stmt, แล้ update_stmt){้น
ทำางานในขั
for (init_stmt; condition; วupdate_stmt){ ตอนที่ 2
for (init_stmt; condition;
1.

}
}

statement1;
statement1;
statement2;
statement2;
:
:
statementN;
statementN;
คำำสัง for: ผัง กำรทำำ งำน
่
START
START
Initialize counter
Initialize counter

condition
condition

false

true
Statement
Statement
Statement
Statement
Update counter
Update counter

END
END
for vs. while


คำำสัง: พิมพ์เลข 1,2,...,10 ออกหน้ำจอ
่

using System;
using System;

using System;
using System;

class Counting {
class Counting {
static void Main() {
static void Main() {
int i;
int i;

class Counting {
class Counting {
static void Main() {
static void Main() {
int i;
int i;

}
}

}
}

i = 1;
i = 1;
while (i <= 10) {
while (i <= 10) {
Console.WriteLine(i);
Console.WriteLine(i);
i++;
i++;
}
}

while loop

}
}

}
}

for (i = 1; i <= 10; i++) {
for (i = 1; i <= 10; i++) {
Console.WriteLine(i);
Console.WriteLine(i);
}
}

for loop
ทดสอบ: พิม พ์ด าว



เขียนโปรแกรมเพื่อพิมพ์ดาวจำานวน N
ดวง เมื่อ Nกำาหนดโดยผูใช้
้
ตัวอย่าง
using System;
using System;

Enter N: 3
Enter N: 3
***
***
Enter N: 8
Enter N: 8
********
********

class Stars {
class Stars {
static void Main() {
static void Main() {
int i,N;
int i,N;
Console.Write("Enter N: ");
Console.Write("Enter N: ");
N = int.Parse(Console.ReadLine());
N = int.Parse(Console.ReadLine());
?
?
for (i = 1; i <= N; i++) {
for (i ? 1; i <= N; i++) {
=
???
Console.Write("*");
Console.Write("*");
}
}

}
}

}
}

Console.WriteLine();
Console.WriteLine();
ตัว อย่า ง


แสดงค่า 0,2,4,...,20
for (i = 0; i <= 20; i += 2)
for (i = 0; i <= 20; i += 2)
Console.WriteLine(i);
Console.WriteLine(i);



แสดงค่า 1,3,5,...,19

?
?
?
for (i = 1; i <= 19; i += 2)
for (i = 1; i <= 19; i += 2)
Console.WriteLine(i);
Console.WriteLine(i);



แสดงค่า 15,12,9,...,0
?

?

?

for (i = 15; i >= 0; i -= 3)
for (i = 15; i >= 0; i -= 3)
Console.WriteLine(i);
Console.WriteLine(i);
ลูป หลายชั้น


โปรแกรมที่มีความซับซ้อนมากขึ้นอาจมี
ความต้องการใช้ลูปหลายชั้น




คล้ายกันกับคำาสัง if
่

ตัวอย่าง
N

using System;
using System;

i

sum = ∑∑ j
i =1 j =1

Inner
Loop

class Sum {
class Sum {
static void Main() {
static void Main() {
int sum = 0, i, j, N;
int sum = 0, i, j, N;
Console.Write("Enter N: ");
Console.Write("Enter N: ");
N = int.Parse(Console.ReadLine());
N = int.Parse(Console.ReadLine());
for (i = 1; i <= N; i++) {
for (i = 1; i <= N; i++) {
for (j = 1; j <= i; j++) {
for (j = 1; j <= i; j++) {
Outer
sum = sum + j;
sum = sum + j;
Loop
}
}
}
}
Console.WriteLine("Sum is {0}", sum);
Console.WriteLine("Sum is {0}", sum);
}
}
}
}
ตัว อย่า ง: พิม พ์ด าวอีก
ครัง !
้



เพิมเติมจากโปรแกรมพิมพ์ดาวเดิม
่
ตัวอย่าง
Enter N: 3
Enter N: 3
*
*
**
**
***
***
Enter N: 5
Enter N: 5
*
*
**
**
***
***
****
****
*****
*****

using System;
using System;
class Stars2 {
class Stars2 {
static void Main() {
static void Main() {
int i, j, N;
int i, j, N;
N = int.Parse(Console.ReadLine());
N = int.Parse(Console.ReadLine());
for (i = 1; i <= N; i++) {
for (i = 1; i <= N; i++) {
for (j = 1; j <= i; j++) {
for (j = 1; j <= i; j++) {
Console.Write("*");
Console.Write("*");
}
}
Console.WriteLine();
Console.WriteLine();
}
}
}
}
}
}

Mais conteúdo relacionado

Mais procurados (20)

Flowchar5
Flowchar5Flowchar5
Flowchar5
 
Know3 2
Know3 2Know3 2
Know3 2
 
Unit10
Unit10Unit10
Unit10
 
05 loops
05 loops05 loops
05 loops
 
Know4 1
Know4 1Know4 1
Know4 1
 
comproyui
comproyuicomproyui
comproyui
 
โปรแกรม
โปรแกรมโปรแกรม
โปรแกรม
 
Week5
Week5Week5
Week5
 
Unit7
Unit7Unit7
Unit7
 
คอมโปรเฟรม
คอมโปรเฟรมคอมโปรเฟรม
คอมโปรเฟรม
 
ภาษา C เบื้องต้น
ภาษา C เบื้องต้นภาษา C เบื้องต้น
ภาษา C เบื้องต้น
 
Control structure
Control structureControl structure
Control structure
 
Week8
Week8Week8
Week8
 
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
นางสาว จรัญญา-กฤตย์ณัชช์-59170236-กลุ่ม-1
 
Week8
Week8Week8
Week8
 
เริ่มต้นกับภาษาซี
เริ่มต้นกับภาษาซีเริ่มต้นกับภาษาซี
เริ่มต้นกับภาษาซี
 
Matlab
MatlabMatlab
Matlab
 
Matlap
MatlapMatlap
Matlap
 
Week6
Week6Week6
Week6
 
1 test
1  test1  test
1 test
 

Destaque

04 conditional
04 conditional04 conditional
04 conditional
a-num Sara
 
09 multi arrays
09 multi arrays09 multi arrays
09 multi arrays
a-num Sara
 
หน่วยการเรียนที่ 3 เรียนรู้ไวยากรณ์ ตัวแปร ค่าคงที่ comment
หน่วยการเรียนที่ 3 เรียนรู้ไวยากรณ์ ตัวแปร ค่าคงที่ commentหน่วยการเรียนที่ 3 เรียนรู้ไวยากรณ์ ตัวแปร ค่าคงที่ comment
หน่วยการเรียนที่ 3 เรียนรู้ไวยากรณ์ ตัวแปร ค่าคงที่ comment
ครูสม ฟาร์มมะนาว
 
หน่วยการเรียนที่ 4 การใช้คำสั่งควบคุมและการตัดสินใจ vb2013 express
หน่วยการเรียนที่ 4 การใช้คำสั่งควบคุมและการตัดสินใจ vb2013 expressหน่วยการเรียนที่ 4 การใช้คำสั่งควบคุมและการตัดสินใจ vb2013 express
หน่วยการเรียนที่ 4 การใช้คำสั่งควบคุมและการตัดสินใจ vb2013 express
ครูสม ฟาร์มมะนาว
 
บทที่ 2 สถาปัตยกรรมและแบบจำลองฐานข้อมูล
บทที่ 2 สถาปัตยกรรมและแบบจำลองฐานข้อมูลบทที่ 2 สถาปัตยกรรมและแบบจำลองฐานข้อมูล
บทที่ 2 สถาปัตยกรรมและแบบจำลองฐานข้อมูล
ครูสม ฟาร์มมะนาว
 
คำสั่งควบคุม
คำสั่งควบคุมคำสั่งควบคุม
คำสั่งควบคุม
umaraporn
 

Destaque (20)

C#
C#C#
C#
 
160
160160
160
 
10 win apps
10 win apps10 win apps
10 win apps
 
02 basic
02 basic02 basic
02 basic
 
04 conditional
04 conditional04 conditional
04 conditional
 
09 multi arrays
09 multi arrays09 multi arrays
09 multi arrays
 
07 methods
07 methods07 methods
07 methods
 
Joomla 56 km
Joomla 56 kmJoomla 56 km
Joomla 56 km
 
หน่วยการเรียนที่ 3 เรียนรู้ไวยากรณ์ ตัวแปร ค่าคงที่ comment
หน่วยการเรียนที่ 3 เรียนรู้ไวยากรณ์ ตัวแปร ค่าคงที่ commentหน่วยการเรียนที่ 3 เรียนรู้ไวยากรณ์ ตัวแปร ค่าคงที่ comment
หน่วยการเรียนที่ 3 เรียนรู้ไวยากรณ์ ตัวแปร ค่าคงที่ comment
 
บทที่ 3 ฐานข้อมูลเชิงสัมพันธ์
บทที่ 3 ฐานข้อมูลเชิงสัมพันธ์บทที่ 3 ฐานข้อมูลเชิงสัมพันธ์
บทที่ 3 ฐานข้อมูลเชิงสัมพันธ์
 
08 arrays
08 arrays08 arrays
08 arrays
 
หน่วยการเรียนที่ 4 การใช้คำสั่งควบคุมและการตัดสินใจ vb2013 express
หน่วยการเรียนที่ 4 การใช้คำสั่งควบคุมและการตัดสินใจ vb2013 expressหน่วยการเรียนที่ 4 การใช้คำสั่งควบคุมและการตัดสินใจ vb2013 express
หน่วยการเรียนที่ 4 การใช้คำสั่งควบคุมและการตัดสินใจ vb2013 express
 
ภาษา C# เบื้องต้น
ภาษา C# เบื้องต้นภาษา C# เบื้องต้น
ภาษา C# เบื้องต้น
 
Java Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่นJava Programming: อะเรย์และคอลเล็กชั่น
Java Programming: อะเรย์และคอลเล็กชั่น
 
บทที่ 2 สถาปัตยกรรมและแบบจำลองฐานข้อมูล
บทที่ 2 สถาปัตยกรรมและแบบจำลองฐานข้อมูลบทที่ 2 สถาปัตยกรรมและแบบจำลองฐานข้อมูล
บทที่ 2 สถาปัตยกรรมและแบบจำลองฐานข้อมูล
 
ภาษา C#
ภาษา C#ภาษา C#
ภาษา C#
 
คำสั่งควบคุม
คำสั่งควบคุมคำสั่งควบคุม
คำสั่งควบคุม
 
การประมวลผลข้อมูล
การประมวลผลข้อมูลการประมวลผลข้อมูล
การประมวลผลข้อมูล
 
ความรู้เบื้องต้นเกี่ยวกับ Internet
ความรู้เบื้องต้นเกี่ยวกับ Internetความรู้เบื้องต้นเกี่ยวกับ Internet
ความรู้เบื้องต้นเกี่ยวกับ Internet
 
คำสั่งในการวนรอบการทำงาน Vb2010 (1)
คำสั่งในการวนรอบการทำงาน Vb2010 (1)คำสั่งในการวนรอบการทำงาน Vb2010 (1)
คำสั่งในการวนรอบการทำงาน Vb2010 (1)
 

Semelhante a 06 for loops

บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบค
Theeravaj Tum
 
Computer programming
Computer  programmingComputer  programming
Computer programming
Preaw Jariya
 
อัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหาอัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหา
supatra178
 
2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี
mansuang1978
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Finian Nian
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
Finian Nian
 

Semelhante a 06 for loops (20)

บทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบคบทที่ 3 คำสั่งควบค
บทที่ 3 คำสั่งควบค
 
C lang
C langC lang
C lang
 
Chapter 05 Loop
Chapter 05 LoopChapter 05 Loop
Chapter 05 Loop
 
Chapter 05 Loop
Chapter 05 LoopChapter 05 Loop
Chapter 05 Loop
 
การเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา Cการเขียนฟังก์ชั่นในภาษา C
การเขียนฟังก์ชั่นในภาษา C
 
Computer programming
Computer  programmingComputer  programming
Computer programming
 
Computer programming
Computer programmingComputer programming
Computer programming
 
Lesson3
Lesson3Lesson3
Lesson3
 
Know4 3
Know4 3Know4 3
Know4 3
 
Tec4
Tec4Tec4
Tec4
 
capture 59170107 group 2
capture 59170107 group 2capture 59170107 group 2
capture 59170107 group 2
 
อัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหาอัลกอริทึมและการวิเคราะห์ปัญหา
อัลกอริทึมและการวิเคราะห์ปัญหา
 
C slide
C slideC slide
C slide
 
Know3 4
Know3 4Know3 4
Know3 4
 
6.Flow control
6.Flow control6.Flow control
6.Flow control
 
chapter 3 คำสั่งควบคุม
chapter 3 คำสั่งควบคุมchapter 3 คำสั่งควบคุม
chapter 3 คำสั่งควบคุม
 
2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี2. โครงสร้างภาษาซี
2. โครงสร้างภาษาซี
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้นคลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
คลาสและการเขียนโปรแกรมเชิงวัตถุเบื้องต้น
 
Computer Programming 3
Computer Programming 3 Computer Programming 3
Computer Programming 3
 

06 for loops

  • 2. คำาสัง for ่ สะดวกสำาหรับการนับรอบของลูป  for (init_stmt; condition; update_stmt) for (init_stmt; condition; update_stmt) statement; statement;  การดำาเนินการที่เกี่ยวข้อง ทำา init_ stmt หนึงครั้ง ่ 2. ตรวจสอบ co nditio n ; ถ้าเป็นจริงให้ทำางาน ใน bo dy lo o p 3. ทำา update _ stmt, แล้ update_stmt){้น ทำางานในขั for (init_stmt; condition; วupdate_stmt){ ตอนที่ 2 for (init_stmt; condition; 1. } } statement1; statement1; statement2; statement2; : : statementN; statementN;
  • 3. คำำสัง for: ผัง กำรทำำ งำน ่ START START Initialize counter Initialize counter condition condition false true Statement Statement Statement Statement Update counter Update counter END END
  • 4. for vs. while  คำำสัง: พิมพ์เลข 1,2,...,10 ออกหน้ำจอ ่ using System; using System; using System; using System; class Counting { class Counting { static void Main() { static void Main() { int i; int i; class Counting { class Counting { static void Main() { static void Main() { int i; int i; } } } } i = 1; i = 1; while (i <= 10) { while (i <= 10) { Console.WriteLine(i); Console.WriteLine(i); i++; i++; } } while loop } } } } for (i = 1; i <= 10; i++) { for (i = 1; i <= 10; i++) { Console.WriteLine(i); Console.WriteLine(i); } } for loop
  • 5. ทดสอบ: พิม พ์ด าว   เขียนโปรแกรมเพื่อพิมพ์ดาวจำานวน N ดวง เมื่อ Nกำาหนดโดยผูใช้ ้ ตัวอย่าง using System; using System; Enter N: 3 Enter N: 3 *** *** Enter N: 8 Enter N: 8 ******** ******** class Stars { class Stars { static void Main() { static void Main() { int i,N; int i,N; Console.Write("Enter N: "); Console.Write("Enter N: "); N = int.Parse(Console.ReadLine()); N = int.Parse(Console.ReadLine()); ? ? for (i = 1; i <= N; i++) { for (i ? 1; i <= N; i++) { = ??? Console.Write("*"); Console.Write("*"); } } } } } } Console.WriteLine(); Console.WriteLine();
  • 6. ตัว อย่า ง  แสดงค่า 0,2,4,...,20 for (i = 0; i <= 20; i += 2) for (i = 0; i <= 20; i += 2) Console.WriteLine(i); Console.WriteLine(i);  แสดงค่า 1,3,5,...,19 ? ? ? for (i = 1; i <= 19; i += 2) for (i = 1; i <= 19; i += 2) Console.WriteLine(i); Console.WriteLine(i);  แสดงค่า 15,12,9,...,0 ? ? ? for (i = 15; i >= 0; i -= 3) for (i = 15; i >= 0; i -= 3) Console.WriteLine(i); Console.WriteLine(i);
  • 7. ลูป หลายชั้น  โปรแกรมที่มีความซับซ้อนมากขึ้นอาจมี ความต้องการใช้ลูปหลายชั้น   คล้ายกันกับคำาสัง if ่ ตัวอย่าง N using System; using System; i sum = ∑∑ j i =1 j =1 Inner Loop class Sum { class Sum { static void Main() { static void Main() { int sum = 0, i, j, N; int sum = 0, i, j, N; Console.Write("Enter N: "); Console.Write("Enter N: "); N = int.Parse(Console.ReadLine()); N = int.Parse(Console.ReadLine()); for (i = 1; i <= N; i++) { for (i = 1; i <= N; i++) { for (j = 1; j <= i; j++) { for (j = 1; j <= i; j++) { Outer sum = sum + j; sum = sum + j; Loop } } } } Console.WriteLine("Sum is {0}", sum); Console.WriteLine("Sum is {0}", sum); } } } }
  • 8. ตัว อย่า ง: พิม พ์ด าวอีก ครัง ! ้   เพิมเติมจากโปรแกรมพิมพ์ดาวเดิม ่ ตัวอย่าง Enter N: 3 Enter N: 3 * * ** ** *** *** Enter N: 5 Enter N: 5 * * ** ** *** *** **** **** ***** ***** using System; using System; class Stars2 { class Stars2 { static void Main() { static void Main() { int i, j, N; int i, j, N; N = int.Parse(Console.ReadLine()); N = int.Parse(Console.ReadLine()); for (i = 1; i <= N; i++) { for (i = 1; i <= N; i++) { for (j = 1; j <= i; j++) { for (j = 1; j <= i; j++) { Console.Write("*"); Console.Write("*"); } } Console.WriteLine(); Console.WriteLine(); } } } } } }