ใบความรู้ที่ 3.2
โครงสร้าง if และ if...else
กำหนด การใช้งานนั้นมีสองรูปแบบคร่าว ๆ ได้แก่
• รูปแบบที่ 1: โครงสร้าง if
จากการใช้งานด้านล่าง คำสั่ง statement จะถูกเรียกทำงานก็ต่อเมื่อนิพจน์ทางตรรกศาสตร์ที่
กำหนดเป็น condition มีค่าเป็นจริง
if (condition)
statement; // executed if the condition is
true
|
เนื่องจากโครงสร้างข้างต้นอนุญาตให้เรากำหนดเงื่อนไขให้กับคำสั่งเพียงคำสั่งเดียวเท่านั้น อย่างไร
ก็ตาม หากมีคำสั่งมากกว่าหนึ่งภายใต้เงื่อนไขเดียวกัน คำสั่งเหล่านี้สามารถถูกจัดกลุ่มให้เป็น
เสมือนคำสั่งเดียวได้โดยการครอบคำสั่งทั้งหมดด้วยวงเล็บปีกกา ({...})
if (condition) {
statement1; // executed if the condition
is true
statement2; // executed if the condition
is true
statement3; // executed if the condition
is true
:
}
|
• รูปแบบที่ 2: โครงสร้าง if...else
คำสั่ง statement1 จะถูกเรียกทำงานเมื่อนิพจน์ในตำแหน่ง condition มีค่าเป็นจริง หาก
นิพจน์ดังกล่าวมีค่าเป็นเท็จ คำสั่ง statement2 จะถูกเรียกทำงานแทน
if (condition)
statement1; //executed if the condition is
true
else
statement2; //executed if the condition is
false
|
และเช่นเคย เราสามารถใช้งานโครงสร้าง if...else ร่วมกับวงเล็บปีกกาหากมีคำสั่งที่ต้องการ
ให้ทำงานภายใต้เงื่อนไขมากกว่าหนึ่ง
if (condition) {
statementT1; //executed if the condition
is true
statementT2; //executed if the condition
is true
}
else {
statementF1; //executed if the condition
is false
statementF2; //executed if the condition
is false
}
|
ตัวอย่างที่ 3.1 รหัสจำลอง (pseudo-code) ด้านล่างอธิบายขั้นตอนวิธีสำหรับให้โปรแกรมพิมพ์คำว่า
Passed หากคะแนนของนักเรียนมีค่ามากกว่าหรือเท่ากับ 60 ไม่เช่นนั้นให้พิมพ์คำว่า Failed
if student's
score is greater than or equal to 60
Print "Passed"
otherwise
Print "Failed"
|
เราสามารถนำรหัสจำลองข้างต้นมาเขียนเป็นโปรแกรมภาษา C# ได้ดังนี้ (แสดงเพียงส่วนสำคัญเท่านั้น)
if (score >=
60)
Console.WriteLine("Passed");
else
Console.WriteLine("Failed");
|
เนื่องจากคีย์เวิร์ด else เป็นตัวกำหนดให้การพิมพ์คำว่า Failed ทำงานเมื่อเงื่อนไข score >= 60
เป็นเท็จ ดังนั้นหากเราแทนที่ else ด้วยคำสั่ง if และใช้เงื่อนไขที่ตรงข้ามกันคือ score < 60
โปรแกรมก็จะมีการทำงานเหมือนกับโปรแกรมข้างบนทุกประการ
if (score >=
60)
Console.WriteLine("Passed");
if (score <
60)
Console.WriteLine("Failed");
|
ตัวอย่างที่ 3.2 โปรแกรมต่อไปนี้จะรอรับตัวเลขจากผู้ใช้และให้คำตอบว่าตัวเลขนั้น ๆ เป็นเลขคู่ (even) หรือ เลขคี่ (odd)
• ใช้รูปแบบ if
using System;
class OddOrEven {
static void Main() {
int
N;
Console.Write("Please input N:
");
N = int.Parse(Console.ReadLine());
if (N%2 == 0)
Console.WriteLine("{0} is
even", N); //true
if (N%2 != 0)
Console.WriteLine("{0} is
odd", N); //true
}
}
|
• ใช้รูปแบบ if...else
using System;
class OddOrEven {
static void Main() {
int N;
Console.Write("Please input N:
");
N = int.Parse(Console.ReadLine());
if (N%2 == 0)
Console.WriteLine("{0} is
even", N); //true
else
Console.WriteLine("{0} is
odd", N); //false
}
}
|
ตัวอย่างที่ 3.3 บริษัทโทรศัพท์มือถือแห่งหนึ่งเสนอโปรโมชั่นให้กับลูกค้าโดยมีการคำนวณค่าธรรมเนียม
การใช้งานดังนี้
• สองนาทีแรก คิดนาทีละห้าบาท
• นาทีถัดมาคิดนาทีละสองบาท
โปรแกรมด้านล่างจะรับค่าจำนวนนาทีจากผู้ใช้ และคำนวณค่าธรรมเนียมการใช้งาน นอกจากนี้
ภายในโปรแกรมยังมีคอมเมนต์กำกับเอาไว้หลายจุดเพื่ออธิบายการทำงานของโปรแกรมในส่วนต่าง ๆ
using System;
class Cellphone {
static void Main() {
// Step 1: Take the number of minutes
input
Console.Write("Enter the number of
minutes: ");
int minutes =
int.Parse(Console.ReadLine());
// Step 2: Split the number of minutes
into two parts,
// the first two and the remaining
int first, remaining;
if (minutes > 2) {
//
Step 2.1: If the call takes more than two minutes,
//
then the first two minutes is used entirely and the
//
remaining is minutes subtracted by two
first = 2;
remaining = minutes - 2;
}
else {
// Step 2.2: If the call takes less
than 2 minutes,
// these minutes are considered part
of the first two
first = minutes;
remaining = 0;
}
// Step 3: Compute the fee based on the
number of minutes
//
during the first two minutes, and the number of minutes
// after the first two minutes
int fee = (first*5) + (remaining*2);
Console.WriteLine("The air time
fee is {0} baht.", fee);
}
}
|
ตัวอย่างผลการทำงาน
Enter the number
of minutes: 1
The
air time fee is 5 baht.
|
Enter the number
of minutes: 5
The
air time fee is 16 baht.
|
ไม่มีความคิดเห็น:
แสดงความคิดเห็น